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.
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
- CloudBlockBlob blockBlob = container.GetBlockBlobReference("AzureStorageAccount");
- using(var filestream = System.IO.File.OpenRead(@ "C:\Users\devel\Documents\najuma\data.csv")) {
- blockBlob.UploadFromStream(filestream);
- }
- Console.ReadKey();
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.
Press any key to continue.
Open the Azure portal and check the blob which has been uploaded to the container.
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.
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.
Set Container Properties and Metadata on Azure Storage
Step 6
On the Azure portal, select Blob Service and the container followed by Properties.
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);
- 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
- static void ListAttributes(CloudBlobContainer container)
- {
- container.FetchAttributes();
- Console.WriteLine("Container name " + container.StorageUri.PrimaryUri.ToString());
- Console.WriteLine("Last modified " + container.Properties.LastModified.ToString());
- }
Click on "Start" to build and run the solution.
You should be receiving an output, as shown below.
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
Set two (Key, Value) pairs to the Container.
Code Snippet
- static void SetMetadata(CloudBlobContainer container) {
- container.Metadata.Clear();
- container.Metadata.Add("author", "Najuma M");
- container.Metadata["authoredOn"] = "Jan 26, 2018";
- container.SetMetadata();
- }
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);
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.