当前位置: 首页 > 知识库问答 >
问题:

使用Azure上载到Azure Blob存储时身份验证失败。存储Blob v12.9.0

吴均
2023-03-14

我在尝试将文件上载到blob存储时遇到此错误。在本地主机上运行和在Azure函数中运行时都会出现错误。

我的连接字符串如下所示:DefaultEndpoint sProtocol=https; AcCountName=xxx; AcCountKey=xxx; Endpoint Suffix=core.windows.net

身份验证信息的格式不正确。检查授权标头的值。时间:2021 10月14日15:56:26.7659660Z状态:400(身份验证信息的格式不正确。请检查授权标头的值。)错误代码: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;
        }
    }

共有1个答案

廖弘量
2023-03-14

请更改以下代码行:

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

_blobServiceClient = new BlobServiceClient(_connectionString);

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

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

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

如果在其他地方没有使用,则可以从您的配置设置中删除AccountName和Key。

 类似资料:
  • 我们正在配置spring云配置服务器,下面是我的应用程序属性的外观: 我可以登录到github url,即https://github.com/myorganization/config-store.git使用用户名MyUser和密码MyPassword作为上述属性,但当我试图启动我的配置服务器使用:./mvnw sping-boot: run我得到以下错误: 我已经尝试了很多东西,但现在没有任何

  • 我试图使用系统分配的应用程序服务标识从应用程序服务(Web API)向Azure密钥库进行身份验证。在Azure Key Vault中,我创建了一个an access策略,该策略允许应用程序服务访问密钥、机密和证书(稍后将对此进行限制!)。 请注意,使用服务主体对同一个密钥库进行身份验证没有问题,但是使用托管标识的想法当然是为了避免在任何地方存储客户机ID和secret之类的凭据 异常消息:

  • 我尝试将安装程序部署到本地存储库: --设置。xml-- --波姆。xml--- 当我执行“mvn部署”时,我收到错误: 艺术家日志: 如果我将服务器/存储库id更改为“myserver.com”-部署工作!但这不适合我,因为在myserver上,这不适用于svn。通用域名格式 我试图将标签“配置文件”和“镜像”添加到server.xml和“分发管理”添加到pom.xml-得到相同的错误 服务器/

  • 我在scala中使用JGit访问远程Git repo,我需要使用SSH进行身份验证。JGit使用JSch提供安全访问。 我遵循了本教程:http://www.codeaffine.com/2014/12/09/jgit-authentication/ 然而,我一直,安,com。jcraft。jsch。JSchException:Auth失败 我的方法是: 但我一直都有这样的例外:

  • 我已经实现了keycloak用户存储SPI流。它针对外部源进行身份验证。Keycloak是用外部存储属性更新的,如果它在getUserByUsername方法中更新,它就可以工作。 我正在isValid方法中验证userValidation,并更新UserModel和LocalHashMap中的userAttributes。 从日志中,我看到UserStoreProviderFactory创建了两

  • 问题内容: 使用Couchbase 5.0及其Java客户端2.0.3时,出现以下错误。 只需按照以下说明打开存储桶即可: https://developer.couchbase.com/documentation/server/current/sdk/java/managing- connections.html 如前所述,使用基本的本地配置,只需两行代码即可: 那应该打开localhost集群