Blob 服务提供二进制文件和文本文件的存储。通过Blob的 REST API 能够访问2种资源: Containers 和 Blobs。容器可以看做包含多个文件的文件夹,而blob便是属于某个容器的文件。有如下2种blob:
- Block Blobs: 该类型用于流式访问。
- Page Blobs: 该类型用于随机读写操作,能够向blob中写入一部分字节。
Block Blobs 可以通过2种方法创建。 不超过64MB 的Block blobs 可以通过调用Put Blob 操作进行上传。大于64 MB的 Block blobs 必须分块上传,每块不超过4MB。当所有的分块成功上传之后,通过调用Put Block List操作进行合并,成为单个连续的blob。Block blob目前最大支持200GB。
Page blobs 可以由调用Put Blob操作来创建和初始化,支持最大尺寸。通过调用Put Page 操作,向page blob写入内容。Page blob 目前最大支持 1 TB。
Blobs 支持条件更新,更多请参见:
- "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.
以下代码实现PutBlock文件块,最后需调用 PutBlockList实现最后写入文件。
主要代码如下:(PutBlock返回的blockIds数组,最后要将此数组传入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;
- }
- }
下图显示用PutBlock,PutBlockList上传10MB单个文件结果:
原文:
http://blog.csdn.net/lihonggen0/article/details/6924107