Azure Blob Storage Container Creation
Stage 1: Installing Azure Storage Blob Package
pip install azure-storage-blob
The command pip install azure-storage-blob is used to install the Azure Storage Blob package for Python using the pip package manager.
pip: pip is the package installer for Python, allowing users to install and manage Python packages easily.
install: The install command is used with pip to install a Python package.
azure-storage-blob: azure-storage-blob is the name of the Python package being installed. This package provides functionalities for interacting with Azure Blob Storage, allowing users to perform operations like uploading, downloading, and managing blobs (binary large objects) in Azure Storage.
After installation, we can use the functionalities provided by this package in Python scripts or applications to work with Azure Blob Storage.
Stage 2: Interacting with Azure Blob Storage in Python Using Azure Storage Blob Library
from azure.storage.blob import BlobServiceClient, ContainerClient
This line imports the BlobServiceClient and ContainerClient classes from the azure.storage.blob module. These classes are part of the Azure Storage Blob library, which facilitates the interaction with Azure Blob Storage in Python.
BlobServiceClient represents a client for the Azure Storage Blob service. It provides methods for interacting with blob containers, listing containers, and obtaining container clients.
ContainerClient represents a client for a specific container within Azure Blob Storage. It allows to perform various operations on the blob container, such as uploading and downloading blobs, listing blobs, and managing container properties.
Stage 3: Retrieving Shared Access Signature (SAS) Token for Azure Blob Storage Container
sasIncoming = dbutils.secrets.get(scope = "container-secret", key = "containerincoming")
This line retrieves a Shared Access Signature (SAS) token from Databricks Secrets. The scope parameter specifies the secret scope, which is like a container for grouping secrets, and the key parameter identifies the specific secret within that scope. In this case, it is fetching the SAS token with the key “containerincoming” from the “container-secret” scope.
A Shared Access Signature (SAS) token is a string generated based on the storage account key or a policy, granting limited access to specific resources in Azure Blob Storage. It is often used to provide temporary and controlled access to containers or blobs.
Stage 4: Defining Azure Blob Storage Account and Container Parameters
storage_account_name = "fromgee"
container_name = "checknew"
storage_account_name = “fromgee”: This variable holds the name of the Azure Blob Storage account. The storage account is a globally unique namespace that allows to store and retrieve blobs and other data objects.
container_name = “checknew”: This variable specifies the name of the Azure Blob Storage container. Containers are organizational units within a storage account that hold blobs. The container name must be unique within the storage account.
Stage 5: Creating an Azure Blob Storage Container
blob_service_client = BlobServiceClient(
account_url=f"https://{storage_account_name}.blob.core.windows.net",
credential=sasIncoming
)
# Create a container
container_client = blob_service_client.get_container_client(container_name)
try:
container_client.create_container()
print(f"Container '{container_name}' created successfully.")
except Exception as e:
print(f"Container creation failed. {e}")
blob_service_client = BlobServiceClient(…): This line creates a BlobServiceClient instance, representing a client for the Azure Blob Storage service. It takes two parameters:
account_url: The URL of the storage account, constructed using the specified storage_account_name. It includes the “https://” protocol and the “.blob.core.windows.net” endpoint.
credential: The Shared Access Signature (SAS) token (sasIncoming) used for authentication.
container_client = blob_service_client.get_container_client(container_name): This line creates a ContainerClient instance, representing a client for the specific Azure Blob Storage container named container_name. It is obtained from the BlobServiceClient using the get_container_client method.
Container Creation:
try block: The script attempts to create the container using the create_container method on the container_client.
except block: If an exception occurs during the container creation, an error message is printed.
print statements:
print(f”Container ‘{container_name}’ created successfully.”): If the container creation is successful, a confirmation message is printed.
print(f”Container creation failed. {e}”): If the container creation fails, an error message is printed, including details about the exception.