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
- An Azure Storage account. If you don't have one, you can create one for free.
- Permissions to create containers within the storage account.
Using Azure CLI
The Azure Command-Line Interface (CLI) provides a powerful way to manage your Azure resources, including creating blob containers.
Steps:
- Make sure you have the Azure CLI installed and are logged in to your Azure account.
- Use the
az storage container createcommand.
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.
Using Azure Portal
The Azure Portal offers a user-friendly graphical interface for managing your storage account and its containers.
Steps:
- Sign in to the Azure Portal.
- Navigate to your storage account.
- In the left-hand menu, under Data storage, select Containers.
- Click the + Container button.
- Enter a name for your container.
- Choose the public access level (Private, Blob, or Container).
- Click Create.
Using Azure PowerShell
Azure PowerShell provides cmdlets for managing Azure resources. You can create containers using the Az.Storage module.
Steps:
- Install the Az.Storage module if you haven't already:
Install-Module -Name Az.Storage - Connect to your Azure account:
Connect-AzAccount - Use the
New-AzStorageContainercmdlet.
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 useStorageAccountKey,SasToken, or obtain the context from a logged-in user session. For production scenarios, consider using Azure AD authentication.
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.
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_STRINGwith your actual storage account connection string. You can find this in the Azure Portal under your storage account's 'Access keys' section. - The
BlobServiceClientis 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. |