0
votes

I get this error when trying to upload files to blob storage. The error is present both when I run on localhost and when I run in Azure Function.

My connection string looks like: DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx;EndpointSuffix=core.windows.net

Authentication information is not given in the correct format. Check the value of the Authorization header. Time:2021-10-14T15:56:26.7659660Z Status: 400 (Authentication information is not given in the correct format. Check the value of Authorization header.) ErrorCode: InvalidAuthenticationInfo

But this used to work in the past but recently its started throwing this error for a new storage account I created. My code looks like below

    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;
        }
    }
1
How does your connection string look like? Please edit your question and include that. Please make sure to obfuscate account name and key before this information.Gaurav Mantri
I faced the same issue few days ago... And instead of the usual connection string I had to provide a Shared Access Signature uri with the right authorizations as a connection string... Hope it works for you. I think azure enforced security on their storage accounts. (I also was using an Azure Function to upload files on a blob storage)Flo

1 Answers

0
votes

Please change the following line of code:

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

to

_blobServiceClient = new BlobServiceClient(_connectionString);

Considering your connection string has all the necessary information, you don't really need to do all the things you're doing and you will be using BlobServiceClient(String) constructor which expects and accepts the connection string.

You can also delete the following line of code:

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

and can probably get rid of AccountName and Key from your configuration settings if they are not used elsewhere.