【问题标题】:How to create a blob container in Azure using Python?如何使用 Python 在 Azure 中创建 blob 容器?
【发布时间】:2021-11-29 08:23:25
【问题描述】:
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 存储 v12.9.0 - Python 快速入门示例 从 Python 应用程序建立到 Azure 存储帐户的连接 回溯(最近一次通话最后): 文件“C:\Technical docs\cloud Computing\MS Azure\blob-quickstart-v12\menu-driven-strg-ops\ConnectionString.py”,第 31 行,在 sample.create_container_sample() 文件“C:\Technical docs\cloud Computing\MS Azure\blob-quickstart-v12\menu-driven-strg-ops\ConnectionString.py”,第 16 行,在 create_container_sample blob_service_client = BlobServiceClient.from_connection_string(self.connection_str) 文件“C:\Python-InstallPath\lib\site-packages\azure\storage\blob_blob_service_client.py”,第 174 行,在 from_connection_string enter code hereaccount_url, 二级, 凭证 = parse_connection_str(conn_str, 凭证, 'blob') parse_connection_str 中的文件“C:\Python-InstallPath\lib\site-packages\azure\storage\blob_shared\base_client.py”,第 363 行 conn_str = conn_str.rstrip(";") AttributeError:“NoneType”对象没有属性“rstrip”

【问题讨论】:

  • 你能试试把self.connection_str改成connection_str吗?
  • blob_service_client = BlobServiceClient.from_connection_string(connection_str) NameError: name 'connection_str' is not defined ----如果我们不使用 self 调用它就不起作用

标签: python azure-blob-storage azure-sdk-python


【解决方案1】:

我看到您正在尝试使用os.getenv 检索connection_str。但是,如果 connection_str 不是环境值,则此方法返回 None,这可能是因为您的错误状态为 AttributeError: 'NoneType' object has no attribute 'rstrip'

connection_str 添加到您的环境变量中可能会解决您的错误。或者,您也可以在create_container_sample() 方法中为connection_str 创建一个参数,然后将connection_str 作为变量传递以测试您的代码。

【讨论】:

    【解决方案2】:

    我试图在我的系统中重现该场景。

    请检查您是否正确添加了环境变量。使用
    'URL' in os.environ 检查环境是否存在(真或假)

    在命令提示符下添加环境变量

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

    设置

    试试这个代码

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

    输出

    在 Azure 门户中成功创建容器

    【讨论】:

    • 我尝试了您的代码,但仍然看到同样的问题。我试图查看连接字符串 env 变量是否导致问题。我运行了下面的代码。 import os, uuid from azure.storage.blob import BlobServiceClient,BlobClient,ContainerClient,version print('URL' in os.environ) connection_str = os.getenv("URL") 返回false。但是我不明白,因为我设置了 env 变量
    • @aravindhchimtamneedi print('URL' in os.environ) is return false 表示未设置环境变量,请正确设置环境变量并再次运行代码
    • 感谢您的帮助。我修复了环境变量问题,现在我看到了以下错误。 True pystrgdemo10112021.blob.core.windows.net Traceback(最近一次通话最后一次) blob_service_client = BlobServiceClient.from_connection_string(connection_str)e-packages\azure\storage\blob_blob_service_client.py",第 174 行,在 from_connection_string account_url,secondary,credential = parse_connection_str(conn_str, credential, ' blob') raise ValueError("连接字符串为空白或格式错误。") ValueError: 连接字符串为空白或格式错误。
    • 我什至使用了来自访问密钥的连接字符串,而不是存储帐户 DNS。我在发送返回 self._transport.send(request, **kwargs ) 文件“C:\Python-InstallPath\lib\site-packages\azure\core\pipeline\transport_requests_basic.py”,第 330 行,在发送中引发错误 azure.core.exceptions.ServiceRequestError: :建立新连接失败:[Errno 11001] getaddrinfo failed
    • 能否提供连接字符串格式?
    【解决方案3】:

    我遇到了同样的错误,但没有解决方案。在我阅读 Azure 的文档之前,他们稍微改变了一些东西。

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

    setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>"
    

    在此之后,您需要重新启动编辑器。一切都会奏效。 :)

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2011-02-06
      • 2020-09-15
      • 2013-02-23
      • 2011-02-24
      • 2019-12-08
      • 2015-04-23
      • 2016-08-31
      • 1970-01-01
      相关资源
      最近更新 更多