【问题标题】:Copy blob between storage accounts在存储帐户之间复制 blob
【发布时间】:2016-08-04 15:07:51
【问题描述】:

我正在尝试将 blob 从一个存储帐户复制到另一个(在不同的位置)。

我正在使用以下代码:

var sourceContainer = sourceClient.GetContainerReference(containerId);
var sourceBlob = sourceContainer.GetBlockBlobReference(blobId);
if (await sourceBlob.ExistsAsync().ConfigureAwait(false))
{
    var targetContainer = targetClient.GetContainerReference(containerId);
    await targetContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
    var targetBlob = targetContainer.GetBlockBlobReference(blobId);
    await targetBlob.DeleteIfExistsAsync().ConfigureAwait(false);
    await targetBlob.StartCopyAsync(sourceBlob).ConfigureAwait(false);
}

我收到“未找到”错误。 我确实知道源 blob 确实存在。 我使用了错误的命令吗?关于在存储帐户之间进行复制,我有什么遗漏吗?

【问题讨论】:

  • 请提供完整的错误信息
  • 远程服务器返回错误:(404) Not Found。 --- 指定的资源不存在。

标签: c# azure-storage azure-blob-storage


【解决方案1】:

在玩弄了代码之后,我得到了答案。 只有当源 blob 是 uri 而不是 blob 引用时,才能在存储帐户之间进行复制。 以下代码有效:

var sourceContainer = sourceClient.GetContainerReference(containerId);
var sourceBlob = sourceContainer.GetBlockBlobReference(blobId);
// Create a policy for reading the blob.
var policy = new SharedAccessBlobPolicy
{
    Permissions = SharedAccessBlobPermissions.Read,
    SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
    SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7)
};
// Get SAS of that policy.
var sourceBlobToken = sourceBlob.GetSharedAccessSignature(policy);
// Make a full uri with the sas for the blob.
var sourceBlobSAS = string.Format("{0}{1}", sourceBlob.Uri, sourceBlobToken);
var targetContainer = targetClient.GetContainerReference(containerId);
await targetContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
var targetBlob = targetContainer.GetBlockBlobReference(blobId);
await targetBlob.DeleteIfExistsAsync().ConfigureAwait(false);
await targetBlob.StartCopyAsync(new Uri(sourceBlobSAS)).ConfigureAwait(false);

希望将来能对某人有所帮助。

【讨论】:

  • 另一种选择是使用适用于 .NET 的 Azure 存储数据移动库。这是 AzCopy 的基础库。在此处查看公告:azure.microsoft.com/en-us/blog/… 这里还有一个有用的示例应用程序:github.com/Azure/azure-storage-net-data-movement/tree/master/…(如果您不熟悉 AzCopy,请参阅此处:azure.microsoft.com/en-us/documentation/articles/…
  • @TamraMyers-Microsoft 是否比使用标准 Microsoft.WindowsAzure.Storage 库更高效?还是它们都是合法的?
  • 对于代码中使用的 StartCopyAsync,DataMovement 库效率不高,因为它是服务器端副本。但是,对于 blob 之间的下载、上传或同步复制(下载到内存然后上传), AzCopy 和 DataMovement 库比 Microsoft.WindowsAzure.Storage 库效率更高,因为它在多个线程中进行传输。
【解决方案2】:

您还可以使用流在存储帐户之间复制 blob:

var sourceContainer = sourceClient.GetContainerReference(sourceContainer);
var sourceBlob = sourceContainer.GetBlockBlobReference(sourceBlobId);
var targetContainer = targetClient.GetContainerReference(destContainer);
var targetBlob = targetContainer.GetBlockBlobReference(destBlobId);

using (var targetBlobStream = await targetBlob.OpenWriteAsync())
{
    using (var sourceBlobStream = await sourceBlob.OpenReadAsync())
    {
        await sourceBlobStream.CopyToAsync(targetBlobStream);
    }
}

【讨论】:

  • 这是一个非常干净的解决方案。为我工作。
猜你喜欢
  • 1970-01-01
  • 2019-08-01
  • 2022-01-06
  • 2016-05-28
  • 1970-01-01
  • 2021-08-25
  • 2021-07-27
  • 2018-07-13
  • 2022-01-11
相关资源
最近更新 更多