简体   繁体   中英

Cannot import name BlockBlobService from azure.storage.blob

Although, there are many similar questions to the one I am asking, but non of them have helped me.

I am trying to store the file into my azure storage blob through directly through my system using the python script.

Here is the script I am using:

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

Although, if I am running the script, I am getting the error as:

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)

Here is the result of 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

How can I resolve the issue?

v12 sdk uses BlobServiceClient instead of BlockBlobService , if you want to use BlockBlobService should use v2 sdk . For v12 sdk usage, please refer to this official doc .

So please change it to from azure.storage.blob import BlobServiceClient, PublicAccess .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM