【问题标题】:Cannot import name BlockBlobService from azure.storage.blob无法从 azure.storage.blob 导入名称 BlockBlobService
【发布时间】:2020-12-01 23:57:09
【问题描述】:

虽然有很多与我所问的问题类似的问题,但没有一个对我有帮助。

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

这是我正在使用的脚本:

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

虽然,如果我正在运行脚本,我会收到以下错误:

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)

这是pip freeze的结果:

azure-cognitiveservices-vision-computervision==0.6.0

azure-common==1.1.25

azure-core==1.8.0

azure-storage-blob==12.3.2

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    v12 sdk 使用BlobServiceClient 而不是BlockBlobService,如果你想使用BlockBlobService 应该使用v2 sdk。 v12 sdk的使用请参考这个official doc

    所以请改成from azure.storage.blob import BlobServiceClient, PublicAccess

    【讨论】:

    • 谢谢,它成功了。实际上,我正在使用 microsoft 的 ocr 打印手写文档的文本,我想使用脚本将其上传到 azure 上,效果很好
    • 但是,当我在该图像上使用 ocr 读取时,我得到了运行状态的输出以及具有成功状态的 alomg,这在进一步操作中产生了问题,我该如何解决它?
    • @nipan09,你能提出一个新问题吗?然后让我知道新的网址?
    猜你喜欢
    • 2018-11-07
    • 2019-12-16
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 2021-02-27
    • 2020-09-22
    • 2021-12-04
    相关资源
    最近更新 更多