Skip to content

Designed for the official Microsoft launch of Azure Blob Storage. This project demonstrates how to set up Azure Blob Storage using a Windows Forms application, integrating Microsoft’s Azure Storage Connected Service in Visual Studio. It covers creating and managing blob containers, uploading files, listing items, and deleting containers.

Notifications You must be signed in to change notification settings

mohammadahsan/Azure-Blob-Storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 

Repository files navigation

Geting started with Azure Blob storage using Form builder

Requirements

Setting up The WorkSpace

  1. Open Visual Studio

  2. Select File->New->Project from the main menu

  3. On the New Project dialog, specify the options as highlighted in the following figure:

Win Forms Creation 4. Select OK.

Use Connected Services to connect to an Azure storage account

  1. In the Solution Explorer, right-click the reference node, and from the context menu, select Add->Connected Service. Connecting Azure Storage Account

  2. On the Add Connected Service dialog, select Azure Storage.

  • For visual studio 2015 select Azure Storage and then select Configure. Adding Account
  1. Select Reenter your credentials. Reenter Credentials

  2. Enter Azure account Email and password email & password

Creating an Azure Blob Storage account (non-classic version)

  1. Choose the Create a New Storage Account button at the bottom of the Azure Storage dialog box. creating a storage

  2. Fill out the Create Storage Account dialog box and then select Create button. Fill Form

  3. Choose the added storage in the list, and select Add. Add Storage

  4. The storage connected service appears under the Connected Services node of your project. Added Service

Configure your storage connection string

To configure your connection string, open the app.config file from Solution Explorer in Visual Studio. Add the contents of the appSettings element shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <!-- Azure Storage: blobstoragetest1 -->
        <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=blobstoragetest1;AccountKey=Nq2Z5CGL+vT5RrbezP/TboH0XzYzmvGDs1OGHM1nTj8J94MZc/XUVbUCY9arpp5xx/227yBa+SmGDXPx1obnqg==" />
    </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

App settings

Replace account-name with the name of your storage account, Replace account-key with your account access key:

Add namespace declarations

Add the following using statements to the top of the form1.cs file:

using Microsoft.Azure; // Namespace for CloudConfigurationManager
using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types

Creating a container in that storage account

  1. Open the Forms.cs file from Solution Explorer in Visual Studio. open form

  2. Add a button from toolbox and name it as Create Container container

  3. Right Click the button and select View Code view Code

  4. Add this snippet inside buttononclick Event. code

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container. (Replace mycontainer with name of your container)
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Create the container if it doesn't already exist.
container.CreateIfNotExists();

By default, the new container is private, meaning that you must specify your storage access key to download blobs from this container. If you want to make the files within the container available to everyone, you can set the container to be public using the following code:

container.SetPermissions(
    new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

Debug and run the Code, By Clicking on the button Create Container will create a Container on the Storage. form click1 Generated

Seeing what containers exist within the storage account.

  1. Sign in to the Azure portal

  2. Select SHOW MENU, & choose All Resources and click on Storage Account. menu

  3. Slide down to Conatainer slide

  4. Under Blob Service Select Containers, There is the List of Existing containers. containers

Uploading data to that container

  1. Open the Forms.cs file from Solution Explorer in Visual Studio. open form

  2. Add a button from toolbox and name it as Upload upload button

  3. Right Click the button and select View Code<> view Code

  4. Add this snippet inside buttononclick Event. code

// Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.Replace 'mycontainer' with name of your container
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            // Retrieve reference to a blob named "myblob".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

            // Create or overwrite the "myblob" blob with contents from a local file. (Replace C:\users\.... With your file path)
            using (var fileStream = System.IO.File.OpenRead(@"C:\Users\DELL\Desktop\icon48.png"))
            {
                blockBlob.UploadFromStream(fileStream);
            }

Debug and run the Code, By Clicking on the button upload will upload on the Storage. upload click Generated

Seeing items are inside of that container

First Setting the console to see output

  1. In the Solution Explorer, right-click the project, and from the context menu, select properties. properties

  2. Under the application Settings, select output type as console application console application

Listing the items in a container

  1. Open the Forms.cs file from Solution Explorer in Visual Studio. open form

  2. Add a button from toolbox and name it as List Blobs upload button

  3. Right Click the button and select View Code view Code

  4. Add this snippet inside buttononclick Event. code

// Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container. (replace 'mycontainer' with your container name) 
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            // Loop over items within the container and output the length and URI.
            foreach (IListBlobItem item in container.ListBlobs(null, false))
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;

                    Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);

                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob pageBlob = (CloudPageBlob)item;

                    Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);

                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory directory = (CloudBlobDirectory)item;

                    Console.WriteLine("Directory: {0}", directory.Uri);
                }

Debug and run the Code, By Clicking on the button list blobs will list the contents of the container on console. list click listed

Deleting an item in that container

  1. Open the Forms.cs file from Solution Explorer in Visual Studio. open form

  2. Add a button from toolbox and name it as delete blob del button

  3. Right Click the button and select View Code view Code

  4. Add this snippet inside buttononclick Event. code

// Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container. (replace 'mycontainer' with container name
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            // Retrieve reference to a blob. Replace 'myblob' with item name you want to delete.
            CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

            // Delete the blob.
            blockBlob.Delete();

Debug and run the Code, By Clicking on the button delete blob will delete the item on the Storage. delete click deleted

Deleting the container

  1. Open the Forms.cs file from Solution Explorer in Visual Studio. open form

  2. Add a button from toolbox and name it as delete container del c button

  3. Right Click the button and select View Code view Code

  4. Add this snippet inside buttononclick Event. code

// Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container. (replace 'mycontainer' with the container name.
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

            // Delete the container.
            container.DeleteIfExists();

Debug and run the Code, By Clicking on the button delete conotainer will delete the container from the Storage. del con click deleted

Deleting the storage account

  1. Sign in to the Azure portal

  2. Select SHOW MENU, & choose All Resources and click on Storage Account. menu

  3. Click on Overview on the top right corner click Delete delete

  4. Enter Name of the storage you want to delete. name

  5. Successfule Deletion of the Storage Account successful

About

Designed for the official Microsoft launch of Azure Blob Storage. This project demonstrates how to set up Azure Blob Storage using a Windows Forms application, integrating Microsoft’s Azure Storage Connected Service in Visual Studio. It covers creating and managing blob containers, uploading files, listing items, and deleting containers.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published