How to use the command 'az storage blob' (with examples)

How to use the command 'az storage blob' (with examples)

az storage blob is a command-line tool that forms part of the Microsoft Azure Command-Line Interface (azure-cli or az). It is designed to manage Azure Blob Storage services, allowing users to perform a variety of operations on blob storage containers and their contents. Azure Blob Storage is a service for storing large amounts of unstructured data, such as text or binary data. This tool provides various functionalities to download, upload, delete, and manage blob objects efficiently from the CLI.

Use case 1: Download a blob to a file path specifying a source container

Code:

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:

Sometimes, you might need to download a specific blob from your Azure storage account to conduct analysis, processing, or archival. Directly downloading a blob to a specific file path ensures that you have access to the necessary data locally whenever needed.

Explanation:

  • --account-name storage_account_name: Specifies the name of your Azure storage account. This identifies the particular storage account from which the blob will be downloaded.
  • --account-key storage_account_key: Provides the access key for authentication to the Azure storage account. This is necessary for authorizing requests to the service.
  • -c container_name: Denotes the container within the Azure storage account that houses the blob. Containers help organize blobs within a storage account.
  • -n path/to/blob: Defines the path and name of the blob within the container that you wish to download.
  • -f path/to/local_file: Specifies the local file path where the downloaded blob will be saved. This allows precise control over where on your local machine the file will be stored.

Example Output:

Upon successful execution, the terminal will display a message indicating that the blob has been successfully downloaded to your specified local path.

Use case 2: Download blobs from a blob container recursively

Code:

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:

Downloading blobs recursively from a container is invaluable when managing large datasets or files stored in a structured manner. It allows for batch processing and retrieval of data, making data handling more efficient for applications like data migration, backup, or batch analysis.

Explanation:

  • --account-name storage_account_name: Identifies the Azure storage account from which blobs are being downloaded.
  • --account-key storage_account_key: Provides authorization credentials required to access the storage account.
  • -s container_name: Indicates the name of the container that contains the blobs you wish to download.
  • -d path/to/remote: Specifies the directory path within the container to be downloaded. This allows you to target specific subdirectories or contents.
  • --pattern filename_regex: Utilizes regular expressions to filter and identify blobs to download. This is useful for selectively downloading files that match specific naming patterns.
  • --destination path/to/destination: Specifies the local directory where downloaded blobs should be stored, helping organize local storage and align with the desired file structure.

Example Output:

The command outputs a series of messages confirming the successful download of each blob to the specified local directory, providing feedback on the process.

Use case 3: Upload a local file to blob storage

Code:

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:

Uploading files to Azure Blob Storage is essential for making data accessible in the cloud. Whether for backup, collaboration, or to serve applications, moving files to cloud storage facilitates accessibility and scalability.

Explanation:

  • --account-name storage_account_name: Identifies the specific Azure storage account where the file will be uploaded.
  • --account-key storage_account_key: The access key authenticates your upload request, ensuring security and integrity.
  • -c container_name: States the target container where the blob will be uploaded, organizing the file within the correct context.
  • -n path/to/blob: Specifies the intended name and path of the blob inside the container, allowing necessary naming and path management.
  • -f path/to/local_file: Indicates the local file path of the content intended for upload, identifying the file to transfer to Azure storage.

Example Output:

Upon uploading, the CLI confirms the successful transfer of the file to the specified blob path in the container, ensuring users of the completeness of their task.

Use case 4: Delete a blob object

Code:

az storage blob delete --account-name storage_account_name --account-key storage_account_key -c container_name -n path/to/blob

Motivation:

Deleting unnecessary or outdated blobs is a crucial part of managing storage expenses and ensuring data compliance. Removing obsolete data helps maintain the relevance and efficiency of your storage solution.

Explanation:

  • --account-name storage_account_name: Indicates the Azure storage account containing the blob to be deleted.
  • --account-key storage_account_key: Confirms authentication credentials for executing the delete operation securely.
  • -c container_name: Specifies the container holding the blob targeted for deletion, helping pinpoint the correct storage context.
  • -n path/to/blob: Identifies the path and exact blob to be deleted, ensuring accuracy in the removal process.

Example Output:

The CLI provides a confirmation message after successfully deleting the blob, offering reassurance that storage space has been effectively managed.

Use case 5: Generate a shared access signature for a blob

Code:

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:

Shared Access Signatures (SAS) are essential when you need to provide limited-time access to your data stored in Azure without sharing your account keys. This use case is ideal for collaboration, data sharing, or temporarily granting access to third parties.

Explanation:

  • --account-name storage_account_name: Designates the Azure storage account containing the blob for which the SAS is generated.
  • --account-key storage_account_key: Verifies access rights for generating a SAS.
  • -c container_name: Points to the container where the blob resides, establishing the central context for the SAS.
  • -n path/to/blob: Indicates the exact blob for which the SAS is intended, ensuring precision in access control.
  • --permissions permission_set: Specifies the level of access to be granted (e.g., read, write), outlining what the recipient of the SAS can do.
  • --expiry Y-m-d'T'H:M'Z': Sets the expiration time/date of the SAS, delineating the duration for which access is valid.
  • --https-only: Restricts access through HTTPS, enhancing security by ensuring encrypted data transmission.

Example Output:

The command outputs the SAS token, which can be shared with users needing access, ensuring they have the necessary permissions for the specified duration.

Conclusion:

The az storage blob command offers versatile functionalities to manage Azure Blob Storage effectively. By understanding the use cases and applications, users can adeptly handle their storage resources, optimizing performance and maintaining control over their data management processes.

Related Posts

How to use the command 'watchdogd' (with examples)

How to use the command 'watchdogd' (with examples)

The watchdogd command is a system utility employed primarily to work in conjunction with the Watchdog KEXT to ensure that a macOS system remains healthy and operational.

Read More
A Comprehensive Guide to Using the SF Command-Line Interface (with examples)

A Comprehensive Guide to Using the SF Command-Line Interface (with examples)

The Salesforce Command-Line Interface (sf) is a remarkably powerful tool designed to simplify development and build automation for developers working with Salesforce organizations.

Read More
How to use the command 'qtcreator' (with examples)

How to use the command 'qtcreator' (with examples)

Qt Creator is a robust, cross-platform Integrated Development Environment (IDE) designed for developing applications using the Qt application framework.

Read More