2017-04-04 31 views
0

我想找到一種方法將VHD複製到我的資源組中的存儲帳戶。Azure資源管理器.NET SDK,複製VHDs/Blobs

我有一個VHD的Sas Uri。

我PowerShell的,我會用:

Start-AzureStorageBlobCopy ` 
-AbsoluteUri $sas.AccessSAS ` 
-DestContainer 'vhds' -DestContext $destContext -DestBlob 'MyDestinationBlobName.vhd' 

做到這一點。

我似乎無法找到一種方法來從Azure資源管理器的.NET SDK中執行此操作。 https://github.com/Azure/azure-sdk-for-net/tree/AutoRest/src/ResourceManagement/

有沒有什麼辦法可以使用.NET複製blob?

回答

1

有什麼辦法可以使用.NET複製blob?

您將需要使用Azure Storage SDK for .NetGithub | Nuget)。

using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Auth; 
using Microsoft.WindowsAzure.Storage.Blob; 

    static void CopyBlobUsingSasExample() 
    { 
     var destinationAccountName = ""; 
     var destinationAccountKey = ""; 
     var destinationAccount = new CloudStorageAccount(new StorageCredentials(destinationAccountName, destinationAccountKey), true); 
     var destinationContainerName = "vhds"; 
     var destinationContainer = destinationAccount.CreateCloudBlobClient().GetContainerReference(destinationContainerName); 
     destinationContainer.CreateIfNotExists(); 
     var destinationBlob = destinationContainer.GetPageBlobReference("MyDestinationBlobName.vhd"); 
     var sourceBlobSasUri = ""; 
     destinationBlob.StartCopy(new Uri(sourceBlobSasUri)); 
     //Since copy operation is async, please wait for the copy operation to finish. 
     do 
     { 
      destinationBlob.FetchAttributes(); 
      var copyStatus = destinationBlob.CopyState.Status; 
      if (copyStatus != CopyStatus.Pending) 
      { 
       break; 
      } 
      else 
      { 
       System.Threading.Thread.Sleep(5000);//Sleep for 5 seconds and then fetch attributes to check the copy status. 
      } 
     } while (true); 
    }