在存储帐户之间复制Blob [英] Copy blob between storage accounts

查看:46
本文介绍了在存储帐户之间复制Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个存储帐户中的Blob复制到另一个存储帐户中(位于其他位置).

I'm trying to copy a blob from one storage account to another (In a different location).

我正在使用以下代码:

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);
}

,出现未找到"错误. 我确实知道源斑点确实存在. 我使用了错误的命令吗?关于在存储帐户之间进行复制,我缺少什么吗?

and I get a "Not Found" error. I do get that the source blob indeed exists. Am I using the wrong command? Is there something I'm missing regarding copying between storage accounts?

推荐答案

在玩完代码后,我找到了答案. 仅当源Blob是uri而不是Blob引用时,才可以在存储帐户之间进行复制. 以下代码有效:

After playing around with the code, I reached an answer. Copying between storage accounts can only be achieved when the source blob is a uri and not a blob reference. The following code worked:

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);

希望它将对以后的人有所帮助.

Hope it will help someone in the future.

这篇关于在存储帐户之间复制Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆