Reading Azure Storage Blobs using Visual Studio 2017


Reading Azure Storage Blobs using Visual Studio 2017

You can also use Server Manager in Visual Studio 2017 to view the contents of your blob containers and upload or download files.

1. Navigate to the blob storage account that you want to use.
2. Double-click the blob storage account to open a window showing a list of blobs and providing functionality to upload or download blobs.


Changing data

You can modify the contents of a blob or delete a blob using the Storage API directly, but it is more common to do this programmatically as part of an application, for example using the Storage Client Library.

The following steps illustrate how to update a blob programmatically. Note that this example uses a block blob.

1. Fileà Open ->Project àOpen visual studio 2017 and Create C# Console application as shown below screen shot.



2.  In your app.config file, create a storage configuration string and entry, replacing AccountName (storage account name and AccountKey with you storage account values   or  you should be able to write connection string in the code as show below.

string connection =  DefaultEndpointsProtocol=https;AccountName=trainingsazurestorage;AccountKey=vdd5FRLiwBtyUWrOG5stY3oOBRgFBVZT9tedUr9vrpLHcuUEOnsRWi44BKcNu0t6QWlzjxcAuGXr==";
      

    



 3.  Use NuGet to obtain the Microsoft.WindowsAzure.Storage.dll. Install below package from visual studio 2017, please follow below steps.

   1.   Right click on console project and select “Manage Nuget packages” as shown blow.
  

       2. Search for windowsazure.Storage and click install 




1       3. Package is installed successfully to your project as shown below.





14. Add the following Windows Azure reference to class using statements to the top of your Program.cs file:



1  5.   Add the following code in the main entry point:



1  6.  Use CloudBlobClient to gain access to the containers and blobs in your Azure storage account. After it is created, you can set permissions to make it publicly available:


CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();





    7.  Use a CreateIfNotExists method to ensure a container is there before you interact with it:


CloudBlobContainer container = blobClient.GetContainerReference(“files”);
                                container.CreateIfNotExists();
                         container.SetPermissions(new BlobContainerPermissions {PublicAccess =
BlobContainerPublicAccessType.Blob });





    8.   To upload a File, use the FileStream object to access the stream, and then use the UploadFromFileStream method on the CloudBlockBlob class to upload the file to Azure blob storage:



                  CloudBlockBlob blockBlob = container.GetBlockBlobReference(“trainingazureBlob”);
using (var fileStream = System.IO.File.OpenRead(@c:\test\contacts1.txt”))
{
blockBlob.UploadFromStream(fileStream);

}



 9. Press F5 to run the application, file will be added to blob in azure  portal with name “trainingazureblob” as shown below.




10.  You should be able to download blob by click download(highlighted) button on command bar in azure portal as shown.



          11.  To download blobs, use the CloudBlobContainer using below code.
                          
                             CloudBlockBlob blockBlob = container.GetBlockBlobReference(“contacts1.txt”);
       using (var fileStream = System.IO.File.OpenWrite(@”c:\test\contacts1.txt”))
{                      blockBlob.DownloadToStream(fileStream);
}
}






Comments

Popular posts from this blog

Azure Storage Tables with Visual Studio 2017

Azure Storage Accounts

What is Azure?