admin管理员组文章数量:1323716
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.
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
Reset to default 2I 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/";
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:
Able to download the files locally:
Downloaded files available in the project's bin folder:
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.
本文标签: cAnonymous blob download returns 403 ForbiddenStack Overflow
版权声明:本文标题:c# - Anonymous blob download returns 403 Forbidden - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126256a2421956.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
mono-2.0-bdwgc.dll
file? – Pravallika KV Commented Jan 13 at 10:50