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

标签 c# azure blob azure-blob-storage

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

相关文章:

MYSQL BLOB 与带有搜索查询的 TEXT 字段

http - FileSaver.js 正在保存损坏的图像

sql - 如何将存储在 Azure Blob 存储中的图像文件插入 Azure SQL 表中

c# - 在 MVC3 中未授权页面时重定向用户

c# - 如何在 Worker Role 托管的 Web API 中获取客户端服务器的唯一标识符

c# - 如何使用设置变量为 NLog 配置 Azure 表连接字符串?

azure - 无效的部署模板参数(Bicep/Yaml)

python - 如何使用 Http 触发器和队列消息输出修复 Azure 函数 (Python) 测试错误

c# - 通过引用检索属性

c# - 访问文档属性 - VB 中的 Excel 工作簿/CSV