Microsoft.WindowsAzure.Storage (Azure Storage SDK for Windows) version : 9.3.2.0 : Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden.

Sangamesh S Gouri 6 Reputation points
2021-08-17T10:42:39.417+00:00

Hi,

I am using Microsoft.WindowsAzure.Storage (Azure Storage SDK for Windows) version : 9.3.2.0 for uploading the blob.

Here is logic of c# code for the same.

    private class FileBlock
    {
        internal string Id { get; set; }
        internal byte[] Content { get; set; }
    }

    public static async Task<Uri> UploadFileToAzureStorageAsync(string azureStorageUri, string filePath)
    {
        var bytes = File.ReadAllBytes(filePath);

        var cloudBlockBlob = new CloudBlockBlob(new Uri(azureStorageUri));

        var blocks = new HashSet<string>();            

        try
        {
            foreach (var block in GetFileBlocks(bytes))
            {
                cloudBlockBlob.PutBlock(block.Id, new MemoryStream(block.Content, true), null);
                blocks.Add(block.Id);
            }

            await cloudBlockBlob.PutBlockListAsync(blocks);
        }
        catch (Exception ex)
        {
            Logging.logger.Error(ex);                
        }

        return cloudBlockBlob.Uri;
    }

    private static IEnumerable<FileBlock> GetFileBlocks(byte[] fileContent)
    {
        if (fileContent.Length == 0)
            return new HashSet<FileBlock>();

        var maxBlockSize = 4 * 1024 * 1024;
        var hashSet = new HashSet<FileBlock>();
        var blockId = 0;
        var index = 0;
        var currentBlockSize = maxBlockSize;

        while (currentBlockSize == maxBlockSize)
        {
            if ((index + currentBlockSize) > fileContent.Length)
                currentBlockSize = fileContent.Length - index;

            var chunk = new byte[currentBlockSize];

            Array.Copy(fileContent, index, chunk, 0, currentBlockSize);

            hashSet.Add(new FileBlock
            {
                Content = chunk,
                Id = Convert.ToBase64String(BitConverter.GetBytes(blockId))
            });

            index += currentBlockSize;
            blockId++;
        }

        return hashSet;
    }

I am seeing this issue intermittently for few packages. If i publish again it will be successful. I read few blogs which suggest to change machine to UTC time zone. I tried that as well but still the same error occurs as shown below.
I wanted to know if there is issue for the below exception.

Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden.
---> System.Net.WebException: The remote server returned an error: (403) Forbidden.

StatusMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
ErrorCode:AuthenticationFailed
ErrorMessage:Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

Thanks,
Sangamesh

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,639 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. deherman-MSFT 35,636 Reputation points Microsoft Employee
    2021-08-17T18:56:54.47+00:00

    @Sangamesh S Gouri
    Azure Storage SDK for Windows version : 9.3.2.0 has been deprecated. We recommend moving to our latest SDK version for the latest features and fixes. More information can be found on the page located here. We recommend you try this using the latest SDK and letting us know if you are still facing issues.

    If you are unable to try using the latest SDK you might try posting your code to StackOverflow for our developer community to review.

    -------------------------------

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

  2. Sangamesh S Gouri 6 Reputation points
    2021-08-18T05:14:45.023+00:00

    Hi @deherman-MSFT ,

    Thanks for the quick reply. We have plans to migrate to latest version of the Azure storage SDK next release. Currently we are shipping this version of Azure Storage SDK. I will also post my code in the stackoverflow for developer community.

    Thanks,
    Sangamesh

    0 comments No comments