[!INCLUDE storage-selector-blob-include]
This tutorial will show you how to use Azure Blob Storage with ASP.NET and Visual Studio 2017. It will show how to upload, list, download, and delete blobs in the context of a simple Web API project that works with images.
I am trying to create Locally Blob Container using Microsoft Azure Storage Explorer. I am trying to create Locally Blob Container using Microsoft Azure Storage. In this article you will see about ‘Azure Storage Explorer’ an extension that supports the adminitration of blob objects in Microsft Azure. Here are the steps to set up ‘Azure Storage Explorer’ in your machine. Install Visual studio code in your machine. VS code can be downloaded from here. Microsoft Azure Storage Explorer is a standalone app that makes it easy to work with Azure Storage data on Windows, macOS, and Linux. It allows you to efficiently connect and manage your Azure storage service accounts and resources across subscriptions.You can create, delete, view, and edit resources in Azure Storage, Azure Cosmos DB, and Data Lake Storage. When you upload a VHD to a Managed Disk, Storage Explorer is leveraging the power and speed of AzCopy v10 to quickly get your data into Azure. Storage Explorer’s support for managed disks also includes the ability to create snapshots of, copy, download, and delete your managed disks.
Get the sample project
To begin, clone the sample project from GitHub. The rest of the tutorial will work with this project.
If you’re unfamiliar with git, you can also download the project as a zip file. In the GitHub repository, there is a green button called Clone or download. You can click that and select Download ZIP:
Set up your environment
Ensure that you have Visual Studio 2017 installed with the ASP.NET and web development and Azure Development workloads. This will give you all of the tools that you will need for this tutorial.
![Microsoft Microsoft](/uploads/1/3/4/7/134779852/686100041.png)
Run the application locally
![Blob Blob](/uploads/1/3/4/7/134779852/405991164.png)
First, start the Azure Storage Emulator by pressing the Windows key on your keyboard and searching for Azure Storage Emulator:
This will start the Azure Storage Emulator on your machine, and open a command prompt which you can use to control it from there.
Open the sample application in Visual Studio 2017. Start it by pressing f5 in Visual Studio. This will launch the web app on
http://localhost:58673/
so that you can interact with it or click the API tab to browse the reference.Next, enter the following in your browser to test that your GET API is working:
You should see a response that is similar to the following (if viewed as JSON):
Basic blob operations
The following sections will walk through the basics of interacting with blob storage within an ASP.NET Web API project:
- Storage account and container creation, done in a constructor.
- Uploading a file on the server to a blob.
- Download a file from a blob to the server.
- Deleting a blob.
- Listing metadata about each blob.
These five basic operations should be enough for you to do a large amount of tasks with Blob Storage.
Create a storage account and container
In the constructor, a Storage Account and Container are created as follows:
- Parse the connection string (which could be for local development or an actual connection string).
- Create a client, which could then be used to create multiple containers.
- Create a container by name if it doesn’t already exist, and write diagnostics based on if it already did.
Every blob in Azure Storage must reside in a container. The container name is part of how you uniquely identify any blob in Azure Storage, and is your entry point for any blob operation.
Container names must also be a valid DNS name. To learn more about containers and naming, see Naming and Referencing Containers, Blobs, and Metadata.
Upload to a blob
The
UploadFromStreamAsync
method is a general-purpose API for uploading from anything which can be exposed as a stream to a blob. There are other APIs available such as UploadTextAsync
, UploadFromFileAsync
, and UploadFromByteArrayAsync
.Download a blob
The
DownloadToStreamAsync
method is a general-purpose API for downloading to anything that can be exposed as a stream. There are other APIs available such as DownloadTextAsync
, DownloadToFileAsync
, and DownloadToByteArrayAsync
.Azure Blob Storage Explorer
Delete a blob
What Is Blob Storage Azure
DeleteIfExistsAsync
will delete a blob if it exists. It returns a Task<bool>
, where true
indicates that it successfully deleted the blob, and false
indicates that the container did not exist. In this case, we can simply ignore it.You can also use the
DeleteAsync
method, but you’ll need to handle any exceptions if the blob doesn’t exist yourself.List blobs in a container
To list blobs in a container, call
ListBlobs
on the container as done in the HTTP GET call from the API controller:You’ve likely noticed that an
is
expression is needed on the blob
item in the for
loop. This is because each blob
is of type IListBlobItem
, which is a common interface implemented by multiple blob types. This tutorial uses Block Blobs, which are the most common kinds of blobs. If you are working with Page Blobs or Blob Directories, you can cast to those types instead.If you have a large number of blobs, you may need to use other listing APIs such as ListBlobsSegmentedAsync.
Basic blob operations together
Now that you’ve run the the sample application, take a look at the basic operations on Blobs that it performs. All code for interacting with blobs is in a single file,
BlobsController.cs
.Run the sample application in Azure
To publish the application this sample application to Azure, you’ll need an Azure Storage account. The easiest way to do this is with Visual Studio Connected Services.
Create a storage account with Connected Services
- In Solution Explorer, right-click on Connected Services.
- From the context menu, select Add > Connected Service.
- In the Connected Services dialog box, select Cloud Storage with Azure Storage, and then select Create a new Storage Account.
- In the Azure Storage dialog box, complete the form. Under Resource Group, select Create new. Under Location, pick anything you like.
- Once your account has been created, click Add. This will install all the necessary dependencies that you’ll need to get started.
Add the connection string to the release configuration file.
After you’ve added a service reference through Connected Services, it will have placed a true connection string in the
appSettings
section of your Web.config
like so:Replace the
'UseDevelopmentStroage=true'
value with the real connection string generated by Connected Services.Publish to Azure
Finally, you can publish to Azure!
- In Solution Explorer, right-click and select Publish.
- Under Microsoft Azure App Service, ensure that Create New is selected and press the Publish button.
- Fill out the form - all fields should be automatically populated with values that will allow you to create a new App Service web app with the project.
- Click Create.
After it creates the App Service web app, and the application is fully published, a browser window will open and you can interact with the sample app live over the internet!
Next steps
Now that you’ve learned the basics of Blob storage, follow these links to learn more.
Microsoft Azure Storage Explorer
- Microsoft Azure Storage Explorer is a free, crossplatform application which allows you to work visually with Azure Storage Data.