无法从azure.storage.blob导入名称BlockBlobService

问题描述

尽管有很多与我要问的问题类似的问题,但没有一个我有帮助。@H_404_1@

我正尝试使用python脚本直接通过系统将文件存储到我的Azure存储blob中。@H_404_1@

这是我正在使用的脚本:@H_404_1@

import os,uuid,sys
from azure.storage.blob import BlockBlobService,PublicAccess

def run_sample():
    try:
        block_blob_service = BlockBlobService(account_name=<acc-name>,account_key=<acc-key>)

        # Create a file in Documents to test the upload and download.
        local_path=os.path.abspath(os.path.curdir)
        local_file_name =input("Enter file name to upload : ")
        full_path_to_file =os.path.join(local_path,local_file_name)

        print("Temp file = " + full_path_to_file)
        print("\nUploading to Blob storage as blob" + local_file_name)

        # Upload the created file,use local_file_name for the blob name
        container_name ='handwritten-text'
        block_blob_service.create_blob_from_path(container_name,local_file_name,full_path_to_file)

        # List the blobs in the container
        print("\nList blobs in the container")
        generator = block_blob_service.list_blobs(container_name)
        for blob in generator:
            print("\t Blob name: " + blob.name)
    
    except Exception as e:
        print(e)


# Main method.
if __name__ == '__main__':
    run_sample()

尽管,如果我正在运行脚本,则会收到以下错误消息:@H_404_1@

ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob' (/home/nishant/anaconda3/envs/mera_env/lib/python3.7/site-packages/azure/storage/blob/__init__.py)

这是点冻结的结果:@H_404_1@

azure-cognitiveservices-vision-computervision==0.6.0

azure-common==1.1.25

azure-core==1.8.0

azure-storage-blob==12.3.2

如何解决该问题?@H_404_1@

解决方法

v12 sdk使用BlobServiceClient代替BlockBlobService,如果要使用BlockBlobService,则应使用v2 sdk。有关v12 sdk的用法,请参阅此official doc

因此,请将其更改为from azure.storage.blob import BlobServiceClient,PublicAccess