Lab 7 - Get Started With Azure Blob Storage and Visual Studio Connected Services (ASP - Net Core)
Lab 7 - Get Started With Azure Blob Storage and Visual Studio Connected Services (ASP - Net Core)
Lab 7: Get started with Azure Blob storage and Visual Studio
connected services (ASP.NET Core)
Azure Blob storage is a service that stores unstructured data in the cloud as objects or blobs. Blob
storage can store any type of text or binary data, such as a document, media file, or application
installer. Blob storage is also referred to as object storage.
This tutorial shows how to write ASP.NET Core code for some common scenarios that use Blob
storage. Scenarios include creating a blob container, and uploading, listing, downloading, and deleting
blobs.
1. Open Visual Studio. From the main menu, select File > New > Project.
2. In the New Project dialog box, select Web > ASP.NET Core Web
Application > AspNetCoreStorage. Then select OK.
3. In the New ASP.NET Core Web Application dialog box, select .NET
Core > ASP.NET Core 2.0 > Web Application (Model-View-Controller). Then
select OK.
3. In the Connected Services dialog box, select Cloud Storage with Azure Storage,
and then select Configure.
4. In the Azure Storage dialog box, select the Azure storage account to be used for this
tutorial. To create a new Azure storage account, select Create a New Storage
Account, and complete the form.
5. After selecting either an existing storage account or creating a new one, select Add.
Visual Studio installs the NuGet package for Azure Storage, and a storage
connection string to appsettings.json.
6. To allow the network able to access to manage the blob storage account, a
connection string need to generate from the azure portal and replace the current
connection string in the appsettings.json. The steps as below:
Go to the storage account and click the “Shared access signature” button.
Then, click on the “Generate SAS and connection string” button to generate the
connection string and then click on the copy button to copy the string.
c. Create a controller.
Estimation time for this section C: 10 Minutes
3. In the Add Scaffold dialog box, select MVC Controller - Empty, and
select Add.
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
ViewBag.Success = container.CreateIfNotExistsAsync().Result;
ViewBag.BlobContainerName = container.Name;
6. In Solution Explorer, right-click the Views folder. From the context menu,
select Add > New Folder. Name the new folder Blobs.
9. In the Add MVC View box, enter CreateBlobContainer for the view name, and
select Add.
11. In Solution Explorer, expand the Views > Shared folder, and
open _Layout.cshtml. Look for the unordered list that looks like this: <ul
class="nav navbar-nav">. After the last <li> element in the list, add the
following HTML to add another navigation menu item:
12. Run the application, and select Create Blob Container to see results similar to
the following screenshot:
When the blob container is created, upload files into that container. This section walks
through uploading a local file to a blob container.
5. After there is a blob reference, you can upload any data stream to it by calling
the blob reference object's UploadFromStream method.
using (var fileStream = System.IO.File.OpenRead(@"<file-to-upload>"))
{
blob.UploadFromStreamAsync(fileStream).Wait();
}
7. After the last <li> element in the list, add the following HTML to add another
navigation menu item:
8. Run the application, and select Upload blob. The word success! should appear.
9. Exercise 01: Modify the BlobsController.cs and store multiple items into the
azure blob storage in one time. The example output as below:
This section illustrates how to list the blobs in a blob container. The sample code
references the test-blob-container created in the section - Create a blob container.
The following code snippet enumerates all the blobs in a blob container. Each
blob is cast to the appropriate object, based on its type. Its name (or URI in the
case of a CloudBlobDirectory) is added to a list.
7. From the context menu, select Add > View. In the Add View dialog box,
enter ListBlobs for the view name, and select Add.
8. Open ListBlobs.cshtml, and replace the contents with the following code:
10. Run the application, and select List blobs to see results similar to the following
screenshot:
g. Download blobs
Estimation time for this section G: 5 Minutes.
This section illustrates how to download a blob. You can either persist it to local storage
or read the contents into a string. The sample code references the test-blob-container
created in the section - Create a blob container.
That object is then persisted to a local file. (Change <local-file-name> to the fully
qualified file name representing where the blob is to be downloaded.)
6. The following shows the completed ListBlobs method (with a fully qualified path
for the local file being created):
7. In Solution Explorer, expand the Views > Shared folder, and open _Layout.cshtml.
After the last <li> element in the list, add the following HTML to add another
navigation menu item:
<li><a asp-area="" asp-controller="Blobs" asp-action="DownloadBlob">
Download blob</a></li>
8. Run the application, and select Download blob to download the blob. The blob
specified in the CloudBlobContainer.GetBlockBlobReference method call
downloads to the location specified in the File.OpenWrite method call. The
text success! should appear in the browser.
9. Exercise 02: Modify the code to download the picture to the /wwwroot/images
folder and display it into the download.cshtml page. The sample of output as
below:
h. Delete blobs.
Estimation time for this section H: 5 Minutes.
blob.DeleteAsync().Wait();
7. In Solution Explorer, expand the Views > Shared folder, and open _Layout.cshtml.
After the last <li> element in the list, add the following HTML to add another
navigation menu item:
<li><a asp-area="" asp-controller="Blobs" asp-action="DeleteBlob">
Download blob</a></li>
8. Run the application, and select Delete blob to delete the blob specified in
the CloudBlobContainer.GetBlockBlobReference method call. The
text success! should appear in the browser. Select the browser's Back button, and
then select List blobs to verify that the blob is no longer in the container.
i. Exercise 03: Combine the SQL Database together with the Blob
Storage in a system.
Estimation time for this section I: 45 Minutes.
1. Open Visual Studio 2017, and go to the File > New > Project
From the left menu, select Installed > Visual C# > .NET Core.
Select ASP.NET Core Web Application.
Enter EFGetStarted.AspNetCore.NewDb for the name and click OK.
In the New ASP.NET Core Web Application dialog:
Make sure that .NET Core and ASP.NET Core 2.1 are selected in the drop-
down lists
Select the Web Application (Model-View-Controller) project template
Make sure that Authentication is set to No Authentication
Uncheck the Configure for HTTPS
Click OK
3. Enter Model.cs as the name and click OK. Replace the contents of the file with
the following code:
In Startup.cs add the following using statements. Add the following yellow
highlighted code to the ConfigureServices method:
Add-Migration InitialCreate
Update-Database
6. Create a controller. Scaffold a controller and views for the Blog entity.
Right-click on the Controllers folder in Solution Explorer and select Add >
Controller.
Select MVC Controller with views, using Entity Framework and click Add.
Set Model class to Blog and Data context class to BloggingContext.
Click Add
Open the Shared folder and find the _Layout.schtml add the below line in the
navigational bar coding:
7. Lastly, run the application by using the Debug > Start Without Debugging. If you
able to see the below application running, it means that you are successfully create a
system with the SQL database.
9. Once you successfully to publish to the azure cloud, continue the steps in this
exercise to create a blob storage for this system. You should be able to add a
file to the Azure Blob Storage through this system.
j. Clean up resources.
Estimation time for this section J: 10 Minutes.
1. In the preceding steps, you created Azure resources in a resource group. If you
don't expect to need these resources in the future, you can delete them by deleting
the resource group.
2. From the left menu in the Azure portal, select Resource groups and then
select myResourceGroup.
3. On the resource group page, make sure that the listed resources are the ones you
want to delete.
4. Select Delete, type myResourceGroup in the text box, and then select Delete.
Summary:
In this tutorial, we learned how to build a system with the Azure Blob Storage Service. We
also learned how to combine the existing system (with SQL database) with the Azure Blob
Storage Service.