我在 .NET 应用程序中使用 Azure.Storage.Blobs 库。当我使用此代码上传 Blob 时
string fileName = $"{DateTime.Now:yyyyMMdd_HHmmss_fff}.json";
string json = JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true });
var options = new BlobUploadOptions
{
Tags = new Dictionary<string, string>
{
{ "Status", isFailed ? "Failed" : "Success" },
{ "DeviceId", queueItem.DeviceId.ToString() },
}
};
var blobClient = _blobContainerClient.GetBlobClient(fileName);
await blobClient.UploadAsync(BinaryData.FromString(json), options);
//await _blobContainerClient.UploadBlobAsync(fileName, BinaryData.FromString(json));
我得到这个例外:
Result: Failure Exception: Azure.RequestFailedException: This request is not authorized to perform this operation using this permission.
RequestId:21a733b3-201e-00a4-7899-75df76000000 Time:2024-03-13T22:57:46.6502306Z Status: 403 (This request is not authorized to perform this operation using this permission.)
ErrorCode: AuthorizationPermissionMismatch Content: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthorizationPermissionMismatch</Code><Message>This request is not authorized to perform this operation using this permission.
但是如果我使用上面注释掉的代码,即发布不带标签的blob,它就可以正常工作。
它在 Azure FunctionApp 中运行,我已为服务主体授予“存储 Blob 数据贡献者”角色。
内置角色“存储 Blob 数据贡献者”没有上传带有标签的 Blob 的正确权限。
您可以授予主体“存储 Blob 数据所有者”或创建自定义角色并为
Microsoft.Storage/storageAccounts/blobServices/containers/blobs/tags/write
Azure 资源提供程序操作 添加正确的权限
此任务可以由存储 Blob 数据所有者或已通过自定义 Azure 角色获得 Microsoft.Storage/storageAccounts/blobServices/containers/blob/tags/write Azure 资源提供程序操作权限的安全主体执行。