Azure Blob Storage Service - Uploading A Blob And Set Container Propertied With Metadata - Part Two

Introduction

This is an article about uploading a blob on Azure Storage and setting container properties with metadata. Before reading this article please go through the below writings,

  • Click here for Azure Storage Components – Blob.
  • Click here for Azure Storage – Creating a container using Visual Studio.

Follow the below steps now.

Note

This article assumes that you already have an Azure Storage account created and the solution file for Visual Studio which was created in the previous demo.

Step 1

Open the solution file in Visual Studio, as shown below.

Azure Blob Storage

Azure Blob Storage

Step 2 Uploading a Blob to the container

After creating a container, insert the code below to upload a blob to the container.

Note

Make sure that you have given the exact location of where the file is available on your machine to upload towards the container.

Code Snippet

  1. CloudBlockBlob blockBlob = container.GetBlockBlobReference("AzureStorageAccount");  
  2. using(var filestream = System.IO.File.OpenRead(@ "C:\Users\devel\Documents\najuma\data.csv")) {  
  3.     blockBlob.UploadFromStream(filestream);  
  4. }  
  5. Console.ReadKey(); 

Azure Blob Storage

Upload a Block Blob onto the Container with a particular reference. The Using function performs a file stream operation to upload a Block Blob by mentioning its path.

Step 3

Click on "Start" to build the application.

Azure Blob Storage

Azure Blob Storage

Press any key to continue.

Azure Blob Storage

Open the Azure portal and check the blob which has been uploaded to the container.
Azure Blob Storage

Step 4 - Access policies for the blob on Containers.

The Access Policy for the blob on containers can be managed with the access for both the blobs and containers on the portal. Click on Access Policy over here.
Azure Blob Storage

From the public access level, specify if the data in the container may be accessed publicly. By default, the container data is private to the account owner. You can use the blob to allow public read access for blobs, use Container to allow public read and list access to the entire Container.

Step 5 - Download Blobs from the Container

You can also download the blobs from the container whenever needed, using the download option, as shown below.
Azure Blob Storage

Set Container Properties and Metadata on Azure Storage

Step 6

On the Azure portal, select Blob Service and the container followed by Properties.

Azure Blob Storage

Step 7 Listing container properties programmatically

Add the below code to list the attributes as given below and run the code. Use ListAttributes command to list the container properties.

ListAttributes(container);

Azure Blob Storage

  • Use ListAttributes() function to list the desired properties of a Container
  • Get the properties using the FetchAttributes()
  • Write the output to the Console

Code Snippet

  1. static void ListAttributes(CloudBlobContainer container)   
  2. {  
  3.     container.FetchAttributes();  
  4.     Console.WriteLine("Container name " + container.StorageUri.PrimaryUri.ToString());  
  5.     Console.WriteLine("Last modified " + container.Properties.LastModified.ToString());  

Azure Blob Storage

Click on "Start" to build and run the solution.

Azure Blob Storage

You should be receiving an output, as shown below.

Azure Blob Storage

Step 8 - Set Metadata of the Container

Metadata is a set of data that describes and gives information about other data. For a container define the metadata set (key, value pair), as shown below.

Set Metadata, a (Key, Value) pair to the Container.

Code Snippet

  1. //Set Metadata  
  2. SetMetadata(container);  

Azure Blob Storage

Set two (Key, Value) pairs to the Container.

Code Snippet

  1. static void SetMetadata(CloudBlobContainer container) {  
  2.     container.Metadata.Clear();  
  3.     container.Metadata.Add("author""Najuma M");  
  4.     container.Metadata["authoredOn"] = "Jan 26, 2018";  
  5.     container.SetMetadata();  

 Azure Blob Storage

Step 9 - List Metadata of the Container

Now, list the metadata of the container using ListMetadata() command and run the code.

Code Snippet

ListMetadata(container);

Azure Blob Storage

Summary

Thus, by this article, we have learned about uploading a blob on Azure Storage and setting container properties with metadata. Follow the next article of mine to work on accessing the Blob securely.


Similar Articles