Create blob storage container in c# using fluent API
Asked Answered
C

2

6

I am using the C# management API of Azure to create my resources in the cloud. I am stuck creating a container in the blob storage. I did create a storage account following these instructions:

// storageAccount here is a Microsoft.Azure.Management.Storage.Fluent.IStorageAccount instance
var storageAccount = _azure.StorageAccounts
                .Define(name)
                .WithRegion(region)
                .WithExistingResourceGroup(resourceGroup)
                .WithBlobEncryption()
                .WithOnlyHttpsTraffic()
                .WithBlobStorageAccountKind()
                .WithAccessTier(AccessTier.Hot)
                .Create();

But I do not know how to go on from there. From the Microsoft documentation it seems that I have to retrieve a Microsoft.WindowsAzure.Storage.CloudStorageAccount instance, but the storageAccount instance I obtain with the above code lives in the namespace Microsoft.Azure.Management.Storage.Fluent, and I have not found any way to get the CloudStorageAccount from there.

Is it possible to create containers directly with the API in the fluent namespace? If not, how do I get the CloudStorageAccount instance from my IStorageAccount one?

This answer in SO is not helpful because it does use the Microsoft.WindowsAzure namespace directly.

Crossfertilization answered 26/2, 2018 at 12:44 Comment(0)
I
6

Is it possible to create containers directly with the API in the fluent namespace? If not, how do I get the CloudStorageAccount instance from my IStorageAccount one?

At least as of today, it is not possible. When it comes to managing storage accounts in Azure, there are two REST APIs - Storage Resource Provider REST API and Storage Service REST API. Former deals with management of storage account themselves (like creation/updation etc.) while the latter deals with the data in storage account. You can say that the former API deals with control plane and the latter deals with data plan. Creation of a blob container falls under latter category.

Fluent API you mentioned is a wrapper over Storage Resource Provider API while Microsoft.WindowsAzure.Storage library is a wrapper over Storage Service REST API. You would need to reference this library in your project to create blob containers.

What you could do is get the access key (key 1 or key 2) using the Fluent API. Once you have the key, you will create an instance of CloudStorageAccount using Microsoft.WindowsAzure.Storage library and create a blob container using that. I know that this is not the answer you're looking for but unfortunately this is the only way as of today.

Ironmaster answered 26/2, 2018 at 13:38 Comment(1)
Thank you so much @Gaurav Mantri, it is not the answer I was looking for but it clearly explains why it doesn't work as I expected. It is really a pity that you can't create blobs directly using the object returned by the storage account creation, it feels like the natural way to do it. Hope Microsoft improves these APIs in the near future.Crossfertilization
E
1

This is now possible with Fluent API. Using your StorageAccount, you can do:

storageAccount.Manager.BlobContainers.DefineContainer(containerName)
.WithExistingBlobService(resourceGroupName, storageAccountName)
.WithPublicAccess(PublicAccess.None)
.CreateAsync();
Enschede answered 20/7, 2020 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.