1
votes

I would like to know how to form Blobrequest.PutBlock(Uri uri,int timeout,string blockid,string leaseid);

When user tries to upload large file like 100 MB,i will divide them into chunk each of 4MB in memory(reading 4 MB data into byte [])..

How split the incoming filestream into chunks and upload to blob using BlobRequest.PutBlock and BlobRequest.PutBlockList since i have lease associated with blob.This is the only option i guess if i need to split the file and upload chunks with lease id with available Azure SDK 1.7.0

Regards, Vivek

2

2 Answers

1
votes

Simply pass the leaseId as the last parameter when calling PutBlock:

public static HttpWebRequest PutBlock (
    Uri uri,
    int timeout,
    string blockId,
    string leaseId
)

The URL is pretty easy to build if you have a CloudBlob (see Steve's blog post for more information):

var creds = blob.ServiceClient.Credentials;
var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
BlobRequest.PutBlock(transformedUri, ...)
0
votes

You can find this link useful to study uploading files to azure blob in block:
http://wely-lau.net/2012/02/26/uploading-big-files-in-windows-azure-blob-storage-with-putlistblock/
I am copying the code here to remove external dependencies:

 protected void btnUpload_Click(object sender, EventArgs e)
{
    CloudBlobClient blobClient;
    var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
    blobClient = storageAccount.CreateCloudBlobClient();

    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    container.CreateIfNotExist();

    var permission = container.GetPermissions();
    permission.PublicAccess = BlobContainerPublicAccessType.Container;
    container.SetPermissions(permission);

    string name = fu.FileName;
    CloudBlockBlob blob = container.GetBlockBlobReference(name);

    blob.UploadFromStream(fu.FileContent);

    int maxSize = 1 * 1024 * 1024; // 4 MB

    if (fu.PostedFile.ContentLength > maxSize)
    {
        byte[] data = fu.FileBytes; 
        int id = 0;
        int byteslength = data.Length;
        int bytesread = 0;
        int index = 0;
        List<string> blocklist = new List<string>();
        int numBytesPerChunk = 250 * 1024; //250KB per block

        do
        {
            byte[] buffer = new byte[numBytesPerChunk];
            int limit = index + numBytesPerChunk;
            for (int loops = 0; index < limit; index++)
            {
                buffer[loops] = data[index];
                loops++;
            }
            bytesread = index;
            string blockIdBase64 = Convert.ToBase64String(System.BitConverter.GetBytes(id));

            using (var ms = new MemoryStream(buffer, true))
              blob.PutBlock(blockIdBase64, ms, null);

null); blocklist.Add(blockIdBase64); id++; } while (byteslength - bytesread > numBytesPerChunk);

        int final = byteslength - bytesread;
        byte[] finalbuffer = new byte[final];
        for (int loops = 0; index < byteslength; index++)
        {
            finalbuffer[loops] = data[index];
            loops++;
        }
        string blockId = Convert.ToBase64String(System.BitConverter.GetBytes(id));
        using (var ms = new MemoryStream(finalbuffer, true))
          blob.PutBlock(blockId, ms, null);
        blocklist.Add(blockId);

        blob.PutBlockList(blocklist); 
    }
    else
        blob.UploadFromStream(fu.FileContent);            
}

You can also find the silverlight control developed by Steve Marx for big file upload(for example) here.