Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate SAS token c# programmatically

Tags:

.net

azure

I have copied a file 1.txt from Container A to Container B.

I am also able to get the complete URL of the copied file and also able to open it in the same browser tab as long as container is public.

Now I am making my container private .. I was hoping there was a simple API which could give me SAS URL & TOKEN on the spot .

Is there any such API ?

like image 963
ecstasy Avatar asked May 07 '26 11:05

ecstasy


1 Answers

Please try this code:

        Azure.Storage.Sas.BlobSasBuilder blobSasBuilder = new Azure.Storage.Sas.BlobSasBuilder()
        {
            BlobContainerName = "demo-copy",
            BlobName = "test.txt",
            ExpiresOn = DateTime.UtcNow.AddMinutes(5),//Let SAS token expire after 5 minutes.
        };
        blobSasBuilder.SetPermissions(Azure.Storage.Sas.BlobSasPermissions.Read);//User will only be able to read the blob and it's properties
        var sasToken = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(accountName, accountKey)).ToString();
        var sasUrl = blobClient.Uri.AbsoluteUri + "?" + sasToken;

Basically you need to make use of BlobSasBuilder class.

like image 177
Gaurav Mantri Avatar answered May 10 '26 05:05

Gaurav Mantri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!