关于BlobRequest.PutBlock方法

时间:2012-10-10 11:49:14

标签: azure blob

我想知道如何形成Blobrequest.PutBlock(Uri uri,int timeout,string blockid,string leaseid);

当用户尝试上传100 MB这样的大文件时,我会将它们分成内存为4MB的块(将4 MB数据读入byte [])。

如何使用BlobRequest.PutBlock和BlobRequest.PutBlockList将传入的文件流拆分为块并上传到blob 因为我有与blob相关的租约。如果我需要拆分文件并使用可用的Azure SDK 1.7.0上传具有租约ID的块,这是唯一的选择

此致 的Vivek

2 个答案:

答案 0 :(得分:1)

在调用PutBlock时,只需将leaseId作为最后一个参数传递:

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

如果您有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, ...)

答案 1 :(得分:0)

您可以在此栏目中找到此链接,用于研究将文件上传到azure blob:
http://wely-lau.net/2012/02/26/uploading-big-files-in-windows-azure-blob-storage-with-putlistblock/
我在这里复制代码以删除外部依赖项:

 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&gt; 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);            
}

您还可以找到Steve Marx为大文件上传开发的silverlight控件(例如)here.