python - 从 azure blob 存储下载文件的替代方法

标签 python azure azure-blob-storage

我正在尝试从 azure blob 存储下载文件,并且希望在不使用任何文件处理程序或打开或关闭方法的情况下下载它。

这是我的方法,我想要一种不使用“with open()”的替代方法

`

def download_to_blob_storage(CONTAINERNAME ,remote_path,local_file_path):
    client = BlobServiceClient(STORAGEACCOUNTURL, credential=default_credential)
    blob_client = client.get_blob_client(container =CONTAINERNAME,blob = remote_path)

    with open(local_file_path, "wb") as my_blob:
       download_stream = blob_client.download_blob()
       my_blob.write(download_stream.readall()) 
    print('downloaded'+remote_path+'file')

download_to_blob_storage(CONTAINERNAME , '/results/wordcount.py',"./wordcount.py")

`

最佳答案

对于这个问题,首先要确定你想做什么。

你要实现的“下载”是一个什么样的概念?如果你只是想获取文件内容,我可以为你提供一个方法。但是如果你想避开python的I/O机制来实现创建和写入文件,那么这是不可能的。 file的open方法是python原生最基本的方法,如果你想要一个文件可以存储在磁盘上,你不可避免地会调用这个方法。

为您提供的 Python 演示:

from azure.storage.blob import BlobClient, ContainerClient


account_url = "https://xxx.blob.core.windows.net"
key = "xxx"
container_name = "test"
blob_name = "test.txt"
# Create the ContainerClient object
container_client = ContainerClient(account_url, container_name, credential=key)

# Create the BlobClient object
blob_client = BlobClient(account_url, container_name, blob_name, credential=key)

#download the file without using open
file_data = blob_client.download_blob().readall()

print("file content is: " + str(file_data))

结果:

enter image description here

关于python - 从 azure blob 存储下载文件的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74940269/

相关文章:

python - 将 new.module() 移植到 Python3

python - 删除 3D numpy 数组中的行

python - 类和函数的装饰器

azure - 有条件地将数据从一个 SQL 实例同步到另一实例

azure - 如何使用 Azure 中的串行控制台连接到 VM

具有 Azure Blob 存储操作的 Azure 逻辑应用程序 : Getting 429 statusCode error

azure - 我收到模拟和真实 Azure 存储的错误请求 (400) 消息

python - 在 Python 中制作唯一对象列表

c# - 如何在公共(public)访问设置为 'Private' 的容器中复制 blob?

azure - 使用 azure 数据工厂获取 azure blob 元数据键值