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:
- The interval over which the SAS is valid, including the start time and the expiry time.
- 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.
- 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.
- 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:
- 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.
Please provide your comments on this blog in the comments section.