Azure blob从一个存储到另一个存储的并行副本

时间:2015-07-14 03:22:43

标签: azure azure-storage-blobs

我目前正在学习blob块存储。我想将块blob从一个存储复制到另一个存储帐户。通常,当从系统上传到云时,我看到的例子是计算块大小,然后使用PutBlock和PutBlockList。我想使用相同的方法从一个存储帐户复制到另一个存储帐户。使用DownloadBlockList我能够获得blockid,但是我无法获得与块id相关的数据。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse( ConfigurationManager.AppSettings["StorageConnectionString"]);

            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = cloudBlobClient.GetContainerReference("input");

            CloudBlockBlob blob = container.GetBlockBlobReference("Cloud.mp4");

            List<string> commitedBlocks = new List<string>();

            IEnumerable<ListBlockItem> blockItem = blob.DownloadBlockList(BlockListingFilter.All);
            commitedBlocks.AddRange(blob.DownloadBlockList(BlockListingFilter.Committed).Select(id => id.Name));
            ); 

如果我能够获得与块id相关的数据,那么我可以对块进行并行复制。

由于

1 个答案:

答案 0 :(得分:1)

如果您的目标是将blob从一个存储帐户复制到另一个存储帐户,则不必执行所有这些操作:)。 Azure Storage API允许您执行服务器端异步复制操作。您只需向Azure存储服务发送请求,即将blob从一个存储帐户复制到另一个存储帐户,它将执行复制操作。由于它是异步操作,您可能希望跟踪操作状态,以便了解复制操作何时完成。

    private static void AsyncCopyExample()
    {
        var sourceAccount = new CloudStorageAccount(new StorageCredentials("source-account-name", "source-account-key"), true);
        var sourceContainer = sourceAccount.CreateCloudBlobClient().GetContainerReference("source-container-name");
        var sourceBlockBlob = sourceContainer.GetBlockBlobReference("source-blob-name");
        var targetAccount = new CloudStorageAccount(new StorageCredentials("target-account-name", "target-account-key"), true);
        var targetContainer = sourceAccount.CreateCloudBlobClient().GetContainerReference("target-container-name");
        var targetBlockBlob = sourceContainer.GetBlockBlobReference("source-blob-name");
        var copyId = targetBlockBlob.StartCopyFromBlob(sourceBlockBlob);//This will initiate the copy operation
        //Following code can be used to check if the copy has been completed.
        var isCopyOperationInProgress = true;
        do
        {
            targetBlockBlob.FetchAttributes();
            if (targetBlockBlob.CopyState.Status == CopyStatus.Pending)
            {
                Thread.Sleep(1000); //Sleep for a second and then check again
            }
            else
            {
                isCopyOperationInProgress = false;
            }
        } while (isCopyOperationInProgress);
    }

您可能还会从存储小组中发现这篇博文有用:http://blogs.msdn.com/b/windowsazurestorage/archive/2012/06/12/introducing-asynchronous-cross-account-copy-blob.aspx