Generating Azure Blob file SAS key using Azure SDK | CloudFronts

Generating Azure Blob file SAS key using Azure SDK

Why do we need SAS key for Blob File?

Azure Blob is massively scalable object storage for unstructured data.

  • Blob Storage can handle all your unstructured data, scaling up or down as your needs change.
  • You no longer have to manage it
  • You only pay for what you use and you save money over on-premises storage options.

A shared access signature (SAS) provides you with a way to grant limited access to objects in your storage account to other clients, without exposing your account key.

A SAS gives you granular control over the type of access you grant to clients who have the SAS, including:

  1. The interval over which the SAS is valid, including the start time and the expiry time.
  2. The permissions granted by the SAS. For example, a SAS for a blob might grant read and write permissions to that blob, but not delete permissions.
  3. An optional IP address or range of IP addresses from which Azure Storage will accept the SAS. For example, you might specify a range of IP addresses belonging to your organization.
  4. The protocol over which Azure Storage will accept the SAS. You can use this optional parameter to restrict access to clients using HTTPS.

Using SDK to Generate SAS key from C-Sharp code:

  • You need to include the Azure SDK DLLs:

You can get the DLLs from Nuget package manager as well. Refer below names to search in Nuget:

  1. WindowsAzure.ConfigurationManager
  2. WindowsAzure.Storage
  • The first step is to connect to the Blob storage using Connection String. You can add the Blob connection string in the App.config and use that in code to create connection like below:
  • Your connection string should be of the format:

  • The below code creates the connection:
//Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

//Create the blob client object.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
  • Once connection is created, we can retrieve the blob container which contains the blob:
  • We can use the container reference, blob file name and the connection to create the SAS key like below:

I have uploaded the entire code to my Github as well for reference.

Why we can’t use Azure Storage Explorer tool to do this:

The Azure storage explorer also has the option to connect to your blob and generate SAS key with clean User Interface. But recently for last 2-3 months, I am facing issues with generating Key with Azure Storage Explorer.

This is the reason I implemented this code to remove dependency on the tool.


Share Story :

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close