gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:56:16 25 4
gpt4 key购买 nike

我正在尝试将一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com