1

We have had an existing implementation with Azure using the depreciated WindowsAzure.Storage nuget for a few years.

We are upgrading the nuget to the new Azure.Storage.Blobs nuget.

As part of the original implementation, the blobs were to be stored in a folder structure within the container: container/year/month/:

  • test/2021/01/test.pdf
  • test/2020/12/test.pdf

The requirement is that this should be continued going forward, but I cannot workout if it is possible anymore.

Has anyone managed to get this to work?

2
  • Not sure what you mean. Folders are virtual. The blob name defines them. Commented Jan 5, 2021 at 17:03
  • That is correct and in the old nuget it worked fine. However, in the latest version, it no longer works as the url within the blob gets url encoded and therefore loses the virtual structure when creating the file. Commented Jan 21, 2021 at 11:46

3 Answers 3

1

In Azure's Blob storage, you have nothing called the folder structure. It is all virtual. If you specify the blob's name with the full path which you wish it to be (year/month in your case) in the blob reference part of your code, its possible to retain the same logic of uploading blobs.

Sign up to request clarification or add additional context in comments.

Comments

0

You can simply create it through code:

using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

// Get a connection string to our Azure Storage account.  You can
// obtain your connection string from the Azure Portal (click
// Access Keys under Settings in the Portal Storage account blade)
// or using the Azure CLI with:
//
//     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
//
// And you can provide the connection string to your application
// using an environment variable.
string connectionString = "<connection_string>";

// Get a reference to a container named "sample-container" and then create it
BlobContainerClient container = new BlobContainerClient(connectionString, "sample-container");
container.Create();

// Get a reference to a blob named "sample-file" in a container named "sample-container"
BlobClient blob = container.GetBlobClient("sample-folder/sample-file");

// Open a file and upload it's data
using (FileStream file = File.OpenRead("local-file.jpg"))
{
    blob.Upload(file);
}

Check out documentation from PG about this nuget.

5 Comments

Does this answer help you?
Hello, sadly not. This is using the old nuget package that has been depreciated and was pretty much how we did it before. nuget.org/packages/Azure.Storage.Blobs/12.0.0 is the newer version.
It has same approach. Pls check my updated response.
Does it help you?
Thanks for the update. Running the code above gives me similar behaviour. Creating the client with the folder path in the name, the name is set correctly. However, the uri that is internally generated, url encodes the name and thus, I do not get folders, just an incorrect filename. For example: container.GetBlobClient("2020/01/90afae7a-de06-4239-8c1b-a01e5e62f199.png"); Returns a blob, that's properties look like this: Name: "2021/01/90afae7a-de06-4239-8c1b-a01e5e62f199.png" Uri: "/development/2021%2F01%2F90afae7a-de06-4239-8c1b-a01e5e62f199.png" (Absolute path)
0

I have done it like so with Azure Blob Storage client library for .NET

 public async Task<string> UploadOldDatabaseAsync(OldDatabaseDto oldDatabase)
 {
     var container = new BlobContainerClient(_storageConnectionString, "old-database-container");
     string blobName = string.Format("{0}/data.db", oldDatabase.UserId);
     await container.CreateIfNotExistsAsync();
     var blob = container.GetBlobClient(blobName);
     var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(oldDatabase.DatabaseStream));
     await blob.UploadAsync(memoryStream, overwrite: true);
     return blob.Uri.ToString();
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.