c# - 在存储帐户之间复制 blob

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

我正在尝试将一个 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 确实存在。 我使用了错误的命令吗?关于存储帐户之间的复制,我是否遗漏了什么?

最佳答案

在研究了代码之后,我找到了答案。 只有当源 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);

希望它能对以后的人有所帮助。

关于c# - 在存储帐户之间复制 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36599777/

相关文章:

c# - 异步回发后 jquery 不工作

c# - e.data.GetData 始终为空

swift - 如何在 Azure Swift 中循环上传 blob?

react-native - 有没有人在 react native 项目中成功使用 azure-storage 进行 blob 存储?

c# - 如何在 system.array - C# 中保留 null?

c# - 如何将 JSON 数据接收到 Web API ApiController 方法中?

azure - 将实体保存到 Windows Azure 表存储中时 Windows Azure 的奇怪行为

azure - 如何查看 Windows Azure 表存储中每个表中有多少行?

c# - Azure 表查询异步 - 始终返回连续 token

angular - 如果 url 请求返回 404,它可能会返回默认图像吗?