Tom
Tom

Reputation: 665

Azure.Storage.Blobs Folder Structure

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/:

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?

Upvotes: 1

Views: 3593

Answers (3)

Sampath
Sampath

Reputation: 65978

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();
 }

Upvotes: 0

Harshita Singh
Harshita Singh

Reputation: 4870

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.

Upvotes: 0

Deepak
Deepak

Reputation: 2742

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.

Upvotes: 1

Related Questions