1

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'
2
  • Can you try by changing self.connection_str to just connection_str? Commented Oct 10, 2021 at 3:30
  • blob_service_client = BlobServiceClient.from_connection_string(connection_str) NameError: name 'connection_str' is not defined ----It doesnt work if we dont call with self Commented Oct 11, 2021 at 4:07

3 Answers 3

1

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()

enter image description here

OUTPUT

enter image description here

Successfully created container in Azure Portal

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

I tried with your code and still see the same issue. I tried to see if the connection string env variable is causing the issue. I ran the below code. import os, uuid from azure.storage.blob import BlobServiceClient,BlobClient,ContainerClient,version print('URL' in os.environ) connection_str = os.getenv("URL") It returned false. However i dont understand as i set the env variable
@aravindhchimtamneedi print('URL' in os.environ) is returning false means not set the env variable,Set the env variable properly and run the code again
appreciate your help. I fixed the env variable issue and now that i see the below error. True pystrgdemo10112021.blob.core.windows.net Traceback (most recent call last) blob_service_client = BlobServiceClient.from_connection_string(connection_str)e-packages\azure\storage\blob_blob_service_client.py", line 174, in from_connection_string account_url, secondary, credential = parse_connection_str(conn_str, credential, 'blob') raise ValueError("Connection string is either blank or malformed.") ValueError: Connection string is either blank or malformed.
I even used connection string from access keys instead of the storage account DNS. I was encountered with the error below File "C:\Python-InstallPath\lib\site-packages\azure\storage\blob_shared\base_client.py", line 333, in send return self._transport.send(request, **kwargs) File "C:\Python-InstallPath\lib\site-packages\azure\core\pipeline\transport_requests_basic.py", line 330, in send raise error azure.core.exceptions.ServiceRequestError: <urllib3.connection.HTTPSConnection object at 0x000001A5EB909220>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed
can you please provide the connection string format?
|
0

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.

Comments

0

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

https://learn.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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.