DEV Community

Cover image for Working with Storage Blobs
Emily Freeman for Microsoft Azure

Posted on • Edited on

Working with Storage Blobs

We've selected our favorite tips and tricks created by Michael Crump and are delivering fresh technical content on Azure all April! Miss a day (or more)? Catch up with the series.

Don't have Azure? Grab a free subscription.


Azure Storage is described as a service which provides storage that is available, secure, durable, scalable, and redundant.

Azure Storage is an umbrella term. It covers:

We're going to focus on Blob storage for now.

Creating Blob Storage in the Azure Portal ๐Ÿ“‚

Go ahead and open the Azure Portal and click Create a Resource. Select Azure Storage. Weโ€™ll keep it simple as shown below to get started.

create

Once complete โ€” the portal will notify you โ€” go into the resource and look under Services.

find it

Go ahead and click on Blobs and create a Container and give it the name Images.

Remember! A container is a lot like a folder. https://myblob/container/image1.jpg

Youโ€™ll now want to click Upload and select a file on your physical disk.

upload

Now that your file is uploaded, select it. You can click on the ellipsis and select Blob properties to see additional details.

done

Yay! You can download it or access it via the URL provided. That was pretty easy, right?

Let's do the same thing... differently.

Provisioning an Azure Storage Blob Container in C# ๐Ÿ—„๏ธ

Yes, the code we're using is C# but don't freak out if you don't know C# (I don't!) Michael Crump wrote this code and I think it's clear what's happening โ€” regardless of your language of choice.

Navigate to your Azure Storage account in the portal.

Look under Settings, then Access Keys and copy the connection string.

connection string

Create a C# Console Application, and use NuGet to pull in references to:

  • WindowsAzure.Storage
  • Microsoft.WindowsAzure.ConfigurationManager

console application

Inside of your Console app, you will see App.config, now add the following section:

<appSettings>
  <add key="StorageConnection" value="YOUR-CONNECTION-STRING-COPIED-FROM-EARLIER"/>
</appSettings>
Enter fullscreen mode Exit fullscreen mode

Copy the following code into your Main method:

static void Main(string[] args)
{
    var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));
    var myClient = storageAccount.CreateCloudBlobClient();
    var container = myClient.GetContainerReference("images-backup");

container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
    Console.ReadLine();
}
Enter fullscreen mode Exit fullscreen mode

This code will get our connection string from the App.config, create our client and a container named images-backup if it doesnโ€™t exist. We can go back inside of the portal to see if executed correctly.

Success! Weโ€™ve now accomplished the same task via the Azure Portal and C#.

Want more Azure Storage? Check out our quickstarts and tutorials!


We'll be posting articles every day in April, so stay tuned or jump ahead and check out more tips and tricks now.

Top comments (0)