Container Access Policies

This document explains how to manage access to your Azure Storage containers and the blobs within them. Understanding access policies is crucial for securing your data and controlling who can read, write, or delete your blobs.

Container Access Levels

Azure Storage provides several options for controlling access to containers. You can set access policies at the container level, which then apply to all blobs within that container.

Public Access

Public access allows unauthenticated access to containers and their blobs. Use this with caution, as it exposes your data to anyone on the internet.

Note: Enabling public access should be done only when absolutely necessary and with a clear understanding of the security implications.

Private Access with Shared Access Signatures (SAS)

Shared Access Signatures (SAS) provide a secure way to delegate limited access to your storage resources. You can grant specific permissions (e.g., read, write, delete) to a client for a defined period without sharing your account access keys.

SAS tokens are appended to the URL of the resource and specify permissions, expiry time, and other constraints.

Tip: Use SAS tokens with the shortest possible validity period to minimize security risks.

Access Control Lists (ACLs) for Containers

You can define fine-grained access control for containers using Access Control Lists (ACLs). This allows you to grant permissions to specific users or applications.

Accessing Containers and Blobs

You can manage container access through various methods:

Example: Setting Container Public Access (Azure CLI)

The following command sets a container to allow public read access to blobs within it:

        
az storage container set-public-access --account-name mystorageaccount --name mycontainer --public-access blob
        
        

To disable public access:

        
az storage container set-public-access --account-name mystorageaccount --name mycontainer --public-access off
        
        

Example: Generating a Blob SAS Token (Azure SDK for Python)

        
from azure.storage.blob import BlobServiceClient, AccountSasPermissions, ResourceTypes

connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

container_name = "mycontainer"
blob_name = "myblob.txt"

# Generate a SAS token for read access for 1 hour
sas_permissions = AccountSasPermissions(read=True)
sas_token = blob_service_client.generate_container_sas(
    container_name,
    resource_types=ResourceTypes(object=True), # Permissions for blobs
    permission=sas_permissions,
    expiry=datetime.utcnow() + timedelta(hours=1)
)

blob_url_with_sas = f"https://mystorageaccount.blob.core.windows.net/{container_name}/{blob_name}?{sas_token}"
print(f"Blob URL with SAS: {blob_url_with_sas}")
        
        

Important: Replace placeholders like YOUR_AZURE_STORAGE_CONNECTION_STRING, mystorageaccount, mycontainer, and myblob.txt with your actual values.

Best Practices for Container Access