Home > Software design >  Upgrading Azure Blob API 11->12, leading slash in blob name cannot be found in 12
Upgrading Azure Blob API 11->12, leading slash in blob name cannot be found in 12

Time:03-02

My project was built long ago using Microsoft.Azure.Storage.Blob and I have many containers with many files. I am not sure how it happened but all of my folder structures have a leading slash in them. This has not been an issue until I try to upgrade my code to Azure.Blobs (v12).
When I look at my blobs in the Azure Portal, it shows a blank / in the root folder, and browsing down I see it looks like this [container] / / [first folder] / [second folder] / [filename]

Azure portal has not problem showing a file and downloading it. When I look at properties, the URL looks like https://[account].blob.core.windows.net/[container]//folder1/folder2/file.ext

After updating my code, I find that container.GetBlobClient([folder filename]) will not retrieve any files. It always gets a 404. When I look in debugger to see what URL it is trying to access, it is eliminating the double slash so it looks like https://[account].blob.core.windows.net/[container]/folder1/folder2/file.ext

I have tried prepending a slash to the [folder filename] but it always strips it out. Ditto for prepending two slashes.

I have been googling all day and cannot find an answer here. Is there a workaround to this? I am thinking there has to be because both the Azure Portal and Cloudberry Explorer can access and download my blobs.

CodePudding user response:

One possible workaround is to create an instance of BlobClient using the full URL of the blob. That way the leading slashes are preserved.

Please take a look at the code below. It makes use of Azure.Storage.Blobs version 12.10.0.

using System;
using System.Threading.Tasks;
using Azure.Storage;
using Azure.Storage.Blobs;

namespace SO71302870
{
    class Program
    {
        private const string accountName = "account-name";

        private const string accountKey = "account-key";

        private const string containerName = "container-name";
        private const string blobName = "/000/1.txt";//notice the leading slash (/) here.
        static async Task Main(string[] args)
        {
            StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
            BlobClient blobClient =
                new BlobClient(new Uri($"https://{accountName}.blob.core.windows.net/{containerName}/{blobName}"), credential);
            var properties = await blobClient.GetPropertiesAsync();
            Console.WriteLine(properties.Value.ContentLength);
        }
        
    }
}
  • Related