we have utlise this link and others to upload a file from x++ to azure blob via SAS token. However, we want to achive the same via managed identity as the internal security has forbidden usage of Keys. I know that this involvs
I am unable to achieve step 4 via x++ code. Please help and also suggest any alternative OOB solution if applicable.
thanks.
Initially, I registered one application and granted Storage API permission in it as below:
Under storage account, I added "Storage Blob Data Contributor" role to above application like this:
In my case, I used below sample c# code to upload file to Azure Storage account:
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
namespace AzureBlobUploadApp
{
class Program
{
private static async Task Main(string[] args)
{
string tenantId = "tenantId";
string clientId = "appId";
string clientSecret = "secret";
string storageAccountName = "sridemostor1411";
string containerName = "sri";
string blobName = "logo.jpg";
string filePath = "C:\\test\\logo.jpg";
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
await UploadFileToBlobAsync(storageAccountName, containerName, blobName, filePath, credential);
}
private static async Task UploadFileToBlobAsync(string storageAccountName, string containerName, string blobName, string filePath, ClientSecretCredential credential)
{
string blobUri = $"https://{storageAccountName}.blob.core.windows.net/{containerName}/{blobName}";
var blobClient = new BlobClient(new Uri(blobUri), credential);
using FileStream fileStream = File.OpenRead(filePath);
await blobClient.UploadAsync(fileStream, new BlobHttpHeaders { ContentType = "application/octet-stream" });
Console.WriteLine("File uploaded successfully!");
}
}
}
Response:
To confirm that, I checked the same in Azure Portal where file uploaded successfully as below: