【发布时间】:2021-08-28 15:18:56
【问题描述】:
我需要能够将 Blob 从 1 个存储帐户中的容器复制到另一个存储帐户中的容器中。以前,源容器的公共访问权限设置为“容器”。我使用连接字符串连接到帐户,然后获取对 blob 的引用。
我正在使用 StartCopyAsync(sourceBlob)。当容器将公共访问权限设置为容器时,这原本可以正常工作。现在它抛出“指定的资源不存在”的 StorageException。这是权限问题吗?我本来希望一条错误消息说我没有权限。我可以看到资源在容器中。
假设这是一个权限问题,有没有办法从公共访问设置为“私有”的容器中复制 blob?文档建议可以通过“授权请求”来完成,但你是怎么做的呢?
更新
我尝试了 Gaurav Mantri 的建议,但目前收到“此请求无权执行此操作”的错误。这是我的代码:
private async Task<FileActionResult> CopyBlob(string blobName, string sourceBlobSasToken, string sourceBlobName, CloudBlockBlob targetBlob)
{
try
{
await targetBlob.StartCopyAsync(new Uri(sourceBlobSasToken));
var targetBlobRef = await _connectionFactory.GetContainerConnection(BlobContainerType.myTargetContainer).GetBlobReferenceFromServerAsync(targetBlob.Name);
while (targetBlobRef.CopyState.Status == CopyStatus.Pending)
{
await Task.Delay(500);
targetBlobRef = await _connectionFactory.GetContainerConnection(BlobContainerType.myTargetContainer).GetBlobReferenceFromServerAsync(targetBlob.Name);
}
if (targetBlobRef.CopyState.Status == CopyStatus.Success)
{
return new FileActionResult(blobName, true);
}
else
{
var errorMessage = $"{blobName} failed to copy from '{sourceBlobName}' to '{targetBlob}'. Status: {targetBlobRef.CopyState.Status}";
_logServiceApi.Trace(errorMessage);
return new FileActionResult(blobName, false, errorMessage);
}
}
catch (StorageException ex)
{
_logServiceApi.LogException(ex);
return new FileActionResult(blobName, false, ex.Message);
}
catch (AggregateException ex)
{
_logServiceApi.LogException(ex);
return new FileActionResult(blobName, false, ex.Message);
}
}
'sourceBlobSasToken'是通过这段代码获取并传入的:
public string GetFileStorageBlobSasUri(BlobContainerType blobContainer, string fileName, double tokenDuration, long? projectId = null)
{
var blob = GetBlockBlob(blobContainer, fileName, projectId);
var adHocPolicy = new SharedAccessBlobPolicy()
{
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(tokenDuration),
Permissions = SharedAccessBlobPermissions.Read
};
var sasContainerToken = blob.GetSharedAccessSignature(adHocPolicy);
return blob.Uri + sasContainerToken;
}
【问题讨论】:
-
您的连接字符串是否有 AccountName 和 AccountKey?像这样: string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account, key);
-
@JoelCochran - 是的。我可以通过门户访问这两个帐户和所有容器,所以我刚刚获取了两者的连接字符串,并在请求中使用它们。
标签: c# azure azure-blob-storage