none
How to Upload Large File By Splitting Into Blocks In Windows Azure Blob Storage Using Java RRS feed

答案

  • hi Sir,

    关于将文件分割为block blob,并且上传至Blob stroage, 基本思路如下:

    1.split large file and upload each block blob

    // list of all uploaded block ids. need for commiting them at the end
    var blockIdList = new List<string>();
     
     
    using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            // block counter
            var blockId = 0;
     
            // open file
            while (file.Position < file.Length)
            {
                // calculate buffer size (blockSize in KB)   
                var bufferSize = blockSize * 1024 < file.Length - file.Position ? blockSize * 1024 : file.Length - file.Position;
                var buffer = new byte[bufferSize]; 
                // read data to buffer
                file.Read(buffer, 0, buffer.Length); 
     
                // save data to memory stream and put to storage
                using (var stream = new MemoryStream(buffer))
                {
                    // set stream position to start
                    stream.Position = 0;
                    // convert block id to Base64 Encoded string      
                    var blockIdBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId.ToString(CultureInfo.InvariantCulture)));
                    blob.PutBlock(blockIdBase64, stream, null);
                    blockIdList.Add(blockIdBase64);
                    // increase block id
                    blockId++;
                }
            }
     
            fileStream.Close();
        }
    }
    

    2. commit the block blob list:

    blob.PutBlockList(blockIdList);
    

    你可以在下载Azure storage client library for java 的包从这个页面: 

    http://blogs.msdn.com/b/windowsazurestorage/archive/2013/12/19/windows-azure-storage-client-library-for-java-v-0-5-0.aspx

    如果倾向于使用REST API, 可参考这个文档:

    http://msdn.microsoft.com/en-us/library/azure/dd135726.aspx

    同时可参考 Gaurav使用js和html写成的上传实例:

    http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/Regards,

    Will


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年10月8日 1:37
    版主