Using the `az storage blob` command (with examples)
Download a blob to a file path
az storage blob download --account-name storage_account_name --account-key storage_account_key -c container_name -n path/to/blob -f path/to/local_file
Motivation: This command allows you to download a blob from a storage container to a local file path. It is useful when you want to retrieve a specific blob for analysis or further processing on your local machine.
Explanation:
--account-name
: Specifies the name of your Azure Storage account.--account-key
: Specifies the access key for your Azure Storage account.-c
or--container-name
: Specifies the name of the container where the blob is stored.-n
or--name
: Specifies the path to the blob.-f
or--file
: Specifies the local file path where the blob will be downloaded.
Example output:
Downloaded blob 'path/to/blob' to 'path/to/local_file'.
Download blobs from a blob container recursively
az storage blob download-batch --account-name storage_account_name --account-key storage_account_key -s container_name -d path/to/remote --pattern filename_regex --destination path/to/destination
Motivation: This command allows you to download multiple blobs from a container recursively. It is useful when you want to retrieve all blobs from a specific container and its subdirectories for offline processing or backup purposes.
Explanation:
--account-name
: Specifies the name of your Azure Storage account.--account-key
: Specifies the access key for your Azure Storage account.-s
or--source
: Specifies the name of the container to download blobs from.-d
or--destination
: Specifies the local directory path to save the downloaded blobs.--pattern
: Specifies the regular expression pattern for matching specific blob filenames.
Example output:
Downloaded 5 blobs from 'container_name' to 'path/to/destination'.
Upload a local file to blob storage
az storage blob upload --account-name storage_account_name --account-key storage_account_key -c container_name -n path/to/blob -f path/to/local_file
Motivation: This command allows you to upload a local file to blob storage. It is useful when you want to store a file in Azure Blob Storage for later retrieval or sharing with others.
Explanation:
--account-name
: Specifies the name of your Azure Storage account.--account-key
: Specifies the access key for your Azure Storage account.-c
or--container-name
: Specifies the name of the container where the blob will be stored.-n
or--name
: Specifies the path to the blob.-f
or--file
: Specifies the local file path to upload.
Example output:
Uploaded file 'path/to/local_file' as blob 'path/to/blob' in container 'container_name'.
Delete a blob object
az storage blob delete --account-name storage_account_name --account-key storage_account_key -c container_name -n path/to/blob
Motivation: This command allows you to delete a specific blob object from a storage container. It is useful when you want to remove a blob that is no longer needed or to clean up storage space.
Explanation:
--account-name
: Specifies the name of your Azure Storage account.--account-key
: Specifies the access key for your Azure Storage account.-c
or--container-name
: Specifies the name of the container where the blob is stored.-n
or--name
: Specifies the path to the blob to be deleted.
Example output:
Deleted blob 'path/to/blob' from container 'container_name'.
Generate a shared access signature for a blob
az storage blob generate-sas --account-name storage_account_name --account-key storage_account_key -c container_name -n path/to/blob --permissions permission_set --expiry Y-m-d'T'H:M'Z' --https-only
Motivation: This command allows you to generate a shared access signature (SAS) for a specific blob. A SAS provides limited access permissions to the blob without exposing the account key. It is useful when you want to share access to a blob securely with others.
Explanation:
--account-name
: Specifies the name of your Azure Storage account.--account-key
: Specifies the access key for your Azure Storage account.-c
or--container-name
: Specifies the name of the container where the blob is stored.-n
or--name
: Specifies the path to the blob for which the SAS is generated.--permissions
: Specifies the permissions granted to the SAS. Can be a combination of “r” (read), “w” (write), “d” (delete), and “l” (list).--expiry
: Specifies the expiration date and time of the SAS in the format “Y-m-d’T’H:M’Z’”.--https-only
: Specifies whether the SAS can only be used with HTTPS requests.
Example output:
Shared access signature (SAS) for blob 'path/to/blob' generated successfully.
SAS: ?sv=2020-08-04&ss=b&srt=c&sp=rwdlacx&se=2022-12-31T23:59:59Z&st=2022-01-01T00:00:00Z&spr=https&sig=xxxxxxxxxxxxxxxxxxxxxxxxxx
Conclusion
In this article, we explored different use cases of the az storage blob
command. We learned how to download a blob to a file path, download multiple blobs recursively, upload a local file to blob storage, delete a blob object, and generate a shared access signature for a blob. These examples demonstrate the versatility and power of the az storage blob
command in managing blob storage containers and objects in Azure.