- Azure subscription or free trial account
- Create Azure Storage Account
- [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)
-
Open Visual Studio
-
Select File->New->Project from the main menu
-
On the New Project dialog, specify the options as highlighted in the following figure:
-
In the Solution Explorer, right-click the reference node, and from the context menu, select Add->Connected Service.
-
On the Add Connected Service dialog, select Azure Storage.
-
Choose the Create a New Storage Account button at the bottom of the Azure Storage dialog box.
-
Fill out the Create Storage Account dialog box and then select Create button.
-
The storage connected service appears under the Connected Services node of your project.
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>
Replace account-name with the name of your storage account
,
Replace account-key with your account access key
:
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
// 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.
-
Sign in to the Azure portal
-
Select SHOW MENU, & choose All Resources and click on Storage Account.
-
Under Blob Service Select Containers, There is the List of Existing containers.
// 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.
-
In the Solution Explorer, right-click the project, and from the context menu, select properties.
-
Under the application Settings, select output type as console application
// 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.
// 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.
// 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.
-
Sign in to the Azure portal
-
Select SHOW MENU, & choose All Resources and click on Storage Account.