简体   繁体   中英

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 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 attri bute 'rstrip'

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.

I tried to reproduce the scenario in my system.

Please check with you added the environment variables properly. Use
'URL' in os.environ to check environment present or not (true or false)

Add Environment variable in command prompt

set URL=https://pythonazurestorage12345.blob.core.windows.net

set

Try with this code

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

print('URL' in os.environ)

connection_str = os.getenv("URL")
blob_service_client = BlobServiceClient.from_connection_string(connection_str)
        
        # Instantiate a new ContainerClient
container_client = blob_service_client.get_container_client("testcontainers")
container_client.create_container()

在此处输入图片说明

OUTPUT

在此处输入图片说明

Successfully created container in Azure Portal

在此处输入图片说明

I was having the same error and with no solution. Until I read the documentation from the Azure, they slighly changed something.

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>"

After this you need to restart your editor. And everything will work. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM