malai.kuangren
malai.kuangren

Reputation: 359

How to use BlockBlob when uploading large file in Windows Azure web site

All, I was trying to uploading a larger file (700MB) in a web page which include a File html element in a Form . and I want to in the server side can get the stream and upload it into Azure storage. Here is some code snippet on the server side.

public void UploadFile()
{
    Stream source = Request.InputStream; 
    
    long copiedByteCount = 0;

    byte[] buffer = new byte[2 * 1024];
    
    for (int len; (len = from.Read(buffer, 0, buffer.Length)) > 0; )
    {
        //Begin to write buffer to Azure.
        ....(I am still searching the BlockBlob code sample in google.)
        //
        copiedByteCount += len;
    }
   
}

My question is

1.I hope the app does not eat all the memory ,so read the stream by block. I don't know if it work as my wish.

2.Can someone help to give some example How to write all the buffer blocks into BlockBlob in parallel.

Thanks.

Upvotes: 2

Views: 2103

Answers (1)

Sandrino Di Mattia
Sandrino Di Mattia

Reputation: 24895

First you have to know that, to allow 700MB uploads, you'll need to allow this in the web.config:

<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="716800" />
</system.web>

<system.webServer> 
    <security> 
        <requestFiltering>
            <requestLimits maxAllowedContentLength="716800" /> 
        </requestFiltering> 
    </security> 
</system.webServer>

Whenever someone uploads a file larger than 256KB (this is the default), the file is buffered to disk (documented here). So you don't have to worry about the incoming file.

To upload this file to a blob without loading it in memory, you can simply use the stream of the HttpPostedFile and forward it to the storage client:

 var blob = container.GetBlockBlobReference(blobName);
 blob.UploadFromStream(Request.Files[0].InputStream);

Finally, if you want to upload the blob in parallel you can start with the code on this page: Blob Parallel Upload and Download (you will need to change the UploadFileToBlobAsync method to accept a stream instead of a filename).

Upvotes: 2

Related Questions