As I explore the Azure I got an idea of access and manipulate the azure storage through an application.as a IT Pro I’m not an expert of cording 😀 but I thought of giving a try to test it.
So first we have to create a azure storage account and create a container in blob storage
Next Create a .NET console application using Visual Studio
- Create a new .NET console application
File->New->Project->Visual C#->Windows->Console Application
- Then we need to import NuGet Package to the console application, right click the project in solution explore and click Manage NuGet Packages.
- In NuGet Package window search for the WindowsAzure.Storage and install it as follows
- Next step we have to modify the app.config file as follows
Note-Connection String can found in storage accounts keys setting<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key=”StorageConnectionString” value=”DefaultEndpointsProtocol=https;Accou ntName=<your account name>;AccountKey=<your account key>” /></appSettings> </configuration>
- Add relevant reference to the project to access the azure storage
Note:- To use the Configuration reference we have to add the System.Configuration dll file to the reference section in solution explorer.Refer this link - Add the relevant code to Program.cs file access the Azure blob
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Auth; using Microsoft.WindowsAzure.Storage.Blob; namespace AzureStorageAccess { class Program { static void Main(string[] args) { //Get storage account from connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); //create a blob client from storage account CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient(); //retreve referance to container CloudBlobContainer container = blobclient.GetContainerReference("mycontainer"); //create a containe rif it doesnot exist container.CreateIfNotExists(); //retreve referance to a blob name "testblob" CloudBlockBlob blockBlob = container.GetBlockBlobReference("testblob"); //create or overwrite the "testblob" with local files using (var fileStream = System.IO.File.OpenRead(@"C:\storage\storage.txt")) { blockBlob.UploadFromStream(fileStream); } } } }
- Next Run the project and you will see a file uploaded to the azure storage container you mentioned in the code.
As you can see above the file storage.txt which stays at the local file system and i upload that file to the azure blob as myblob and the content of the file are same.
That’s all for this post hope you learn anything new and hope to see you soon. If you have any question comment on the post.