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:Pic1

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

  1. WindowsAzure.ConfigurationManager
  2. 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:Pic2

  • 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:Pic3
  • We can use the container reference, blob file name and the connection to create the SAS key like below:Pic4

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.

Please provide your comments on this blog in the comments section.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s