Tags

, , , , , ,

Hello Everyone,

This Post is to make you understand about how to quick start with Microsoft’s Azure Storage Services. Basically i am gonna explain you about how to create a container, then upload a blob file into the container, then download the blob file and finally deleting a blob file. We will be seeing this one after the other.

As many of them might be already knowing what a blob is? But let me tell you Blob stands for Binary Long Object which can store files of large size. It is again classified into Block Blob and Page Blob. Block Blob saves 200GB of files and Page Blob can stores 1TB of files.

So this sample is prepared on Windows 10 OS, Visual Studio 2013 with update 4. You also need to have an azure account.

OK, lets start with creating a container in the Azure Storage Account using a Windows Store app. Open Visual Studio basically it will be like this.

Visual Studio Home Page

Create a new project, windows apps, and give some name (say blobstoragedemo).

Create New Project
Some requirements in order to communicate with Microsoft Azure, we need to add some libraries.
Go to references and click on manage nuget packages. Search for Windows Azure Storage. You will get the first result as above and install the package.

[3] Nuget Packages

After downloading, click on I Accept to install the nuget packages.

[4] Accepting Licences

After accepting the above library files will be added into the project.

[5] Reference Files Added

Generally, in order to upload and download the files into a blob storage, we need to understand what exactly blob is and where it is stored.
In Microsoft Azure, we have Storage as one of the service under Data Services Category.
In this service, we create a storage account and then create containers to insert, upload, and download the files nothing but the blobs into the containers.
In this demo, we will be creating container from our application, then upload a blob, download a file and then delete a blob file from Azure.
OK, moving back to Visual Studio, now one more thing we need to take care is adding namespaces to the mainpage.xaml
In the Solution Explorer which is at the right side of the Visual Studio IDE, open mainpage.xaml.cs
You will find this one.
Now, add the above namespaces to the existing namespaces.

[6] Adding namespaces

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;

Now, we need to create a container if there is no container in the Microsoft Azure. So in the Visual Studio click on mainpage.xaml and add a button. Using this button we will be creating a container in the Azure.

Give a name (nothing but a label) in the properties bar, content (say create container). Generate a button click event by double click on the button in mainpage.xaml page.

Open Microsoft Azure and then at the bottom we have a + button. Click on it to create a storage account.

[7] Creating Storage Account

After creating a storage account click on manage access keys to get the primary and secondary key associated with storage account.

[8] Access Keys - Primary Key associated with Storage account

Now, we have a storage account. Go to Visual Studio and generate a button click event and write the above code to create a container.

private async void createButton_Click(object sender, RoutedEventArgs e)
{
StorageCredentials sc = new StorageCredentials(“blobstoragedemo1”, “O2c3neazQlou0wm1q2/25EaaV7eHlj+9SeurjsvYQs5omkbaUh4+CqpjUtTVB603ydJ5fsm5foxZMOHEwWGlNQ==”);
CloudStorageAccount storageAccount = new CloudStorageAccount(sc,true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(“democontainer”);
await container.CreateIfNotExistsAsync();
MessageDialog md = new MessageDialog(“Your Container is Created”);
await  md.ShowAsync();
}

What this code does is using the storage account credentials it will create a container with the name given, if there is no container already created.

Now, run your local machine and find whether a container is created!You can see that a container is created in the Azure Portal. This is how we have successfully created a container using a windows app.

Running the app - Creating a Container

Successfully created a container.

Container Visible on Azure

Now, let’s upload files into the container.

So, again go back to Visual Studio, open mainpage.xaml from the same project. Insert a button on the XAML Page and add a name “Upload” to the button.

Generate the button click event and insert the above code into the method.

private async void uploadButton_Click(object sender, RoutedEventArgs e)

{

StorageCredentials sc = new StorageCredentials(“blobstoragedemo1”, “O2c3neazQlou0wm1q2/25EaaV7eHlj+9SeurjsvYQs5omkbaUh4+CqpjUtTVB603ydJ5fsm5foxZMOHEwWGlNQ==”);

CloudStorageAccount storageaccount = new CloudStorageAccount(sc, true);

CloudBlobClient blobclient = storageaccount.CreateCloudBlobClient();

CloudBlobContainer container = blobclient.GetContainerReference(“democontainer”);

CloudBlockBlob blockblob = container.GetBlockBlobReference(“demo.jpg”);

FileOpenPicker openPicker = new FileOpenPicker();

openPicker.ViewMode = PickerViewMode.Thumbnail;

openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

openPicker.FileTypeFilter.Add(“.jpg”);

openPicker.FileTypeFilter.Add(“.jpeg”);

openPicker.FileTypeFilter.Add(“.png”);

 

StorageFile file = await openPicker.PickSingleFileAsync();

await blockblob.UploadFromFileAsync(file);

}

MessageDialog md = new MessageDialog(“File has been uploaded Successfully”);

await md.ShowAsync();

}

Now, run your application and you will see an upload button using which you can select the pic and then upload it to blob storage.

Once it is uploaded you will get a success message. To verify go to azure portal open the storage account and then container, you can see your blob file uploaded into the container.

Uploading Blob File

Picking File from Folder

Blob File Uploaded Successfully

Blob file uploaded into Container in Azure Portal

Go to mainpage.xaml and add one more button with name “Download” and adjust the font size as per your preferences. Now, generate a click event and write the code for the download in that event.

So the code for the Download will be the above one

private async void downloadButton_Click(object sender, RoutedEventArgs e)

{

StorageCredentials sc = new StorageCredentials(“blobstoragedemo1”, “O2c3neazQlou0wm1q2/25EaaV7eHlj+9SeurjsvYQs5omkbaUh4+CqpjUtTVB603ydJ5fsm5foxZMOHEwWGlNQ==”);

CloudStorageAccount storageaccount = new CloudStorageAccount(sc, true);

CloudBlobClient blobclient = storageaccount.CreateCloudBlobClient();

CloudBlobContainer container = blobclient.GetContainerReference(“democontainer”);

CloudBlockBlob blockblob = container.GetBlockBlobReference(“demo.jpg”);

FileSavePicker savePicker = new FileSavePicker();

savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

// Dropdown of file types the user can save the file as

savePicker.FileTypeChoices.Add(“Picture”, new List<string>() { “.jpg” });

// Default file name if the user does not type one in or select a file to replace

savePicker.SuggestedFileName = “New Blob File”;

StorageFile file = await savePicker.PickSaveFileAsync();

await blockblob.DownloadToFileAsync(file);

MessageDialog md = new MessageDialog(“File has been Downloaded Successfully”);

await md.ShowAsync();

}

OK, once you run your app you will see a download button, click on it you will get a save option to save the blob file.

Thus, we have seen how to download a blob file from Azure.

Download Blob File using App from Azure

Saving the Blob File

File Downloaded Successfully

Go to mainpage.xaml and add one more button with name “Delete” and generate a click event. By clicking this button we will be able to delete a blob file from the Azure Storage or the Container.

After the design part, add the above code in the mainpage.xaml.cs file

private async void deleteButton_Click(object sender, RoutedEventArgs e)

{

StorageCredentials sc = new StorageCredentials(“blobstoragedemo1”, “O2c3neazQlou0wm1q2/25EaaV7eHlj+9SeurjsvYQs5omkbaUh4+CqpjUtTVB603ydJ5fsm5foxZMOHEwWGlNQ==”);

CloudStorageAccount storageaccount = new CloudStorageAccount(sc, true);

CloudBlobClient blobclient = storageaccount.CreateCloudBlobClient();

CloudBlobContainer container = blobclient.GetContainerReference(“democontainer”);

CloudBlockBlob blockblob = container.GetBlockBlobReference(“demo.jpg”);

await blockblob.DeleteAsync();

MessageDialog md = new MessageDialog(“File has been Deleted Successfully”);

await md.ShowAsync();

}

Now run your app and click on delete button to delete the file from the Blob Storage

This is how we create a blob storage account, then a container , then uploading, downloading and deleting blob files from container in a Azure Storage Account.

Delete Blob File

Blob File Deleted Successfully

Advertisements