skip to Main Content

My C# program downloads a list of files from an Azure Storage blob using Azure.StorageServices.BlobService. But once it reaches a particular mono-2.0-bdwgc.dll file, I get a Response failed with status: 403 Forbidden response.

This file is the first inside of a subfolder (or whaterver the equivalent would be for a blob). If I add another file with a lower alphanumerical name (alpha.txt), now alpha.txt becomes the one that returns 403 Forbidden. Maybe something to do with subfolders? But some of the files downloaded before this one are under other folders.

All files can be accessed anonymously. In fact, I can even open the [...]/mono-2.0-bdwgc.dll or [...]/alpha.txt URIs in a browser and it downloads them with no issue. My resource paths always use forward slashes / as separators.

If explicitly try to download the file at the start of the program (before I start downloading the list of blobs), it also downloads it just fine. It only seems to complain if I try while downloading the whole list of blobs.

A barebones excerpt of my code:

StorageServiceClient client = StorageServiceClient.Create(STORAGE_ACCOUNT, blobKey);
blobService = client.GetBlobService();

...

for (int i = 0; i < blobsToDownload.Count; i++)
{
    await blobService.GetBlob(OnBlobReceived, blobsToDownload[i]);
}
private async void OnBlobReceived(IRestResponse<byte[]> response)
{
    if (response.IsError)
    {
        // This fails with 403 Forbidden
        throw new Exception($"{(int)response.StatusCode} {response.ErrorMessage} {response.Url}");
    }
    ...
}

I’ve noticed Microsoft recently changed the recommended x-ms-version header on MSDN to 2025-01-05, so I followed suit, but nothing changed.

Does anyone know why it would fail on this particular file on this particular occasion?


Update

I ended up removing the Azure.StorageServices.BlobService 12.23.0 package I was using and switched to the official Azure.Storage.Blobs (via NuGet for Unity) instead. After a quick implementation the problem seems to have gone away. I’m not sure what was causing it, as the same code was working less than a week ago, but at least it’s fixed now.

2

Answers


  1. Chosen as BEST ANSWER

    I ended up removing the Azure.StorageServices.BlobService 12.23.0 package I was using and switched to the official Azure.Storage.Blobs (via NuGet for Unity) instead. After a quick implementation the problem seems to have gone away. I'm not sure what was causing it, as the same code was working less than a week ago, but at least it's fixed now.


  2. I could download the specified blobs including mono-2.0-bdwgc.dll using below code in Console application:

    public class AzureBlobDownloader
    {
        private const string STORAGE_ACCOUNT = "your_storage_account";
        private const string BLOB_KEY = "Storage_Key";
        private static readonly string blobServiceEndpoint = "https://your_storage_account.blob.core.windows.net/";
        private static readonly string CONTAINER= "Container_Name"
        private BlobServiceClient blobServiceClient;
    
        public AzureBlobDownloader()
        {
            var credential = new StorageSharedKeyCredential(STORAGE_ACCOUNT, BLOB_KEY);
            blobServiceClient = new BlobServiceClient(new Uri(blobServiceEndpoint), credential);
        }
    
        public async Task DownloadBlobsAsync(List<string> blobsToDownload)
        {
            int maxConcurrentDownloads = 3;
            SemaphoreSlim semaphore = new SemaphoreSlim(maxConcurrentDownloads);
    
            var tasks = new List<Task>();
    
            foreach (var blobName in blobsToDownload)
            {
                await semaphore.WaitAsync();
                tasks.Add(Task.Run(async () =>
                {
                    try
                    {
                        await TryDownloadBlobAsync(blobName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error downloading {blobName}: {ex.Message}");
                    }
                    finally
                    {
                        semaphore.Release();
                    }
                }));
            }
            await Task.WhenAll(tasks);
        }
    
        private async Task TryDownloadBlobAsync(string blobName, int maxRetries = 3)
        {
            int attempt = 0;
            while (attempt < maxRetries)
            {
                try
                {
                    await DownloadBlobAsync(blobName);
                    return; 
                }
                catch (Exception ex) when (attempt < maxRetries)
                {
                    attempt++;
                    Console.WriteLine($"Attempt {attempt} failed for {blobName}: {ex.Message}");
                    if (attempt < maxRetries)
                    {
                        await Task.Delay(2000); 
                    }
                    else
                    {
                        Console.WriteLine($"Max retries reached for {blobName}. Skipping...");
                    }
                }
            }
        }
    
        private async Task DownloadBlobAsync(string blobName)
        {
            var containerClient = blobServiceClient.GetBlobContainerClient(CONTAINER);
            var blobClient = containerClient.GetBlobClient(blobName);
    
            var localFilePath = Path.Combine("./", blobName);
    
            Console.WriteLine($"Starting download for {blobName}...");
    
            var blobDownloadInfo = await blobClient.DownloadAsync();
            using (var fileStream = File.OpenWrite(localFilePath))
            {
                await blobDownloadInfo.Value.Content.CopyToAsync(fileStream);
            }
            Console.WriteLine($"Downloaded {blobName} to {localFilePath}");
        }
    }
    
    class Program
    {
        static async Task Main(string[] args)
        {
            var blobsToDownload = new List<string>
            {
                "file1.txt",
                "file2.txt",
                "mono-2.0-bdwgc.dll",
                "file3.txt"
            };
            var downloader = new AzureBlobDownloader();
            await downloader.DownloadBlobsAsync(blobsToDownload);
        }
    }
    

    Blobs available in Azure Storage Container:

    enter image description here

    Able to download the files locally:

    enter image description here

    Downloaded files available in the project’s bin folder:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search