Azure Storage Documentation

Create a Container

Containers are the fundamental building blocks for organizing data in Azure Blob Storage. A container groups a set of blobs, and you can think of it as a directory in a file system. All blobs are organized within containers.

Prerequisites

Azure CLI
Azure Portal
Azure PowerShell
SDKs

Using Azure CLI

The Azure Command-Line Interface (CLI) provides a powerful way to manage your Azure resources, including creating blob containers.

Steps:

  1. Make sure you have the Azure CLI installed and are logged in to your Azure account.
  2. Use the az storage container create command.
Bash Copy
az storage container create \
    --name mycontainer \
    --account-name mystorageaccount \
    --auth-mode login

Explanation:

  • --name mycontainer: Specifies the name of the container you want to create. Container names must be between 3 and 63 characters long, and can contain only lowercase letters, numbers, and hyphens.
  • --account-name mystorageaccount: Specifies the name of your storage account.
  • --auth-mode login: Uses your Azure AD credentials for authentication. Alternatively, you can use a connection string or account key.
Note: Container names are case-insensitive in Azure Storage. However, it's a best practice to use lowercase names.

Using Azure Portal

The Azure Portal offers a user-friendly graphical interface for managing your storage account and its containers.

Steps:

  1. Sign in to the Azure Portal.
  2. Navigate to your storage account.
  3. In the left-hand menu, under Data storage, select Containers.
  4. Click the + Container button.
  5. Enter a name for your container.
  6. Choose the public access level (Private, Blob, or Container).
  7. Click Create.
Tip: For security, it's recommended to keep containers private unless public access is explicitly required.

Using Azure PowerShell

Azure PowerShell provides cmdlets for managing Azure resources. You can create containers using the Az.Storage module.

Steps:

  1. Install the Az.Storage module if you haven't already: Install-Module -Name Az.Storage
  2. Connect to your Azure account: Connect-AzAccount
  3. Use the New-AzStorageContainer cmdlet.
PowerShell Copy
New-AzStorageContainer `
    -Name "mycontainer" `
    -Context (New-AzStorageContext -StorageAccountName "mystorageaccount" -StorageAccountKey "YOUR_STORAGE_ACCOUNT_KEY")

Explanation:

  • -Name "mycontainer": The name of the container.
  • -Context (...): This defines the connection context. You can either use StorageAccountKey, SasToken, or obtain the context from a logged-in user session. For production scenarios, consider using Azure AD authentication.
Warning: Avoid hardcoding storage account keys directly in scripts. Use secure methods like Azure Key Vault or managed identities for production environments.

Using SDKs

Azure Blob Storage can be managed programmatically using various SDKs available for popular programming languages.

Example: Python SDK

Here's how to create a container using the Azure Storage Blob SDK for Python.

Python Copy
from azure.storage.blob import BlobServiceClient

# Replace with your actual connection string
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
container_name = "my-python-container"

try:
    # Create the BlobServiceClient object
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)

    # Create the container
    container_client = blob_service_client.create_container(container_name)

    print(f"Container '{container_name}' created successfully.")

except Exception as ex:
    print('Exception:')
    print(ex)

Explanation:

  • First, install the SDK: pip install azure-storage-blob
  • Replace YOUR_AZURE_STORAGE_CONNECTION_STRING with your actual storage account connection string. You can find this in the Azure Portal under your storage account's 'Access keys' section.
  • The BlobServiceClient is the primary interface for interacting with Blob Storage.
  • create_container() method is used to create the container.

Similar examples are available for other SDKs like .NET, Java, Node.js, and Go. Refer to the official Azure SDK documentation for details.

Container Access Levels

When creating a container, you can specify its public access level:

Level Description Use Case
Private No anonymous access is permitted. Requests must be authenticated. This is the default and recommended setting. Sensitive data, internal application data.
Blob Anonymous read access is permitted for blobs only. Container metadata and properties are not accessible. Publicly accessible images, documents, or other files.
Container Anonymous read access is permitted for blobs and container metadata. When you need to list blobs within a container publicly. Use with caution.