Azure Documentation

Understanding and Using Shared Access Signatures (SAS) Tokens for Azure Storage

Shared Access Signatures (SAS) provide a secure way to grant limited access to resources in your Azure Storage account, such as blobs, containers, queues, and tables, without sharing your account access keys. This tutorial will guide you through understanding SAS tokens and how to generate and use them.

What is a Shared Access Signature (SAS)?

A SAS token is a URI that contains a security token in its query parameters. This token allows a client to have delegated access to storage resources in your account. With a SAS, you can grant clients access to specific resources for a specified period, with specific permissions, and from specific IP addresses or networks.

Types of SAS

When to Use SAS Tokens

Generating a SAS Token

You can generate SAS tokens using various methods:

  1. Azure Portal: The easiest way for quick generation. Navigate to your storage account, then to the container or blob, and use the "Generate SAS" option.
  2. Azure CLI: Useful for scripting and automation.
  3. Azure PowerShell: Another scripting option.
  4. Azure Storage SDKs: Programmatically generate SAS tokens within your applications.

Example: Generating a SAS Token using Azure CLI (for a blob)

This example generates a blob SAS token with read and write permissions, valid for 1 hour.


az storage blob generate-sas \
    --account-name mystorageaccount \
    --container-name mycontainer \
    --name myblob.txt \
    --permissions rwk \
    --expiry 2024-07-27T10:00:00Z \
    --https-only \
    --auth-mode login
            

Note: Replace placeholders like mystorageaccount, mycontainer, myblob.txt, and the expiry date with your actual values.

Important: Always restrict the permissions and expiry time of your SAS tokens to the minimum necessary to enhance security. Avoid granting full control unless absolutely required.

Using a SAS Token

Once you have generated a SAS token, it will be appended to the URI of the storage resource. A client can then use this full URI to access the resource with the granted permissions.

Example: Accessing a Blob with a SAS Token

The resulting URI will look something like this:


https://mystorageaccount.blob.core.windows.net/mycontainer/myblob.txt?sv=2022-11-02&ss=b&srt=sco&sp=rw&se=2024-07-27T10%3A00%3A00Z&st=2024-07-27T09%3A00%3A00Z&spr=https&sig=aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789ABCDEFGHIJKLMNO=
            

This URI can be used in applications, scripts, or even directly in a web browser (for read permissions) to access the specified blob.

Best Practices for SAS Tokens

Conclusion

Shared Access Signatures are a powerful tool for securely managing access to your Azure Storage resources. By understanding their types, generation methods, and best practices, you can effectively grant granular and time-bound access to your data.