Implement multipart resumable upload of large files in azure blob storage cloud storage

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.