簡體   English   中英

如何使用 Python 在 Azure 中創建 Blob 容器?

[英]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 快速入門示例從 Python App Traceback 建立到 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 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' 對象沒有屬性 'rstrip'

我看到您正在嘗試使用os.getenv檢索connection_str 但是,如果connection_str不是環境值,則此方法返回None這可能是這種情況,因為您的錯誤狀態AttributeError: 'NoneType' object has no attribute 'rstrip'

connection_str添加到您的環境變量可能會解決您的錯誤。 或者,您也可以在create_container_sample()方法中為connection_str創建一個參數,然后將connection_str作為變量傳遞以測試您的代碼。

我試圖在我的系統中重現這個場景。

請檢查您是否正確添加了環境變量。 利用
'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 門戶中成功創建容器

在此處輸入圖片說明

我遇到了同樣的錯誤並且沒有解決方案。 直到我從 Azure 閱讀文檔,他們才稍微改變了一些東西。

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

setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>"

在此之后,您需要重新啟動編輯器。 一切都會奏效。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM