Bryan
Bryan

Reputation: 3541

Create container in Azure storage with c#

I have the following code:

var cloudStorageAccount = CloudStorageAccount.Parse(this._appSettings.BlobConnectionString);
this._cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

CloudBlobContainer container = this._cloudBlobClient.GetContainerReference("dpd/textures/test");
await container.CreateIfNotExistsAsync();

I want to create the "test" directory inside dpd/textures/, but I keep getting this error:

The requested URI does not represent any resource on the server azure container

What am I doing wrong?

Upvotes: 2

Views: 946

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30025

Actually, there is no real directory in blob storage. The directory(not container) is always a part of the blob's name.

So you can just create a container like "dpd", then you can use any upload methods to upload a file like myfile.txt. During uploading file, you can specify your file name as "textures/test/myfile.txt" in your GetBlockBlobReference method. Then the directory "textures/test" is created automatically.

Simple code:

            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("test1");
            var myblob = cloudBlobContainer.GetBlockBlobReference("textures/test/myfile.txt");
            myblob.UploadFromFile("your file path");

Test result:

enter image description here

And also note that, if the blob(myfile.txt) is deleted, then the directory "textures/test" will also be removed, since it's a part name of the blob.

Upvotes: 2

Related Questions