Search code examples
pythonazure-blob-storageazure-sdk-python

How to create a blob container in Azure using Python?


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 below error.

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 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 hereaccount_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'


Solution

  • I see that you are trying to retrieve the connection_str with os.getenv. However, if the connection_str is not a environment value this method returns None which is probably the case since your error states AttributeError: 'NoneType' object has no attribute 'rstrip'.

    Adding the connection_str to your environment variables will probably solve your error. Alternatively, you can also create an argument for the connection_str in the create_container_sample() method and then passing the connection_str as a variable for the sake of testing your code.