Azure Storage Namespace Package (Legacy V2 SDK)

3.1.0 · deprecated · verified Mon Apr 13

The `azure-storage-nspkg` package is an internal Microsoft Azure Storage namespace package, primarily associated with the V2 Python SDK for Azure Storage. This package served to define the `azure.storage` namespace, allowing older client libraries like `azure-storage-blob` (versions < 3), `azure-storage-file`, and `azure-storage-queue` to be imported under this common prefix. The V2 SDK is deprecated; users should migrate to the V12 SDK, which uses separate, hyphenated packages (e.g., `azure-storage-blob` version 12.x.x) and does not require or use `azure-storage-nspkg`.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to Azure Blob Storage using the *V12 SDK* (`azure-storage-blob` version 12.x.x), which is the currently recommended approach. This package (`azure-storage-nspkg`) is not required for the V12 SDK. Replace `youraccount` and `yourkey` with your actual Azure Storage account details or set the `AZURE_STORAGE_CONNECTION_STRING` environment variable.

import os
from azure.storage.blob import BlobServiceClient

connection_string = os.environ.get('AZURE_STORAGE_CONNECTION_STRING', 'DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey;EndpointSuffix=core.windows.net')

try:
    # Create the BlobServiceClient object which will be used to create a container client
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)

    # Create a unique name for the container
    container_name = 'quickstart-container-12345'
    
    # Create the container if it doesn't exist
    try:
        container_client = blob_service_client.create_container(container_name)
        print(f"Container '{container_name}' created successfully.")
    except Exception as e:
        print(f"Container '{container_name}' already exists or another error: {e}")
        container_client = blob_service_client.get_container_client(container_name)

    # Example: List blobs in the container
    print("Listing blobs...")
    blob_list = container_client.list_blobs()
    for blob in blob_list:
        print(f"\t{blob.name}")

except ValueError as e:
    print(f"Error: {e}. Please ensure AZURE_STORAGE_CONNECTION_STRING is set or configured correctly.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →