Upload Files to Azure Blob Storage C# Mvc
Azure Blob Storage offers a simple way to store files such as images or JSON data in the deject. This tutorial volition teach y'all how to write information to Azure Hulk Storage from a C# application.
Creating a Storage Account
Before jumping into Visual Studio, we need to create an Azure Storage Business relationship. In the Azure portal, create a new resource and create a Storage Account. It'due south usually listed in the list of popular products.
Showtime, select your subscription and resources group. We're going to create a new resource group and proper noun it blobstorage.
Side by side, we demand to provide a globally unique storage account name. We become with blobstoragedemo3. We choose West Europe every bit the region and employ the standard operation tier. We cull Locally-redundant storage (LRS). Both are the cheapest options at the time of recording this tutorial.
Performance and back-up are out of the scope of this tutorial. Brand sure you understand the differences in terms of cost as I am not responsible for your spending.
In the Advanced tab, you can enable and disable different options. For case, you can disable the public blob access. That's what we practice for this tutorial. For everything else, nosotros use the default selection.
The other tabs comprise additional configuration options that we don't demand to change for this tutorial. We click on the Review & Create button to review our service configuration and create the Azure Storage Account.
Creating a Container
When Azure has completed the operation, we open the container'south menu and create a container that volition hold our data.
We click to open up the New Container dialog in the toolbar and provide a name. We cull fileupload as the container name. The name has to be unique inside the storage account.
We click on Create and await for Azure to cease the operation.
Before nosotros start development, we open the Access keys menu and click on the Show keys toolbar action. We then copy the connection cord of key1 into the clipboard.
Implementing the File Upload
In Visual Studio, I created a C# Console application.
The new .NET 6 projection templates utilize top-level statements. If you aren't familiar with the new .NET 6 features, lookout this video.
Showtime, we open the NuGet package director and install the Azure.Storage.Blobs package.
After the installation is completed, we open the Program.cs again and ascertain two variables. Commencement, we create a blobStorageConnectionString variable and insert the connection string we copied from the Azure portal.
using Azure.Storage.Blobs; Panel.WriteLine("Starting..."); var blobStorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=blobstoragedemo3;AccountKey=XYZ;EndpointSuffix=core.windows.net"; var blobStorageContainerName = "fileupload"; var container = new BlobContainerClient(blobStorageConnectionString, blobStorageContainerName); var blob = container.GetBlobClient("nature.jpg"); var stream = File.OpenRead("nature.jpg"); wait blob.UploadAsync(stream); Panel.WriteLine("Upload completed."); Second, we create a blobStorageContainerName variable and assign the container'southward name, in our example, fileupload.
Next, we add a using statement for the Azure.Storage.Blobs namespace. Below the variable definitions, we define a container variable and assign an example of the BlobContainerClient. The first argument is the connection string, and the second argument is the container name.
Next, we create a blob variable and telephone call the GetBlobClient method on the container object. As the only argument, nosotros provide the desired name of the blob.
In this tutorial, I load an image from the hard bulldoze, just you could as well employ any other stream or string and salve information technology to the blob. I create a stream variable and use the File.OpenRead method and provide the name of the file resource.
Now, we await the hulk.UploadAsync method and provide the stream as its statement. This line volition finally upload the information onto the storage account.
Running the awarding
Now let's build and run the application.
At least we don't get whatsoever exceptions, and the program completes with the "Upload completed" message in the console.
Inspecting the Data in the Azure Portal
Now that we uploaded the data to the Azure storage account permit'due south open up the Azure portal to verify the upload. Open the container'southward menu in the Storage business relationship and click on the fileupload container.
A list with all the blobs appears. Nosotros click on the nature.jpg hulk to go more details and download it using the toolbar action.
The browser downloads the file to the local deejay. I open it, and we run across this beautiful image.
What'south Adjacent?
Azure Blob Storage offers more advanced features for different scenarios. Y'all can read about it on the Azure Blob Storage official documentation.
beckersigntearame.blogspot.com
Source: https://www.claudiobernasconi.ch/2022/02/25/how-to-upload-a-file-to-azure-blob-storage-dotnet6/
0 Response to "Upload Files to Azure Blob Storage C# Mvc"
Post a Comment