The Blob service stores binary and text files. Blob rest APIs can be used to access two types of resources: Containers and blobs. Containers can be seen as folders containing multiple files, while blob is a file belonging to a specific container. There are two blob types:
- Block blobs: This type is used for streaming access.
- Page blobs:This type is used for random read/write operations and can write part of the bytes into blob.
Block blobsYou can create one by using two methods. A block blobs of up to 64 MB can be uploaded by calling the put blob operation. Block blobs larger than 64 MB must be uploaded in multiple parts, each of which cannot exceed 4 MB. After all parts are uploaded successfully, call put
The Block List Operation is combined to form a single continuous blob. Block blob currently supports a maximum of 200 GB.
Page blobsIt can be created and initialized by calling the put blob operation. The maximum size is supported. Write content to page blob by calling the put page operation. Page blob currently supports a maximum of 1 TB.
Blobs supports conditional update. For more information, see:
- "Understanding block blobs and page blobs" on msdn.
- "Blob service concepts" on msdn.
- "Blob Service API" on msdn.
- "Windows azure storage client library" on msdn.
The following code implements the putblock file block. You must call putblocklist to write the file to the end.
The main code is as follows: (putblock returns the blockids array and passes the array to putblocklist)
// Put block - upload a block (portion) of a blob. // Return true on success, false if already exists, throw exception on error. public bool PutBlock(string containerName, string blobName, int blockId, string[] blockIds, byte[] content) { try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); string blockIdBase64 = Convert.ToBase64String(System.BitConverter.GetBytes(blockId)); UTF8Encoding utf8Encoding = new UTF8Encoding(); using (MemoryStream memoryStream = new MemoryStream(content)) { blob.PutBlock(blockIdBase64, memoryStream, null); } blockIds[blockId] = blockIdBase64; return true; } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return false; } throw; } } // Put block list - complete creation of blob based on uploaded content. // Return true on success, false if already exists, throw exception on error. public bool PutBlockList(string containerName, string blobName, string[] blockIds) { try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); blob.PutBlockList(blockIds); return true; } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return false; } throw; } }
Results of uploading a single file of 10 MB using putblock and putblocklist are displayed: