Access Azure Storage through a Simple .NET Application

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
1

2

Next Create a .NET console application using Visual Studio

  1. Create a new .NET console application
    File->New->Project->Visual C#->Windows->Console Application
    console
  2. Then we need to import NuGet Package to the console application, right click the project in solution explore and click Manage NuGet Packages.
  3. In NuGet Package window search for the WindowsAzure.Storage and install it as follows
    Package
  4. Next step we have to modify the app.config file as follows
    config file.PNG
    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>
    
  5. Add relevant reference to the project to access the azure storage
    using Statements.PNG
    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
  6. 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);
                }
            }
        }
    }
    
  7. Next Run the project and you will see a file uploaded to the azure storage container you mentioned in the code.
    Capture
    blobfileAs 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.