2012-10-10 54 views
1

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

當用戶嘗試上載大文件等100 MB,我將其劃分爲塊的每個4MB的內存(讀4點MB的數據爲字節[])..

如何傳入FILESTREAM分成塊,並上傳到blob使用BlobRequest.PutBlock和BlobRequest.PutBlockList ,因爲我有與blob.and這是唯一的選擇,我想如果我需要拆分文件和上載塊與租賃id與可用的Azure SDK 1.7.0

Regards, Vivek

回答

1

只需通過leaseId作爲最後一個參數調用PutBlock時:

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

的URL是很容易建立,如果你有一個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

Sandrino嗨,我沒有得到如何通過URI作爲BlolREquest.PutBlock史蒂夫·馬克思對大文件上傳(例如)開發的Silverlight控件。 –

+0

更新了我的答案。 –

+0

嗨Sandrino,上面提到的鏈接調用BlobRequest.Put中的代碼,其中用戶想要一次性上傳內容到blob。我正在尋找與BlobRequest.PutBlock塊上傳塊。 –

0

你可以找到這個鏈接的文件上傳學習到有用的蔚藍的二進制大對象塊:
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)); 

      blob.PutBlock(blockIdBase64, new MemoryStream(buffer, true), 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)); 
     blob.PutBlock(blockId, new MemoryStream(finalbuffer, true), null); 
     blocklist.Add(blockId); 

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

您還可以找到here.

+0

嗨Ruchit.With上面提到的代碼,我們不能傳遞與blob關聯的租約id。 –

+0

你可以從這個msdn主題找到它:http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storageclient.protocol.blobrequest.putblock –

+0

你可以像這樣傳遞租約id:'BlobRequest.PutBlock (uri,timeout,blockId,leaseId)' –