Microsoft Azure Blob Storage - Managing Blobs and storage containers from C#
This is the part two of Microsoft Azure Blob Storage where we will look into managing Microsoft Azure Blobs from C# and .NET.
For this blog, we are going to keep this pretty simple. We will create a new .net console application and add two Nugget packages that make it super easy to work with Microsoft Azure Blobs. Include these two Nugget Packages:
Include Microsoft Azure Storage namespaces
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
Add a Container in Azure Blob Storage from C#
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
The code above obtains storage account by its key, create a connection, obtains a reference for container name mycontainer and finally create the container if it does not already exits.
Upload Image as Block Blob in Azure Storage Container
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
var blockBlob = container.GetBlockBlobReference("imran-images/new/Imran-Duplicate.png");
using (var fileStream = System.IO.File.Open("C:\\Users\\imran\\Downloads\\imran-original.png", System.IO.FileMode.Open))
{
blockBlob.UploadFromStream(fileStream);
}
The above code gets a reference of block blob with the name Imran-Duplicate.png under imran-images/new folder hierarchy (which does not currently exists and we are going to upload it). Then we open a file steam to an image in our local PC and pass it to Azure api for uploading to the container.
Download Block Blob from Azure Blob Storage Container
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
var blockBlobDownload = container.GetBlockBlobReference("imran-images/Imran-Duplicate.png");
using (var fileStream = System.IO.File.Create("C:\\Users\\imran\\Downloads\\imran-downloaded-image-new.png"))
{
blockBlobDownload.DownloadToStream(fileStream);
}
The above code download the same uploaded image in the same folder hierarchy from Microsoft Azure Storage Container to local PC.
Listing all Block Blob in a Container
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
foreach (var blob in container.ListBlobs())
{
Console.WriteLine(blob.Uri);
}
The above code calls ListBlobs() method of container instance to list URI of each blobs in the container.
Adding and updating Azure Container Metadata
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
container.Metadata.Clear(); // clears metadata
// adds a new metadata key-value pair associated with the container
container.Metadata.Add("UserId", "1111");
// adds a new metadata key-value pair associated with the container
container.Metadata.Add("CreatedAt", DateTime.UtcNow.ToString());
// updates existing metadata key-value pair associated with the container
container.Metadata["UpdatedAt"] = DateTime.UtcNow.ToString();
// Finally save Metadata
container.SetMetadata();
The above code first clears all metadata associated to the contianer, then it adds a new key-value pair of CreateAt key, then it shows updating existing key-value pair by accessing dictionary by its key, finally the call to SetMetadata() updates metadata
List of Metadata associated with a container
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
// loads container metadata
container.FetchAttributes();
// prints all metadata associated with the container
foreach (var item in container.Metadata)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
The code in section above prints out all metadata key-value pairs associated to a container.
Copy Block Blob
// Copy Block Blob
var originalImg = container.GetBlobReference("Imran.png");
var copyImg = container.GetBlobReference("Imran-copy.png");
copyImg.BeginStartCopy(originalImg.Uri, (obj) => Console.WriteLine("I am in love"), null);
Above code shows copy blobs from one container to another (a very common use case).
That's it, we have seen how we can create and manage Microsoft Azure Containers as well as Blobs.
For this blog, we are going to keep this pretty simple. We will create a new .net console application and add two Nugget packages that make it super easy to work with Microsoft Azure Blobs. Include these two Nugget Packages:
- WindowsAzure.Storage
- Microsoft.WindowsAzure.ConfigurationManager
Obtain Access keys from Azure Portal
Add it Access Key to App.Config
Include Microsoft Azure Storage namespaces
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
Add a Container in Azure Blob Storage from C#
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
The code above obtains storage account by its key, create a connection, obtains a reference for container name mycontainer and finally create the container if it does not already exits.
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
using (var fileStream = System.IO.File.Open("C:\\Users\\imran\\Downloads\\imran-original.png", System.IO.FileMode.Open))
{
blockBlob.UploadFromStream(fileStream);
}
The above code gets a reference of block blob with the name Imran-Duplicate.png under imran-images/new folder hierarchy (which does not currently exists and we are going to upload it). Then we open a file steam to an image in our local PC and pass it to Azure api for uploading to the container.
Download Block Blob from Azure Blob Storage Container
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
var blockBlobDownload = container.GetBlockBlobReference("imran-images/Imran-Duplicate.png");
using (var fileStream = System.IO.File.Create("C:\\Users\\imran\\Downloads\\imran-downloaded-image-new.png"))
{
blockBlobDownload.DownloadToStream(fileStream);
}
The above code download the same uploaded image in the same folder hierarchy from Microsoft Azure Storage Container to local PC.
Listing all Block Blob in a Container
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
foreach (var blob in container.ListBlobs())
{
Console.WriteLine(blob.Uri);
}
The above code calls ListBlobs() method of container instance to list URI of each blobs in the container.
Adding and updating Azure Container Metadata
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
container.Metadata.Clear(); // clears metadata
// adds a new metadata key-value pair associated with the container
container.Metadata.Add("UserId", "1111");
// adds a new metadata key-value pair associated with the container
container.Metadata.Add("CreatedAt", DateTime.UtcNow.ToString());
// updates existing metadata key-value pair associated with the container
container.Metadata["UpdatedAt"] = DateTime.UtcNow.ToString();
// Finally save Metadata
container.SetMetadata();
The above code first clears all metadata associated to the contianer, then it adds a new key-value pair of CreateAt key, then it shows updating existing key-value pair by accessing dictionary by its key, finally the call to SetMetadata() updates metadata
var storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("AzureStorageConnection"));
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
container.FetchAttributes();
// prints all metadata associated with the container
foreach (var item in container.Metadata)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
The code in section above prints out all metadata key-value pairs associated to a container.
Copy Block Blob
// Copy Block Blob
var originalImg = container.GetBlobReference("Imran.png");
var copyImg = container.GetBlobReference("Imran-copy.png");
copyImg.BeginStartCopy(originalImg.Uri, (obj) => Console.WriteLine("I am in love"), null);
Above code shows copy blobs from one container to another (a very common use case).
That's it, we have seen how we can create and manage Microsoft Azure Containers as well as Blobs.
Such a great post! It looks like a blog and in this content was useful for freshers. Keeping a good job.
ReplyDeleteTableau Training in Chennai
Tableau Course in Chennai
Pega Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai
Oracle Training in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
ReplyDeletePython Training in Chennai
Python Course in Chennai
PHP Training in Chennai
Hadoop Training in Chennai
Big Data Training in Chennai
German Classes in Chennai
German Language Classes in Chennai
Python Training in Velachery
This blog is really nice and informative blog, The explanation given is really comprehensive and informative.
ReplyDeleteDOT NET Training in Bangalore
DOT NET Training in Chennai
Dot NET Training in Marathahalli
DOT NET Training Institute in Marathahalli
DOT NET Course in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
Spoken English Classes in Bangalore
The blog you shared is very good. I expect more information from you like this blog. Thank you.
ReplyDeleteArtificial Intelligence Course in Chennai
ai courses in chennai
artificial intelligence training in chennai
ai classes in chennai
best artificial intelligence training in chennai
Hadoop Training in Bangalore
salesforce training in bangalore
Python Training in Bangalore
I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
I am really impressed with the way of writing of this blog regarding microsoft azure cloud migration services. Thank you for sharing.
ReplyDeleteazure cloud migration services
You have provided a richly informative article about Microsoft Azure Blob Storage. It is a beneficial article for me and also helpful for others. Thanks for sharing this information here. oracle fusion financials
ReplyDeleteGlad to check this blog because it’s a nice and informative blog.
ReplyDeleteTally Course in Chennai
CCNA Course in Chennai
SEO Training in Chennai
Hadoop Training in Chennai
Cloud Computing Training in Chennai
Blue Prism Training in Chennai
Amazing post..Keep updating your blog
ReplyDeleteDigital Marketing Training in Chennai
Best Digital Marketing Institute in Bangalore
Good Informative Blog!!! Keep Sharing
ReplyDeleteSelenium Training in Chennai
Selenium Course in Chennai
Selenium Training Institute in Chennai
Best Selenium Training Institute in Bangalore
Selenium Training in Bangalore
Selenium Training Institute in Bangalore
I loved the information given above, I appreciate the writing. Enhance your learning experience with our top-notch online home tuition classes for Class 11.
ReplyDeleteFor more info Contact us: +91-9654271931, +971-505593798 or visit online tutoring sites for class 11
Diyarbakır
ReplyDeleteKırklareli
Kastamonu
Siirt
Diyarbakır
35H
Thank you so much for taking the time to share this wonderful article. Join Ziyyara's online English tuition classes for a personalized learning experience. Master the language, improve fluency, and boost your confidence in English communication with expert guidance and interactive sessions.
ReplyDeleteFor more info visit Online english tuition
E7EAA
ReplyDeleteSinop Lojistik
Denizli Evden Eve Nakliyat
Kırıkkale Lojistik
Şırnak Lojistik
Siirt Evden Eve Nakliyat
87261
ReplyDeleteIğdır Parça Eşya Taşıma
Çorum Evden Eve Nakliyat
Kütahya Parça Eşya Taşıma
Rize Lojistik
Karaman Evden Eve Nakliyat
BE215
ReplyDeleteBursa Şehir İçi Nakliyat
Hatay Lojistik
Şırnak Şehir İçi Nakliyat
Manisa Lojistik
Kripto Para Borsaları
Yalova Parça Eşya Taşıma
Kırklareli Şehir İçi Nakliyat
Çorum Şehir İçi Nakliyat
Kastamonu Parça Eşya Taşıma
A5FE0
ReplyDeleteÜnye Marangoz
Hakkari Lojistik
Çerkezköy Korkuluk
Ağrı Evden Eve Nakliyat
Yalova Parça Eşya Taşıma
Rize Şehirler Arası Nakliyat
Ünye Oto Elektrik
Zonguldak Şehirler Arası Nakliyat
Erzurum Evden Eve Nakliyat
7DCE3
ReplyDeletereferans kodu
13E51
ReplyDeletemanisa canlı sohbet et
goruntulu sohbet
maraş ücretsiz sohbet
antalya rastgele sohbet siteleri
ucretsiz sohbet
telefonda canlı sohbet
canlı sohbet uygulamaları
ücretsiz sohbet odaları
zonguldak görüntülü sohbet siteleri ücretsiz
64119
ReplyDeleteyalova canlı sohbet siteleri
adana sesli sohbet sitesi
Elazığ Ücretsiz Sohbet Uygulamaları
mardin ücretsiz sohbet sitesi
trabzon canlı ücretsiz sohbet
Bingöl Görüntülü Sohbet Yabancı
ücretsiz sohbet sitesi
görüntülü sohbet canlı
ankara sesli sohbet odası
D0834
ReplyDeleteArg Coin Hangi Borsada
Chat Gpt Coin Hangi Borsada
Mexc Borsası Güvenilir mi
Tumblr Takipçi Hilesi
Luffy Coin Hangi Borsada
Binance Referans Kodu
Parasız Görüntülü Sohbet
Kwai Beğeni Satın Al
Nexa Coin Hangi Borsada
CECEC
ReplyDeletegate io
bitexen
4g mobil proxy
bybit
bibox
bitcoin ne zaman çıktı
telegram kripto
paribu
en iyi kripto grupları telegram
EE700
ReplyDeleteokex
referans kimligi nedir
mexc
probit
referans kod
paribu
bingx
kraken
bitcoin seans saatleri
2B036
ReplyDeletebybit
kripto kanalları telegram
bitexen
telegram kripto
February 2024 Calendar
bitcoin haram mı
kredi kartı ile kripto para alma
filtre kağıdı
bitcoin hangi bankalarda var
FDA34
ReplyDeletefiltre kağıdı
bitexen
en az komisyon alan kripto borsası
bitexen
binance
May 2024 Calendar
April 2024 Calendar
referans kimligi nedir
2024 Calendar
9B532
ReplyDeleteücretli güvenilir şov
B2F73
ReplyDeleteücretli sanal show
FDD95
ReplyDeletewhatsapp görüntülü show güvenilir
989CED4106
ReplyDeletecam şov
ücretli show
telegram show
canli web cam show
cam show
canli cam show
şov
görüntülü şov whatsapp numarası
görüntülü şov
5A305E2DB7
ReplyDeletekaldırıcı
skype şov
cam show
whatsapp görüntülü şov
degra
themra macun
green temptation
whatsapp ücretli show
vega
6A07BD5CDC
ReplyDeletewhatsapp ücretli show
telegram görüntülü şov
performans arttırıcı
degra
görüntülü şov
lady era
görüntülü şov whatsapp numarası
skype şov
themra macun
32FF716179
ReplyDeleteshow
CB21F605F3
ReplyDeleteskype şov
30B1137D26
ReplyDeletegörüntülü show
vigrande
skype şov
maxman
ücretli show
cialis
bufalo içecek
geciktirici jel
kamagra hap
58E7C140E8
ReplyDeleteinstagram ucuz beğeni satın al
2718AABE7C
ReplyDeletetwitter ucuz beğeni satın al
6221AA847A
ReplyDeleteturk takipci satin al instagram
85DEF1FA5C
ReplyDeleteinstagram takipçi
Whiteout Survival Hediye Kodu
Township Promosyon Kodu
Footer Link Satın Al
Online Oyunlar
Free Fire Elmas Kodu
Roblox Şarkı Kodları
Online Oyunlar
Danone Sürpriz Kodları
DD956535E1
ReplyDeleteEn İyi Telegram Mining Botları
Telegram Coin Kazanma Botları
Yeni Telegram Farm Botları
Telegram Airdrop Botları
Yeni Kripto Telegram Botları
7E1F53846E
ReplyDelete-
-
kuşadası eskort bayan
-
-