[Python] How to create a blob container in Azure using Python?

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am a novice in Python programming and trying to create a blob container using python. Even after following the documented steps, I see the error below.

    Here is my code:

    import os, uuid
    from azure.storage.blob import BlobServiceClient,BlobClient,ContainerClient,__version__

    class BlobSamples():
    print("Azure Blob Storage v" + __version__ + " - Python quickstart sample")
    connection_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
    print("Connection established to Azure storage account from the Python App")

    #--Begin Blob Samples-----------------------------------------------------------------
    def create_container_sample(self):
    # Instantiate a new BlobServiceClient using a connection string
    blob_service_client = BlobServiceClient.from_connection_string(self.connection_str)

    # Instantiate a new ContainerClient
    container_client = blob_service_client.get_container_client("mycontainer")

    try:
    # Create new container in the service
    container_client.create_container()
    # List containers in the storage account
    list_response = blob_service_client.list_containers()
    except Exception as ex:
    print('Exception:')
    print(ex)
    #main program
    sample = BlobSamples()
    sample.create_container_sample()


    Error:

    py ConnectionString.py
    Azure Blob Storage v12.9.0 - Python quickstart sample
    Connection established to Azure storage account from the Python App
    Traceback (most recent call last):
    File "C:\Technical docs\cloud computing\MS Azure\blob-quickstart-v12\menu-driven-strg-ops\ConnectionString.py", line 31, in <module>
    sample.create_container_sample()
    File "C:\Technical docs\cloud computing\MS Azure\blob-quickstart-v12\menu-driven-strg-ops\ConnectionString.py", line 16, in create_container_sample
    blob_service_client = BlobServiceClient.from_connection_string(self.connection_str)
    File "C:\Python-InstallPath\lib\site-packages\azure\storage\blob\_blob_service_client.py", line 174, in from_connection_string
    `enter code here`account_url, secondary, credential = parse_connection_str(conn_str, credential, 'blob')
    File "C:\Python-InstallPath\lib\site-packages\azure\storage\blob\_shared\base_client.py", line 363, in parse_connection_str
    conn_str = conn_str.rstrip(";")
    AttributeError: 'NoneType' object has no attribute 'rstrip'

    Continue reading...

Compartilhe esta Página