Introduction
Hi, I am in this blog going to share some code examples to upload images to Azure cloud storage(Blob Storage). Here we will discuss each stuff given in the code snippet to upload photos.
Post Images To Azure Blob Storage |
Getting Started
I know there will be various ways to upload images into Azure Cloud Storage but here in the below code sample uses two Microsoft client libraries that is Microsoft.Azure.Storage.Blob and Microsoft.Azure.Storage.DataMovement. You need to download the client libraries from NuGet using the below code. How to download the libraries from NuGet, at the end of the blog is given.
Code Sample string storageConnectionString = "Azure Storage Account Connection String";
//Instance of Azure Cloud Storage Account
CloudStorageAccount storageacc = CloudStorageAccount.Parse("Storage Connection String");
//Instance of Azure blob
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
// Get a reference to a CloudBlobContainer object in this account.
CloudBlobContainer container = blobClient.GetContainerReference("Name of Azure Container");
//will create a Azure container if not exists
container.CreateIfNotExists();
TransferCheckpoint checkpoint = null;
SingleTransferContext context = GetSingleTransferContext(checkpoint);
// File name with azure storage path
CloudBlockBlob blockBlob = container.GetBlockBlobReference("Path of image file");
//specifies type of pic upload
blockBlob.Properties.ContentType = "image/jpg";
try
{
//path of the upload photos include upload image name
string sourcepath= "";
await TransferManager.UploadAsync(sourcepath, blockBlob, null, context);
}
catch (Exception ex)
{
}
Post Images To Azure Blob Storage
The above code example is to upload images into Azure storage. The CloudStorageAccount class is used to specify to represent a Microsoft Azure Storage account. It comes under Microsoft.Azure.Storage.
In the next line, the object of the CloudBlobClient class is created which provides a client-side logical representation of Microsoft Azure Blob storage. This object is the root object for all operations on the blob service for this particular account.
The instance of CloudBlobContainer class is created to represent a container in the Microsoft Azure Blob service. This instance can be used to create the container on the service, list blobs, delete the container, etc. This operation does not make a call to the Azure Storage service. It neither creates the container on the service nor validates its existence.
The object of TransferCheckpoint class represents a checkpoint from which a transfer may be resumed and continued and comes under Microsoft.Azure.Storage.DataMovement namespace.
The SingleTransferContext call represents the context for a single transfer and provides additional runtime information about its execution and also comes under the DataMovement namespace.
The object of CloudBlockBlob represents a blob that is uploaded as a set of blocks and comes under a blob that is uploaded as a set of blocks. Namespace.
The UploadAsync methods of class TransferManager (which comes under the Microsoft.Azure.Storage.DataMovement namespace) upload a file to Azure Blob Storage.
Code Example string storageConnectionString = "Azure Storage Account Connection String";
CloudStorageAccount storageacc = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("test");
container.CreateIfNotExists();
TransferCheckpoint checkpoint = null;
SingleTransferContext context = GetSingleTransferContext(checkpoint);
// test / media
CloudBlockBlob blockBlob = container.GetBlockBlobReference("media/1312300000.jpg");
blockBlob.Properties.ContentType = "image/jpg";
try
{
//path of the upload photos include upload image name
string sourcepath= "";
//Upload a file to Azure Blob Storage.
await TransferManager.UploadAsync(sourcepath, blockBlob, null, context);
}
catch (Exception ex)
{
}
Post Images To Azure Blob Storage
Code of Function GetSingleTransferContext public static SingleTransferContext GetSingleTransferContext(TransferCheckpoint checkpoint)
{
SingleTransferContext context = new SingleTransferContext(checkpoint);
context.ProgressHandler = new Progress<TransferStatus>((progress) =>
{
Console.Write("\rBytes transferred: {0}", progress.BytesTransferred);
});
return context;
}
Follow the below steps to download the libraries.
In Microsoft Visual Studio, go to the Solution Explorer.
Right-click on your project name.Then click on Manage NuGet Packages…
Manage NuGet Packages will be displayed, click on the Browse tab.
Copy this Microsoft.Azure.Storage.Blob and paste it into the search box.
The library will be displayed in the list, select and click install.
Again copy this Microsoft.Azure.Storage.DataMovement and past it into the search box.
The library will be displayed in the list, select and click install.
Summary
I hope the code example and the description about the used stuff to upload images to the Azure cloud storage may help you.
Related Articles
- Backup SQL Database to the Azure Blob
- Restore Database to Microsoft Azure Blog
- Create service bus queue in Azure portal
Thanks