gpt4 book ai didi

c# - Azure 将 blob 复制到不同的高级存储帐户

转载 作者:行者123 更新时间:2023-12-03 04:29:12 28 4
gpt4 key购买 nike

我正在尝试将一个 blob(特别是 vhd)从一个存储帐户复制到另一个存储帐户。我正在编写的应用程序具有对两个存储帐户的所有者访问权限,并且可以在这些存储帐户上运行其他操作。 vhd 上没有租约(未附加 VM)。我有以下代码,但收到 403 禁止响应。

StorageCredentials scSource = new StorageCredentials(SourceStorageName, strSourceStorageKey);
StorageCredentials scTarget = new StorageCredentials(TargetStorageName, strTargetStorageKey);
CloudStorageAccount csaSource = new CloudStorageAccount(scSource, true);
CloudStorageAccount csaTarget = new CloudStorageAccount(scTarget, true);
CloudBlobClient cbcSource = csaSource.CreateCloudBlobClient();
CloudBlobClient cbcTarget = csaTarget.CreateCloudBlobClient();
CloudBlobContainer bcSource = cbcSource.GetContainerReference(SourceContainer);
CloudBlobContainer bcTarget = cbcTarget.GetContainerReference(TargetContainer);
CloudBlob cbSource = bcSource.GetBlobReference(strSourceDiskName);
CloudBlob cbTarget = bcTarget.GetBlobReference(strTargetDiskName);
Task<string> tskCopy = cbTarget.StartCopyAsync(cbSource.Uri);
while (tskCopy.Status != TaskStatus.RanToCompletion)
{
if (tskCopy.Exception != null)
throw tskCopy.Exception;
Thread.Sleep(2500);
}

不知道为什么会发生这种情况,因为我可以通过其他工具(cloud berry 等)将 blob 从一个存储帐户复制到另一个存储帐户。

最佳答案

感谢 Gaurav Mantri 为我指明了正确的方向。查找共享访问签名后,我发现了 Microsoft https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-shared-access-signature-part-2/ 的这篇文章

具体代码:

SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24);
sasConstraints.Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write;

// Generate the shared access signature on the blob, setting the constraints directly on the signature.
string sasBlobToken = blob.GetSharedAccessSignature(sasConstraints);

// Return the URI string for the container, including the SAS token.
return blob.Uri + sasBlobToken;

关于c# - Azure 将 blob 复制到不同的高级存储帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176446/

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