https://github.com/mohammadahsan/azure-blob-storage
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.
https://github.com/mohammadahsan/azure-blob-storage
azure azure-sdk azure-storage azure-storage-blob blob-cont c-sharp cloud-storage file-upload microsoft-azure visual-studio windows-forms
Last synced: 7 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/mohammadahsan/azure-blob-storage
- Owner: mohammadahsan
- Created: 2017-03-02T15:39:41.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T10:39:22.000Z (over 8 years ago)
- Last Synced: 2025-01-14T05:12:54.442Z (9 months ago)
- Topics: azure, azure-sdk, azure-storage, azure-storage-blob, blob-cont, c-sharp, cloud-storage, file-upload, microsoft-azure, visual-studio, windows-forms
- Homepage:
- Size: 2.06 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Geting started with Azure Blob storage using Form builder
## Requirements
+ Azure subscription or free trial account
+ Create [Azure Storage Account](https://azure.microsoft.com/en-us/free/)
+ [Visual Studio 2015 or above] (https://www.visualstudio.com/)
+ [.NET Framework 4.5 or above] (https://www.microsoft.com/en-us/download/details.aspx?id=30653)
+ [Microsoft Azure Storage Connected Service] (https://marketplace.visualstudio.com/items?itemName=MicrosoftCloudExplorer.MicrosoftAzureStorageConnectedService)## Setting up The WorkSpace
1. Open Visual Studio2. Select **File->New->Project** from the main menu
3. On the **New Project** dialog, specify the options as highlighted in the following figure:

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**.
2. On the **Add Connected Service** dialog, select **Azure Storage**.
+ For visual studio 2015 select **Azure Storage** and then select **Configure**.
3. Select **Reenter your credentials**.
4. Enter Azure account **Email** and **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.
2. Fill out the **Create Storage Account** dialog box and then select **Create** button.
3. Choose the added **storage** in the list, and select **Add**.
4. The storage connected service appears under the **Connected Services** node of your project.
## 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
```

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:
``` C#
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.
2. Add a `button` from **toolbox** and name it as **Create Container**
3. Right Click the `button` and select **View Code**
4. Add this snippet inside `button`onclick Event.

```C#
// 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:
``` C#
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.

## Seeing what containers exist within the storage account.
1. Sign in to the [Azure portal](https://portal.azure.com/)
2. Select **SHOW MENU**, & choose **All Resources** and click on **Storage Account**.
3. Slide down to **Conatainer**
4. Under **_Blob Service_** Select **Containers**, _There is the List of Existing containers_.
## Uploading data to that container
1. Open the `Forms.cs` file from **Solution Explorer** in Visual Studio.
2. Add a `button` from **toolbox** and name it as **Upload**
3. Right Click the `button` and select **View Code<>**
4. Add this snippet inside `button`onclick Event.

```C#
// 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.

## 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**.
2. Under the **application Settings**, select **output type** as **console application**
### Listing the items in a container
1. Open the `Forms.cs` file from **Solution Explorer** in Visual Studio.
2. Add a `button` from **toolbox** and name it as **List Blobs**
3. Right Click the `button` and select **View Code**
4. Add this snippet inside `button`onclick Event.

``` C#
// 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.

## Deleting an item in that container
1. Open the `Forms.cs` file from **Solution Explorer** in Visual Studio.
2. Add a `button` from **toolbox** and name it as **delete blob**
3. Right Click the `button` and select **View Code**
4. Add this snippet inside `button`onclick Event.

``` C#
// 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.

## Deleting the container
1. Open the `Forms.cs` file from **Solution Explorer** in Visual Studio.
2. Add a `button` from **toolbox** and name it as **delete container**
3. Right Click the `button` and select **View Code**
4. Add this snippet inside `button`onclick Event.

``` C#
// 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.

## Deleting the storage account
1. Sign in to the [Azure portal](https://portal.azure.com/)
2. Select **SHOW MENU**, & choose **All Resources** and click on **Storage Account**.
3. Click on **Overview** on the top right corner click **Delete**
4. Enter **Name of the storage** you want to delete.
5. Successfule Deletion of the **Storage Account**
