【发布时间】:2020-05-10 10:26:31
【问题描述】:
我有一个 azure 函数,它生成一个 sas 密钥,我稍后会使用该密钥将文件上传到我的 blob。这是我生成 sas 密钥的方式:
CloudBlobContainer container = blobClient.GetContainerReference("sasimagecontainer");
container.CreateIfNotExists();
static string GetContainerSasUri(CloudBlobContainer container)
{
//Set the expiry time and permissions for the container.
//In this case no start time is specified, so the shared access signature becomes valid immediately.
SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5);
sasConstraints.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(25);
sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Add | SharedAccessBlobPermissions.Create;
//Generate the shared access signature on the container, setting the constraints directly on the signature.
string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);
//Return the URI string for the container, including the SAS token.
return container.Uri + sasContainerToken + "&comp=list&restype=container";
}
然后我就可以打电话了
GetContainerSasUri(容器));
获取 sas 密钥。但是当我使用下面的代码来调用它时:
CloudBlockBlob blob = new CloudBlockBlob(thesaskey);
using (var fileStream = File.OpenRead(file))
{
await blob.UploadFromStreamAsync(fileStream);
}
我不断收到 403 错误。这就是我在 VS 2017 中调试所获得的全部信息,我不确定如何获取有关此问题的更多信息。
我已经在 SO 上查看了关于类似问题的大多数类似线程并应用了可能的修复,例如末尾的“&comp=list&restype=container”参数。
// 我尝试将 SharedAccessBlobPermissions.List 添加到权限中,但没有成功。
// 我确实添加了所有权限(当然,无权限除外)以检查这是否会改变某些东西 - 它不会。目标仍然是只有上传权限。
【问题讨论】:
-
您可能需要在客户端包含
x-ms-blob-type标头。我认为查询参数 (comp) 仅适用于阻止列表上传。我不确定这是否适用于容器级 URL。 -
您能详细说明一下吗?我不太确定如何添加标题。我可能错了,但我认为 UploadFromStreamAsync 自己处理请求,我无权修改其标头。
-
Docs 您必须将其添加到客户端发出的请求中。正如我所说,我只知道这对于实际的 blob 是正确的。我不确定容器。
-
您检查过返回的 SAS 吗?它有效吗?您也可以尝试添加
SharedAccessBlobPermissions.List看看是否有任何改变 -
我认为除了像我提出的那样发出上传请求之外,我还没有其他方法可以检查 sas 密钥。如果还有什么我可以使用的,请告诉我。
标签: c# azure azure-storage azure-blob-storage