Starting Azure Platform as a Service (PaaS) Development 101 Series: Lesson 3 of n – BLOB
Welcome back to the series You can always view the list of all lessons here. Lesson 3 will be all about Azure BLOB Storage.
First things first.!
BLOB storage is part of storage accounts. BLOB storage enables you to store files in the cloud. When you upload a file into a BLOB container, you will start referring to the file as a BLOB and not a file anymore, don’t ask me why . There are two types of BLOBs, Block BLOBs and Page BLOBs. Block BLOBs can sit in one fixed block of a max of 200GB while Page BLOBs can grow up to 1TB and is used mostly when ranges of bytes in a file are frequently updated. Unfortunately you will get a flat container, meaning you can not have folders and sub folders there. Easiest trick in the book to get organized is to include a “/” in the BLOB name e.g. “images/avatar01.png” instead of “avatar1.png”. Below diagram quickly illustrates the structure where containers sit inside storage accounts and BLOBs would sit inside the containers.
To learn more about Azure BLOB Storage click here.
What are we building today?
Since we have established by now that we won’t have fancy sample applications but instead direct answers in this series, I expect you to go over the previous lesson before continuing. I won’t show you how to create a storage account and get your hands on an access key every post
In a nutshell, we will build a simple CRUD command line application for an Azure Blob. Checkout the below screenshot of the console application.
The Console Application
Very straight forward, an endless loop displaying the menu and waiting for the user to input. A switch statement is there to trigger the right function based on user input.
- private const string Protocol = "https";
- private const string AccountName = "paas101";
- private const string AccountKey = "XXXXXX-KEY GOES HERE";
- private const string ContainerName = "lesson03";
- private const string Success = "\nOperation completed successfuly. Press any key to go back.";
- private static string _connection;
- static void Main(string[] args)
- {
- _connection = string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}",
- Protocol, AccountName, AccountKey);
- Console.TreatControlCAsInput = true;
- Console.Clear();
- while (true)
- {
- Console.Clear();
- Console.WriteLine("Welcome to Lesson 03, let's play with BLOB Storage.");
- Console.WriteLine("\t1. Upload a new BLOB");
- Console.WriteLine("\t2. Delete a specific BLOB");
- Console.WriteLine("\t3. List all BLOBs");
- Console.WriteLine("\t4. Exit");
- Console.WriteLine("So what do you want to do?");
- int input;
- if (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out input)) return;
- switch (input)
- {
- case 1:
- UploadNewBlob();
- break;
- case 2:
- DeleteBlob();
- break;
- case 3:
- ListAllBlobs();
- break;
- case 4:
- Exit();
- break;
- }
- }
- }
- private static void Exit()
- {
- Environment.Exit(0);
- }
Upload a new BLOB
To keep it simple, we will ask the user for the path of a file they would like to upload to the BLOB container. At the main menu, when the user would input 1, they will be directed to the input screen.
At the input screen we will ask them about their file path.
Check out the function’s code below. We simply establish a BLOB client, get a reference to the container, ask the user for the file path and execute the upload activity.
- private static void UploadNewBlob()
- {
- try
- {
- var client = CloudStorageAccount.Parse(_connection).CreateCloudBlobClient();
- Console.Clear();
- Console.WriteLine("Where is your file?");
- var filePath = Console.ReadLine();
- var container = client.GetContainerReference(ContainerName);
- container.CreateIfNotExists();
- var blob = container.GetBlockBlobReference(filePath.Substring(filePath.LastIndexOf("\\") + 1));
- using (var fileStream = File.OpenRead(filePath))
- {
- blob.UploadFromStream(fileStream);
- }
- Console.WriteLine(Success);
- Console.ReadKey();
- }
- catch (StorageException ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadKey();
- }
- }
Delete a specific BLOB
From the main menu, the user will input 2 and be directed to the input screen.
At the input screen we will ask the user for the name of the BLOB they would like to delete.
To verify that the Blob was deleted, Browse the Server Explorer to reach the container and list it’s contents. As you can see the Blob03
Check out the function’s code below. We simply establish a BLOB client, get a reference to the container, ask the user for the BLOB name and execute the deletion activity.
- private static void DeleteBlob()
- {
- try
- {
- var client = CloudStorageAccount.Parse(_connection).CreateCloudBlobClient();
- var container = client.GetContainerReference(ContainerName);
- container.CreateIfNotExists();
- Console.Clear();
- Console.WriteLine("\nEnter Blob name: ");
- var blobName = Console.ReadLine();
- var blockBlob = container.GetBlockBlobReference(blobName);
- blockBlob.Delete();
- Console.WriteLine(Success);
- Console.ReadKey();
- }
- catch (StorageException ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadKey();
- }
- }
List all BLOBs in a Container
From the main menu, the user will input 3 and be directed to the list screen.
At the list screen an output of all the BLOB Uri’s will be displayed.
Check out the function’s code below. We simply establish a BLOB client, get a reference to the container, and finally loop the container BLOBs printing out the Uri.
- private static void ListAllBlobs()
- {
- try
- {
- var client = CloudStorageAccount.Parse(_connection).CreateCloudBlobClient();
- var container = client.GetContainerReference(ContainerName);
- container.CreateIfNotExists();
- Console.Clear();
- foreach (var item in container.ListBlobs(null, false))
- {
- var blockBlob = item as CloudBlockBlob;
- if (blockBlob != null)
- {
- var blob = blockBlob;
- Console.WriteLine(blob.Uri);
- }
- else
- {
- var blob = item as CloudPageBlob;
- if (blob != null)
- {
- var pageBlob = blob;
- Console.WriteLine(pageBlob.Uri);
- }
- else
- {
- var blobDirectory = item as CloudBlobDirectory;
- if (blobDirectory == null) continue;
- var directory = blobDirectory;
- Console.WriteLine("Directory: {0}", directory.Uri);
- }
- }
- }
- Console.WriteLine(Success);
- Console.ReadKey();
- }
- catch (StorageException ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadKey();
- }
- }
aaaand we are done with the lesson. Let me know if you have any questions or comments
Comments
- Anonymous
October 23, 2014
molto interessante - Anonymous
October 24, 2014
The comment has been removed - Anonymous
October 29, 2014
@Paul Topping: Personally I would always go for BLOB and CDN. A hit over CDN will be always faster than connecting to a database and querying a table. In addition, how would the solution scale in case you have a growing user base globally? There are a lot of things to consider :-) - Anonymous
October 29, 2014
@Luigi Crispo: grazie il mio amico per la lettura il mio diario