在存储帐户之间复制blob

shl*_*chz 6 c# azure-storage azure-storage-blobs

我正在尝试将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);
}
Run Code Online (Sandbox Code Playgroud)

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

shl*_*chz 10

在玩完代码之后,我找到了答案.只有当源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);
Run Code Online (Sandbox Code Playgroud)

希望它能帮助将来的某个人.

  • 对于代码中使用的StartCopyAsync,DataMovement Library的效率不高,因为它是服务器端副本.但是,要在blob之间下载,上传或同步副本(下载到内存然后上传),AzCopy和DataMovement Library效率更高比Microsoft.WindowsAzure.Storage库,因为它在多个线程中进行传输. (3认同)
  • 另一种选择是使用Azure存储数据移动库for .NET.这是AzCopy的基础库.看看这里的公告:https://azure.microsoft.com/en-us/blog/announcing-azure-storage-data-movement-library-0-2-0/这里还有一个有用的示例应用程序: https://github.com/Azure/azure-storage-net-data-movement/tree/master/samples/DataMovementSamples(如果您不熟悉AzCopy,请访问:https://azure.microsoft.com/ EN-US /文档/文章/存储使用-azcopy /) (2认同)
  • @ TamraMyers-Microsoft它比使用标准的Microsoft.WindowsAzure.Storage库更有效吗?或者他们都合法吗? (2认同)

Mig*_*ruz 5

您还可以使用流在存储帐户之间复制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);
    }
}
Run Code Online (Sandbox Code Playgroud)