我正在尝试从 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))
结果:
关于python - 从 azure blob 存储下载文件的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74940269/