gpt4 book ai didi

azure - 使用 Azure.Storage.Blob v12.9.0 上传到 Azure Blob 存储时身份验证失败

转载 作者:行者123 更新时间:2023-12-03 02:19:34 24 4
gpt4 key购买 nike

尝试将文件上传到 Blob 存储时出现此错误。当我在本地主机上运行和在 Azure Function 中运行时,都会出现该错误。

我的连接字符串如下所示:DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net

未以正确的格式提供身份验证信息。检查授权 header 的值。时间:2021-10-14T15:56:26.7659660Z状态:400(未以正确的格式给出身份验证信息。检查授权 header 的值。)错误代码:InvalidAuthenticationInfo

但这在过去曾经有效,但最近它开始为我创建的新存储帐户抛出此错误。我的代码如下所示

    public AzureStorageService(IOptions<AzureStorageSettings> options)
{
_connectionString = options.Value.ConnectionString;
_containerName = options.Value.ImageContainer;
_sasCredential = new StorageSharedKeyCredential(options.Value.AccountName, options.Value.Key);
_blobServiceClient = new BlobServiceClient(new BlobServiceClient(_connectionString).Uri, _sasCredential);
_containerClient = _blobServiceClient.GetBlobContainerClient(_containerName);
}
public async Task<string> UploadFileAsync(IFormFile file, string location, bool publicAccess = true)
{
try
{

await _containerClient.CreateIfNotExistsAsync(publicAccess
? PublicAccessType.Blob
: PublicAccessType.None);

var blobClient = _containerClient.GetBlobClient(location);

await using var fileStream = file.OpenReadStream();

// throws Exception here
await blobClient.UploadAsync(fileStream, true);

return blobClient.Uri.ToString();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
// To be able to do this, I have to create the container client via the blobService client which was created along with the SharedStorageKeyCredential
public Uri GetSasContainerUri()
{
if (_containerClient.CanGenerateSasUri)
{
// Create a SAS token that's valid for one hour.
var sasBuilder = new BlobSasBuilder()
{
BlobContainerName = _containerClient.Name,
Resource = "c"
};

sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
sasBuilder.SetPermissions(BlobContainerSasPermissions.Write);

var sasUri = _containerClient.GenerateSasUri(sasBuilder);
Console.WriteLine("SAS URI for blob container is: {0}", sasUri);
Console.WriteLine();

return sasUri;
}
else
{
Console.WriteLine(@"BlobContainerClient must be authorized with Shared Key
credentials to create a service SAS.");
return null;
}
}

最佳答案

请更改以下代码行:

_blobServiceClient = new BlobServiceClient(new BlobServiceClient(_connectionString).Uri, _sasCredential);

_blobServiceClient = new BlobServiceClient(_connectionString);

考虑到您的连接字符串具有所有必要的信息,您实际上不需要做所有正在做的事情,您将使用 BlobServiceClient(String)构造函数需要并接受连接字符串。

您还可以删除以下代码行:

_sasCredential = new StorageSharedKeyCredential(options.Value.AccountName, options.Value.Key);

并且可能可以从您的配置设置中删除 AccountNameKey(如果它们不在其他地方使用)。

关于azure - 使用 Azure.Storage.Blob v12.9.0 上传到 Azure Blob 存储时身份验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69574018/

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