1

使用示例代码 example.py(随 Azure 文档提供:快速入门:使用 Python 上传、下载和列出 blob)时,我收到以下导入错误。

文档链接:https ://github.com/Azure-Samples/storage-blobs-python-quickstart/blob/master/example.py

ImportError:没有名为 azure.storage.blob 的模块
-------------------------------------------------- -------------------------
ImportError Traceback(最近一次调用最后一次)
 在 ()
      1 导入操作系统、uuid、sys
----> 2 从 azure.storage.blob 导入 BlockBlobService,PublicAccess
      3

请帮我解决这个问题。

由于它在 Azure 云上的笔记本中运行,因此不涉及 python 安装。所以,请不要回复我应该使用不同版本的 python 的建议。

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

def run_sample():
    try:
        # Create the BlockBlockService that is used to call the Blob service for the storage account
        block_blob_service = BlockBlobService(account_name='accountname', account_key='accountkey')

        # Create a container called 'quickstartblobs'.
        container_name ='quickstartblobs'
        block_blob_service.create_container(container_name)

        # Set the permission so the blobs are public.
        block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container)

        # Create a file in Documents to test the upload and download.
        local_path=os.path.expanduser("~/Documents")
        local_file_name ="QuickStart_" + str(uuid.uuid4()) + ".txt"
        full_path_to_file =os.path.join(local_path, local_file_name)

        # Write text to the file.
        file = open(full_path_to_file,  'w')
        file.write("Hello, World!")
        file.close()

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

        # Download the blob(s).
        # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
        full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
        print("\nDownloading blob to " + full_path_to_file2)
        block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file2)

        sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
                         "application will exit.")
        sys.stdout.flush()
        input()

        # Clean up resources. This includes the container and the temp files
        block_blob_service.delete_container(container_name)
        os.remove(full_path_to_file)
        os.remove(full_path_to_file2)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    run_sample()
4

2 回答 2

1

据我所知,您可以在 NoteBook 单元格中使用 pip intall ,例如:

pip install azure-storage-file
于 2021-06-20T23:13:15.880 回答
0

您必须在您的环境中添加一个库 https://docs.databricks.com/user-guide/libraries.html#install-a-library-on-a-cluster

于 2019-01-09T16:41:12.320 回答