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

    <br />
    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;<br />
    &lt;configuration&gt;<br />
        &lt;startup&gt;<br />
            &lt;supportedRuntime version=&quot;v4.0&quot; sku=&quot;.NETFramework,Version=v4.5&quot; /&gt;<br />
            &lt;/startup&gt;<br />
    &lt;appSettings&gt;<br />
      &lt;add key=”StorageConnectionString” value=”DefaultEndpointsProtocol=https;Accou<br />
    ntName=&lt;your account name&gt;;AccountKey=&lt;your account key&gt;” /&gt;&lt;/appSettings&gt;<br />
    &lt;/configuration&gt;<br />
    

  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

    <br />
    using System;<br />
    using System.Collections.Generic;<br />
    using System.Linq;<br />
    using System.Text;<br />
    using System.Threading.Tasks;<br />
    using System.Configuration;<br />
    using Microsoft.WindowsAzure;<br />
    using Microsoft.WindowsAzure.Storage;<br />
    using Microsoft.WindowsAzure.Storage.Auth;<br />
    using Microsoft.WindowsAzure.Storage.Blob;</p>
    <p>namespace AzureStorageAccess<br />
    {<br />
        class Program<br />
        {<br />
            static void Main(string[] args)<br />
            {<br />
                //Get storage account from connection string<br />
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings[&quot;StorageConnectionString&quot;]);</p>
    <p>            //create a blob client from storage account<br />
                CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();</p>
    <p>            //retreve referance to container<br />
                CloudBlobContainer container = blobclient.GetContainerReference(&quot;mycontainer&quot;);</p>
    <p>            //create a containe rif it doesnot exist<br />
                container.CreateIfNotExists();</p>
    <p>            //retreve referance to a blob name &quot;testblob&quot;<br />
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(&quot;testblob&quot;);</p>
    <p>            //create or overwrite the &quot;testblob&quot; with local files<br />
                using (var fileStream = System.IO.File.OpenRead(@&quot;C:\storage\storage.txt&quot;))<br />
                {<br />
                    blockBlob.UploadFromStream(fileStream);<br />
                }<br />
            }<br />
        }<br />
    }<br />
    

  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.

 

 

 

, , ,

Post navigation