How to programmatically find out what operations I can do in a blob storage?

476 Views Asked by At

I am using libraries Microsoft.Azure.Storage.Blob 11.2.3.0 and Microsoft.Azure.Storage.Common 11.2.3.0 to connect to an Azure BlobStorage from a .NET Core 3.1 application.

When I started working on this, I had been given connection strings that gave me full access to the BlobStorage (or rather, the entire cloud storage account). Based upon those, I chose to write my connection code "defensively", making use of Exists() and CreateIfNotExists() from the CloudBlobContainer class to ensure the application would not fail when a container was not yet existing.


Now, I'm connecting a BlobStorage container using a SAS. While I can freely retrieve and upload blobs within the container like this, unfortunately, it seems that I am not allowed to do anything on the container level. Not only CreateIfNotExists, but even the mere querying of existence by Exists() throws a StorageException saying

This request is not authorized to perform this operation.

The documentation does not mention the exception.

Is there any way to check preemptively whether I am allowed to check the container's existence?

I have tried looking into the container permissions retrieved from GetPermissions, but that will throw an exception, as well.

The only other alternative I can see is to check for container existence within a try-catch-block and assume existence if an exception is thrown ...

2

There are 2 best solutions below

1
On BEST ANSWER

There's a no definitive way to identify if an operation can be performed using a SAS token other than performing that operation and catching any exception that may be thrown by the operation. The exception that is of your interest is Unauthorized (403).

However you can try to predict if an operation can be performed by looking at the SAS token. If it is a Service SAS Token and not an Account SAS Token, that means all the account related operations are not not allowed. The way to distinguish between an Account SAS token and a Service SAS token is that the former will contain attributes like SignedServices (ss) and SignedResourceTypes (srt).

Next thing you would want to do is look for SignedPermissions (sp) attribute in your SAS token. This attribute will tell you what all operations are possible with the SAS token. For example, if your SAS token is a Service SAS token and if it includes Delete (d) permission, that would mean you can use this SAS token to delete a blob.

Please see these tables for the permissions/allowed operations combinations:

Please note that the operation might still fail for any number of reasons like SAS token has expired, account key has changed since the generation of SAS token, IP restrictions etc.

6
On

I tried in in my system to check whether the container exist or not able check it and if container not exists created container and able to upload file.

You need to give proper permission for your SAS Token

enter image description here

const string sasToken = “SAS Token”

            const string accountName = "teststorage65";
            const string blobContainerName = "example";
            const string blobName = "test.txt";
            const string myFileLocation = @"Local Path ";

            var storageAccount = new CloudStorageAccount(storageCredentials, accountName, null, true);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName);
            var result=blobContainer.Exists();
            if (result == true)
            {
                Console.WriteLine("Container exists");
            }
            else
            {
               // blobContainer.CreateIfNotExists();
                Console.WriteLine("Conatiner not exists");

               Console.WriteLine("Creating Container   "+ blobContainerName);
                blobContainer.CreateIfNotExists();
            }

               // blobContainer.CreateIfNotExists();
            //Console.WriteLine("Creating Container   ");
            CloudBlockBlob cloudBlob = blobContainer.GetBlockBlobReference(blobName);
            cloudBlob.UploadFromFile(myFileLocation);
        

OUTPUT

enter image description here

enter image description here