skip to Main Content

I’m trying to follow the example to upload a file to Azure Data Storage as mentioned in the documentation : https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-dotnet?tabs=visual-studio%2Cmanaged-identity%2Croles-azure-portal%2Csign-in-azure-cli%2Cidentity-visual-studio

Following is my code:

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.IO;
using Azure.Identity;

// TODO: Replace <storage-account-name> with your actual storage account name
var blobServiceClient = new BlobServiceClient(
        new Uri("https://[some azure storage]"),
        new DefaultAzureCredential());

// Set container name
string containerName = "data";

// Get container
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

// Create a local file in the ./data/ directory for uploading and downloading
string localPath = "data";
Directory.CreateDirectory(localPath);
string fileName = "testupload" + Guid.NewGuid().ToString() + ".txt";
string localFilePath = Path.Combine(localPath, fileName);

// Write text to the file
await File.WriteAllTextAsync(localFilePath, "Hello, World!");

// Get a reference to a blob
BlobClient blobClient = containerClient.GetBlobClient(fileName);

Console.WriteLine("Uploading to Blob storage as blob:nt {0}n", blobClient.Uri);

// Upload data from the local file
await blobClient.UploadAsync(localFilePath, true);

But I’m getting an error message that the request is not authorized.

Error message:

Azure.RequestFailedException: 'This request is not authorized to perform this operation using this permission.

I have Contributor role (which based on description is Grant full access to manage all resources ….), is this role still not enough to perform the operation?

enter image description here

2

Answers


  1. Make sure you have to change the Network Access to Enable Public to All, if you’re not using VPN or dedicated Network to access Azure Environment.

    Login or Signup to reply.
  2. I tried in my environment and got below results:

    Initially I tried same code in my environment and got same error

    Console:

    enter image description here

    Azure.RequestFailedException: ‘This request is not authorized to perform this operation using this permission.

    The above error occurs when your principal doesn’t has access to azure blob storage.

    For accessing blob storage through identity, Gaurav Mantri comment it is correct, you need a role to access blob storage,
    The roles are

    • Storage-blob-contributor(or)
    • Storage-blob-owner

    Go to portal -> storage accounts -> Access Control (IAM) ->Add -> Add role assignments -> storage-blob-contributor or storage-blob-owner role to the storage account.

    Portal:

    enter image description here

    After assigning role to my storage account, I executed same code and it successfully uploaded file in azure blob storage.

    Code:

    using Azure.Storage.Blobs;
    using System;
    using System.IO;
    using Azure.Identity;
    
    // TODO: Replace <storage-account-name> with your actual storage account name
    var blobServiceClient = new BlobServiceClient(
            new Uri("https://[some azure storage]"),
            new DefaultAzureCredential());
    
    // Set container name
    string containerName = "test";
    
    // Get container
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    
    // Create a local file in the ./data/ directory for uploading and downloading
    string localPath = "data";
    Directory.CreateDirectory(localPath);
    string fileName = "testupload" + Guid.NewGuid().ToString() + ".txt";
    string localFilePath = Path.Combine(localPath, fileName);
    
    // Write text to the file
    await File.WriteAllTextAsync(localFilePath, "Hello, World!");
    
    // Get a reference to a blob
    BlobClient blobClient = containerClient.GetBlobClient(fileName);
    
    Console.WriteLine("Uploading to Blob storage as blob:nt {0}n", blobClient.Uri);
    
    // Upload data from the local file
    await blobClient.UploadAsync(localFilePath, true);
    

    Console:

    enter image description here

    Portal:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search