2015-10-05 43 views

回答

0

複製文件的最簡單方法之一是使用AzCopy實用程序。

6

實現它的最簡單方法是使用"Azure Storage Data Movement Library"(你可以通過nuget包得到它)。

這是一個簡單的樣本,使傳輸:

using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Blob; 
using Microsoft.WindowsAzure.Storage.DataMovement; 
using System; 

namespace BlobClient 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      const string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=juanktest;AccountKey=loHQwke4lSEu1p2W3gg=="; 
      const string container1 = "juankcontainer"; 
      const string sourceBlobName = "test.txt"; 
      const string destBlobName = "newTest.txt"; 


      //Setup Account, blobclient and blobs 
      CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString); 
      CloudBlobClient blobClient = account.CreateCloudBlobClient(); 

      CloudBlobContainer blobContainer = blobClient.GetContainerReference(container1); 
      blobContainer.CreateIfNotExists(); 

      CloudBlockBlob sourceBlob = blobContainer.GetBlockBlobReference(sourceBlobName); 

      CloudBlockBlob destinationBlob = blobContainer.GetBlockBlobReference(destBlobName); 

      //Setup data transfer 
      TransferContext context = new TransferContext(); 
      Progress<TransferProgress> progress = new Progress<TransferProgress>(
       (transferProgress) => { 
         Console.WriteLine("Bytes uploaded: {0}", transferProgress.BytesTransferred); 
       }); 

      context.ProgressHandler = progress; 

      // Start the transfer 
      try 
      { 
       TransferManager.CopyAsync(sourceBlob, destinationBlob, 
        false /* isServiceCopy */, 
        null /* options */, context); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("The transfer is cancelled: {0}", e.Message); 
      } 

      Console.WriteLine("CloudBlob {0} is copied to {1} ====successfully====", 
          sourceBlob.Uri.ToString(), 
          destinationBlob.Uri.ToString()); 

      Console.ReadLine(); 
     } 
    } 
} 

注意,「Azure存儲數據移動圖書館」是非常強大的,所以你可以跟蹤傳輸進度,取消操作,甚至暫停它稍後恢復;)