Azure Resource Manager .NET SDK,复制VHD / Blob

时间:2017-04-04 00:17:53

标签: .net azure azure-storage-blobs vhd

我正在尝试找到一种方法将VHD复制到我的资源组中的存储帐户。

我有一个VHD的Sas Uri。

I Powershell我会用:

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

这样做。

我似乎无法从Azure Resource Manager的.NET SDK中找到一种方法。 https://github.com/Azure/azure-sdk-for-net/tree/AutoRest/src/ResourceManagement/

我有什么方法可以使用.NET复制blob吗?

1 个答案:

答案 0 :(得分: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);
    }