Upload File to Azure Blob Storage C#

Introduction

Azure Storage is a service provided by Microsoft to store the information, such as text or binary. You tin utilise this data to arrive available to the public or secure it from public admission. There are multiple ways I institute on the internet to upload the content to Azure Hulk Storage, like you lot can utilize shared keys, or employ a Connection string, or a native app.

Problem

Now, here is the problem. Suppose you have to create a Windows Service which will upload and download your files to the Azure Blob Storage. And as per the requirements, you don't have to use a shared key or connexion string or merely your customer didn't share it. The client wants to become it done by Active Directory Authentication. With the Native app, as near of the samples over the net, the user consent is required if your token expires and that's why information technology cannot be used in Windows Service.

Solution

After spending some time on information technology, I establish the right fashion to upload and download a file on Azure Hulk Storage. As I didn't observe any documentation or samples detail to this requirement over the internet, I am sharing it. Hope it will aid.

Create an application on Azure

Create an awarding of Web app/API blazon on the Azure portal.

Upload And Download File To Azure Blob Storage Using C#

Get Keys for Hallmark

Y'all need to have the awarding id, tenant/Directory Id, and Client Clandestine key.

Later on application creation, you volition see the below screen where yous have the application Id and from the Keys section, you can generate Client Clandestine key. See the below screens.

Upload And Download File To Azure Blob Storage Using C#

To get Directory/ Tenant Id.

Upload And Download File To Azure Blob Storage Using C#

Upload And Download File To Azure Blob Storage Using C#

Role assignment to your Azure application

Now, go to your storage account that you desire to use and assign your application Storage Hulk Data Owner/Contributor Role.

Upload And Download File To Azure Blob Storage Using C#

Upload And Download File To Azure Blob Storage Using C#

Actually, the above part gives you permissions to read/write/delete on Blob Management and Data. With Management permission, you tin can read/write/delete a container in the Blob and with Data, yous can read/write/delete content in it.

Upload And Download File To Azure Blob Storage Using C#

At present, you accept all the things you need to build an application. Let's see the code snippets.

Hallmark

The offset matter you need is to get an access token and you tin get it using the below method.

  1. static string  GetUserOAuthToken( cord  tenantId, string  applicationId, string  clientSecret)
  2.         {
  3. const string  ResourceId = "https://storage.azure.com/" ;
  4. const string  AuthInstance = "https://login.microsoftonline.com/{0}/" ;
  5. string  dominance = cord .Format(CultureInfo.InvariantCulture, AuthInstance, tenantId);
  6.             AuthenticationContext authContext =new  AuthenticationContext(authority);
  7.             var clientCred =new  ClientCredential(applicationId, clientSecret);
  8.             AuthenticationResult result = authContext.AcquireTokenAsync(
  9.                                                 ResourceId,
  10.                                                 clientCred
  11.                                                 ).Effect;
  12. render  result.AccessToken;
  13.         }

File Upload and Download Methods

Below is the code snippet to upload and download the file to Azure Blob Storage.

  1. public static class  AzureOperations {
  2.     #region ConfigParams
  3. public static string  tenantId;
  4. public static cord  applicationId;
  5. public static string  clientSecret;
  6.     

0 Response to "Upload File to Azure Blob Storage C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel