azure blob- sas tokenazure blob- sas token

Azure Blob storage

Azure Blob Storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that doesn’t adhere to a particular data model or definition, such as text or binary data.

Azure Blob storage resources

Azure Blob storage offers three types of resources:

  • The storage account
  • container in the storage account
  • blob in a container

The following diagram shows the relationship between these resources.

Shared access signatures (SAS) token

 The SAS token is a string that you generate on the client side, for example by using one of the Azure Storage client libraries. The SAS token is not tracked by Azure Storage in any way. You can create an unlimited number of SAS tokens on the client side. After you create a SAS, you can distribute it to client applications that require access to resources in your storage account.

Flowchart :

Sample C# Code for generating SAS token:
private static string GenearteSas(string container,string blob, string accountName, string accountKey)
        {

            var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
            var cloudBlobClient = account.CreateCloudBlobClient();
            var containerr = cloudBlobClient.GetContainerReference(container);
            var blobb = containerr.GetBlockBlobReference(blob);

            Azure.Storage.Sas.BlobSasBuilder blobSasBuilder = new Azure.Storage.Sas.BlobSasBuilder()

            {
                BlobContainerName =container,
                BlobName =blob,
                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();

        }

Example: https://myaccount.blob.core.windows.net/pictures/profile.jpg?{{SAStoken}}

Sample Code reference for other languages:

https://docs.microsoft.com/en-us/rest/api/eventhub/generate-sas-token

4 thoughts on “How to download Azure Blob file using SAS token?”

Leave a Reply

Your email address will not be published. Required fields are marked *