ecstasy
ecstasy

Reputation: 181

Generate SAS token c# programmatically

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 ?

Upvotes: 9

Views: 17982

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136136

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.

Upvotes: 28

Related Questions