In this article we are discussing about uploading a blob using the PutBlock method. I would like to give a note that there are two types of blob in Windows Azure:
- Block Blob
- Page Blob
The Block Blob uploading is discussed here as
Block Blobs are used for large file uploads. The associated code was developed
to upload large files to Windows Azure Blob. The large file will be split into 4 MB chunks and then uploaded.
Method
The following are the important methods used:
- CloudBlockBlob.PutBlock()
- CloudBlockBlob.PutBlockList()
The PutBlock method is called for each 4MB file
chunk and the PutBlockList method is called at the end passing the file chunk ids.
Code
The following is the code of AzureBlobUtil.cs
public
class AzureBlobUtil
{
private const
int MaxBlockSize = 4000000;
// Approx. 4MB chunk size
public Uri UploadBlob(string
filePath, CloudStorageAccount account,
string containerName)
{
byte[] fileContent =
File.ReadAllBytes(filePath);
string blobName =
Path.GetFileName(filePath);
return UploadBlob(fileContent, account,
containerName, blobName);
}
public Uri UploadBlob(byte[]
fileContent, CloudStorageAccount account,
string containerName,
string blobName)
{
CloudBlobClient blobclient =
account.CreateCloudBlobClient();
CloudBlobContainer container =
blobclient.GetContainerReference(containerName);
container.CreateIfNotExist();
CloudBlockBlob blob = container.GetBlockBlobReference(blobName);
HashSet<string>
blocklist = new
HashSet<string>();
foreach (FileBlock
block in GetFileBlocks(fileContent))
{
blob.PutBlock(
block.Id,
new
MemoryStream(block.Content, true),
null
);
blocklist.Add(block.Id);
}
blob.PutBlockList(blocklist);
return blob.Uri;
}
private IEnumerable<FileBlock>
GetFileBlocks(byte[] fileContent)
{
HashSet<FileBlock>
hashSet = new
HashSet<FileBlock>();
if (fileContent.Length == 0)
return new
HashSet<FileBlock>();
int blockId = 0;
int ix = 0;
int currentBlockSize = MaxBlockSize;


candidate interviewPosted Dec 20, 2017, 1:28 AM
Can you help me on this http://www.c-sharpcorner.com/Forums/saving-video-to-azure-unable-to-load-video-some-times