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.
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.
To get Directory/ Tenant Id.
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.
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.
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.
- static string GetUserOAuthToken( cord tenantId, string applicationId, string clientSecret)
- {
- const string ResourceId = "https://storage.azure.com/" ;
- const string AuthInstance = "https://login.microsoftonline.com/{0}/" ;
- string dominance = cord .Format(CultureInfo.InvariantCulture, AuthInstance, tenantId);
- AuthenticationContext authContext =new AuthenticationContext(authority);
- var clientCred =new ClientCredential(applicationId, clientSecret);
- AuthenticationResult result = authContext.AcquireTokenAsync(
- ResourceId,
- clientCred
- ).Effect;
- render result.AccessToken;
- }
File Upload and Download Methods
Below is the code snippet to upload and download the file to Azure Blob Storage.
- public static class AzureOperations {
- #region ConfigParams
- public static string tenantId;
- public static cord applicationId;
- public static string clientSecret;
0 Response to "Upload File to Azure Blob Storage C#"
Post a Comment