This request is not authorized to perform this operation. Azure blobClient
Asked Answered
D

3

39

I have the following code to return a list of containers using the WindowsAzure.Storage nuget package:

public static class AzureBlobStorageClient
{
    public static CloudBlobClient GetClient(string AccountName = "foo", string AccountKey = "bar" )
    {
        try
        {

            var connectionString = $"DefaultEndpointsProtocol=https;AccountName={AccountName};AccountKey={AccountKey};EndpointSuffix=core.windows.net";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            IRetryPolicy exponentialRetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(2), 10);
            blobClient.DefaultRequestOptions.RetryPolicy = exponentialRetryPolicy;
            return blobClient;
        }
        catch (StorageException ex)
        {
            Console.WriteLine("Error returned from the service: {0}", ex.Message);
            throw;
        }
    }

    public static void DeleteContainer(CloudBlobContainer container)
    {
        var result = container.DeleteIfExistsAsync().Result;
    }

    public static List<CloudBlobContainer> GetContainers()
    {
        var client = GetClient();
        BlobContinuationToken continuationToken = null;
        List<CloudBlobContainer> results = new List<CloudBlobContainer>();
        do
        {
            var response = client.ListContainersSegmentedAsync(continuationToken).Result;
            continuationToken = response.ContinuationToken;
            results.AddRange(response.Results);
        }
        while (continuationToken != null);

        return results;
    }

}

when i run this, i get the following error on client.ListContainersSegmentedAsync(continuationToken).Result :

System.AggregateException: 'One or more errors occurred. (This request is not authorized to perform this operation.)'

and I can't see how to set the authorization for the request.

My question is how to get past this error message

Di answered 14/5, 2019 at 12:42 Comment(1)
Your code looks fine to me. Please see my comment here: #56130473.Jason
D
107

Thanks to @gaurav Mantri for this answer.

The issue was my client IP was not added to the firewall rules for the storage account.

To change this go to :

Storage accounts > {yourAccount} > Networking > Firewalls and Virtual networks

and add your IP address

Di answered 14/5, 2019 at 12:59 Comment(3)
+1'd ...so annoyingly obvious! Yet whenever I work from a remote location, I keep forgetting to add my new IP to the firewalls... thinking instead my errors are due to Azure Functions updates that have broken everything overnight.... welcome to my morning! :)Swivet
yay for secure solutions!Jaconet
It should be the public IP address can be found by curl ifconfig.coCheyney
C
1

Please note that after enabling all or adding your ip addressed in to whitelist it will take around 1 minute to reflect the changes.

Chorion answered 29/9, 2022 at 7:42 Comment(0)
O
0

This looks like the user doesn't have permission to perform the action. Go to IAM and do the role assignment required to perform the action. I had similar situation of not able to create cobtainer in an SA. It worked after assigning contributer role to my login.

Olethea answered 14/11, 2022 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.