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 used to manage blob storage containers in Azure. It provides various functionalities such as creating containers, generating shared access signatures, listing containers, and deleting containers within a storage account.

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 is the first step in utilizing blob storage in Azure. It allows you to organize and store different types of data.

Explanation:

  • --account-name: Specifies the name of the storage account where the container will be created.
  • --name: Specifies the name of the container.
  • --public-access: Sets the level of public access to the container. It can be set to blob, container, or off.
  • --fail-on-exist: If specified, the command fails if the container already exists.

Example output:

{
  "created": true,
  "etag": "\"0x8D8EEB8B4EBEE07\"",
  "lastModified": "2022-01-20T12:34:56+00:00"
}

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: A shared access signature (SAS) allows you to provide time-limited access to the container, without directly exposing the storage account access keys. It can be useful when you want to share data securely with others.

Explanation:

  • --account-name: Specifies the name of the storage account where the container exists.
  • --name: Specifies the name of the container.
  • --permissions: Sets the permissions for the SAS. It can include r (read), w (write), d (delete), and l (list).
  • --expiry: Specifies the expiry date and time for the SAS.
  • --https-only: If specified, the SAS can only be used over HTTPS.

Example output:

sv=2020-12-xx&st=2022-01-20T12%3A00%3A00Z&se=2022-01-20T13%3A00%3A00Z&sr=c&sp=racwdxlt&sig=abcdefghijklmnopqrstuvwxyz1234567890

Use case 3: List containers in a storage account

Code:

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

Motivation: When working with multiple containers within a storage account, it can be helpful to retrieve a list of all the containers for reference or further processing.

Explanation:

  • --account-name: Specifies the name of the storage account where the containers exist.
  • --prefix: Filters the list of containers by the specified prefix.

Example output:

[
  {
    "name": "container1",
    "properties": {
      "lastModified": "2022-01-20T12:34:56+00:00",
      "etag": "\"0x8D8EEB8B4EBEE07\"",
      "publicAccess": "container"
    }
  },
  {
    "name": "container2",
    "properties": {
      "lastModified": "2022-01-20T12:34:56+00:00",
      "etag": "\"0x8D8EEB8B4EBEE07\"",
      "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: When a container is no longer needed and you want to clean up storage resources, marking it for deletion allows for systematic removal.

Explanation:

  • --account-name: Specifies the name of the storage account where the container exists.
  • --name: Specifies the name of the container.
  • --fail-not-exist: If specified, the command fails if the container does not exist.

Example output:

{
  "deleted": true
}

Conclusion:

The az storage container command provides a convenient way to manage blob storage containers in Azure. Whether you need to create containers, generate shared access signatures, list containers, or delete containers, this command offers a range of functionalities to efficiently manage your storage resources.

Related Posts

How to use the command "sf" (with examples)

How to use the command "sf" (with examples)

The “sf” command is a powerful command line interface that simplifies development and build automation when working with your Salesforce org.

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

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

The uuidgen command is used to generate unique identifiers known as UUIDs.

Read More
Using the gprof command (with examples)

Using the gprof command (with examples)

1: Compiling binary with gprof information and generating gmon.out file gcc -pg program.

Read More