Introduction
In today’s article, we will look at a feature of Microsoft Azure, which is Microsoft’s Cloud offering. One of the main services provided with Azure is Cloud Storage. This allows us to store different artifacts in the Cloud. These include Tables, Queues, Files, and Containers. Containers are used to store Blobs which can be any type of file including text files, image files, video files, etc. You can read more about the different types of Blobs on the web.
In this article, we will look at how to create an Azure Blob Container and then using C#, upload a text file there.
How to create the Azure Storage Account and Container
In order to complete this step, you would need a Microsoft Azure account with an active subscription. You can get a free one for initial use. However, as my free subscription ended a long time ago, I have set up a pay-as-you-go one. Kindly follow the below steps,
Once you have created your Azure portal account and subscription, you see the below. You can see I have one subscription which is “Pay-As-You-Go”,
Select “Create a resource”,
Type “Storage Account”,
Click “Create”,
Here, you select the subscription and resource group (you can create a new one if required),
Then, enter a Storage account name, region, performance, and redundancy value. I will not discuss these options in detail here as there are many articles that detail them.
Once done click “Review + Create”, you will see the below,
Click the Create button, resource group and storage account will be created.
Next, we need to create the container. Go to the main page,
Here, you see the resource group and a storage account you have just created. Select the storage account and then the “Containers” option under “Data storage” as below,
Next, select “+ Container” to add a new container as below,
Name the container “blobcontainer” and create it.
Finally, copy the connection string from the storage account as this is needed to access the container from the C# code.
Now, we will create our C# application to upload a file to this container we have just created.
How to create the Visual Studio 2019 application
Next, we will create a console application using Visual Studio 2019 Community edition as below,
Once done we will add a class library to the project.
Next, we will add the required Nugget packages,
Now that we have the environment all setup, add the code below to the “Program.cs” and “AzureBlobClient.cs” files,
Program.cs
- using AzureBlobStorageClient;
- using System;
- await AzureBlobClient.UploadBlob();
- Console.ReadKey();
AzureBlobClient.cs
- using Azure.Storage.Blobs;
- using System;
- using System.IO;
- using System.Threading.Tasks;
- namespace AzureBlobStorageClient {
- public class AzureBlobClient {
- public static async Task UploadBlob() {
- var connectionString = "<Enter the connection string here>";
- string containerName = "blobcontainer";
- var serviceClient = new BlobServiceClient(connectionString);
- var containerClient = serviceClient.GetBlobContainerClient(containerName);
- var path = @ "c:\temp";
- var fileName = "Testfile.txt";
- var localFile = Path.Combine(path, fileName);
- await File.WriteAllTextAsync(localFile, "This is a test message");
- var blobClient = containerClient.GetBlobClient(fileName);
- Console.WriteLine("Uploading to Blob storage");
- using FileStream uploadFileStream = File.OpenRead(localFile);
- await blobClient.UploadAsync(uploadFileStream, true);
- uploadFileStream.Close();
- }
- }
- }
Now, we run the application and after that when we look into our container, we see that the new file has been uploaded there.
Summary
In this article, we looked at creating a Microsoft Azure storage account and then added a blob container to it. Then, we created a Visual Studio project to create and upload a file to this blob container. In the coming articles, I will talk about the other data storage types.
Happy coding!