Step1: Open Visual studio and create a new .NET Core solution

Step2: Open cmd and navigate to the directory of your project using below command:
cd C:\Users\vishkuma\source\repos\ConsoleApp20\ConsoleApp20
Add nuget package to Azure storage using below command:
dotnet add package WindowsAzure.Storage
Add Microsoft.Extensions.Configuration.Json Nuget package by executing below command in cmd
dotnet add package Microsoft.Extensions.Configuration.Json
You’ll see both packages added in your application.

Step3 : Now add a json file named appsetiings.json in the project

Now right click on appsettings.json and change the copy to output directory property to “Copy if newer”

Step4: Paste below value in appsetiings.json file:
{
"StorageAccountConnectionString": "<your storage account connection string>"
}
Replace <your storage account connection string> with your storage account connection string.
You can get the connection string from account keys blade in your storage account:

Step5: Add below using statements in program.cs
using Microsoft.Extensions.Configuration;
using System.IO;
using Microsoft.WindowsAzure.Storage;
using System.Threading.Tasks;
Replace the main method with below code
static async Task Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build();
var connectionString = configuration["StorageAccountConnectionString"];
if (!CloudStorageAccount.TryParse(connectionString,
out CloudStorageAccount storageAccount))
{
Console.WriteLine("Unable to parse connection string");
return;
}
var blobClient = storageAccount.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("mycontainer");
bool created = await blobContainer.CreateIfNotExistsAsync();
Console.WriteLine(created ? "Created the Blob container" : "Blob container already exists.");
//Create blockBlob Reference and Read data from local file and write to blob
var blockBlob = blobContainer.GetBlockBlobReference("textfile.txt");
using (Stream myStream = await blockBlob.OpenWriteAsync())
{
byte[] data = File.ReadAllBytes(@"C:\Users\vishkuma\Documents\test.txt");
myStream.Write(data);
}
Console.WriteLine("File Uploaded successfully.");
}
Code explanation :
CloudStorageAccount.TryParse try to validate the connection string and if it is correct it return the CloudStorageAccount object named storageAccount which can be used to initialize blobClient object.
var blobClient = storageAccount.CreateCloudBlobClient();
Above line initialize BlobClient object. If you are are dealing with Storage account FileShare, Queues or Tables, you can use CreateCloudFileClient, CreateCloudQueueClient or CreateCloudTableClient to initialize object wiing which you can perform operation on your object.
var blobContainer = blobClient.GetContainerReference("mycontainer");
bool created = await blobContainer.CreateIfNotExistsAsync();
Above code create a reference to a container named “mycontainer” and create that container if it doesn’t exist in your storage account.
var blockBlob = blobContainer.GetBlockBlobReference("textfile.txt");
using (Stream myStream = await blockBlob.OpenWriteAsync())
{
byte[] data = File.ReadAllBytes(@"C:\Users\vishkuma\Documents\test.txt");
myStream.Write(data);
}
Above code create BlockBlob reference and open a stream with blob. Then it read data from a file from local machine in byte array and write data to the opened steam.
Now build the project and run it. You would see the container created and file uploaded on your storage account:

Leave a Reply