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

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

The az storage container command is a part of the Azure CLI suite, commonly referred to as az. It is specifically designed for managing blob storage containers within Microsoft Azure. Azure Blob Storage is a service for storing large amounts of unstructured data such as text or binary data. This command offers comprehensive functionalities to create, configure, and manage these containers efficiently and programmatically.

Whether you are setting up your applications to handle photos, documents, backups, or even video files, understanding how to effectively use the az storage container command can significantly streamline your data management within Azure.

Use case 1: Create a container in a storage account

Code:

az storage container create --account-name storage_account_name --name container_name --public-access access_level --fail-on-exist

Motivation:
Creating a container in an Azure storage account is a fundamental step when organizing and structuring data in blob storage. Containers act as an organizational unit that enables users to easily group and manage their stored blobs. By creating a container, users can set specific permissions and access levels to ensure data is secured and only accessible to intended parties.

Explanation:

  • --account-name storage_account_name: Specifies the name of the storage account in which you want to create the container. This is essential as it directs the Azure CLI to the specific location where the container should be established.
  • --name container_name: Defines the name of the new container. This name is unique within the storage account and serves as the container’s identifier.
  • --public-access access_level: Sets the level of public access for the container. It can be ‘blob’ or ‘container’, where ‘blob’ allows anonymous read access to blobs in the container, and ‘container’ allows anonymous read access to all blobs and the container properties.
  • --fail-on-exist: Causes the command to fail if a container with the same name already exists. This ensures there are no accidental overwrites or duplications.

Example Output:

{
  "created": true
}

Use case 2: Generate a shared access signature for the container

Code:

az storage container generate-sas --account-name storage_account_name --name container_name --permissions sas_permissions --expiry expiry_date --https-only

Motivation:
Generating a shared access signature (SAS) for a container is crucial for enabling secure and temporary access to your data without having to share storage account keys. This is especially useful for delegated access scenarios where you might want to provide limited access to partners, developers, or clients.

Explanation:

  • --account-name storage_account_name: Directs the command towards the specific storage account where the container resides.
  • --name container_name: Identifies the container for which the SAS is generated.
  • --permissions sas_permissions: Specifies the permissions for the SAS, such as read, write, list, and more, allowing for granular control over what actions can be performed with the SAS.
  • --expiry expiry_date: Sets the expiration date and time for the SAS, after which it becomes invalid. This ensures that access is time-bound for security purposes.
  • --https-only: Ensures that the SAS can only be used with HTTPS, adding an additional layer of security by encrypting data while in transit.

Example Output:

{
  "sas": "sv=2021-08-06&ss=bfqt&srt=sco&sp=rwdlacu&se=2022-12-31T23:59:59Z&st=2022-01-01T00:00:00Z&spr=https&sig=exampleSignature"
}

Use case 3: List containers in a storage account

Code:

az storage container list --account-name storage_account_name --prefix filter_prefix

Motivation:
Listing containers within a storage account provides an overview of your data organization and helps in auditing, managing, and planning infrastructure growth. It’s critical for users who manage large volumes of data and need to quickly find specific containers or confirm their existence.

Explanation:

  • --account-name storage_account_name: Points the operation to the relevant storage account, which contains the containers you wish to list.
  • --prefix filter_prefix: Filters the listed containers by a prefix. This is useful in scenarios where there are numerous containers, and you need to narrow down the results to a specific subset that share a common naming convention or prefix.

Example Output:

[
  {
    "name": "container1",
    "properties": {
      "lastModified": "2022-02-01T12:00:00Z",
      "leaseStatus": "unlocked",
      "leaseState": "available",
      "publicAccess": "None"
    }
  },
  {
    "name": "container2",
    "properties": {
      "lastModified": "2022-02-02T12:00:00Z",
      "leaseStatus": "locked",
      "leaseState": "leased",
      "publicAccess": "blob"
    }
  }
]

Use case 4: Mark the specified container for deletion

Code:

az storage container delete --account-name storage_account_name --name container_name --fail-not-exist

Motivation:
Marking a container for deletion is a crucial maintenance task in data lifecycle management. It’s necessary for freeing up resources, maintaining compliance with data retention policies, and ensuring clutter is minimized within your storage account. This use case represents a decisive action to manage data efficiently.

Explanation:

  • --account-name storage_account_name: Targets the specific storage account holding the container slated for deletion.
  • --name container_name: Specifies the particular container to be deleted within the account.
  • --fail-not-exist: Ensures the command exits with an error if the container does not exist, preventing unnecessary command execution and potential misunderstandings about the container’s status.

Example Output:

{
  "deleted": true
}

Conclusion

The az storage container command offers a foundational set of operations essential for managing Azure Blob Storage containers. From creation to deletion, and generating SAS tokens to listing all containers, this command provides powerful and secure management capabilities that enable users to efficiently handle their storage strategies on the Azure platform. Through these examples, users can gain clarity and confidence in using Azure CLI’s storage commands as part of their routine data management tasks.

Related Posts

How to Use the Command 'mdbook' (with examples)

How to Use the Command 'mdbook' (with examples)

MdBook is a versatile tool designed to create online books using Markdown files, providing a straightforward way to convert written content into a beautifully rendered HTML book.

Read More
How to Use the Command 'sunicontopnm' (with examples)

How to Use the Command 'sunicontopnm' (with examples)

The sunicontopnm command is part of the Netpbm suite, an open-source library of graphics programs and a programming library used to manipulate graphic images.

Read More
How to Use the Command 'pee' (with Examples)

How to Use the Command 'pee' (with Examples)

The pee command is a tool from the moreutils package that allows you to tee stdin to multiple pipes.

Read More