gpt4 book ai didi

django - 如何使用 get_blob_to_stream 从 azure-storage 下载数据

转载 作者:行者123 更新时间:2023-12-02 06:22:43 29 4
gpt4 key购买 nike

我的 azure 存储帐户中有一些文件。我需要使用 get_blob_to_stream 下载它们。它返回 azure.storage.blob.models.Blob 对象。所以我无法使用下面的代码下载它。

def download(request):
file_name=request.POST['tmtype']
fp = open(file_name, 'wb')
generator = block_blob_service.list_blobs(container_name)
for blob in generator:
print(blob.name)
if blob.name==file_name:
blob=block_blob_service.get_blob_to_stream(container_name, blob.name, fp,max_connections= 2)
response = HttpResponse(blob, content_type="image/png")
response['Content-Disposition'] = "attachment; filename="+file_name
return response

最佳答案

您实际上可以使用 get_blob_to_path 属性,下面是 python 中的示例:

from azure.storage.blob import BlockBlobService

bb = BlockBlobService(account_name='', account_key='')
container_name = ""
blob_name_to_download = "test.txt"
file_path ="/home/Adam/Downloaded_test.txt"

bb.get_blob_to_path(container_name, blob_name_to_download, file_path, open_mode='wb', snapshot=None, start_range=None, end_range=None, validate_content=False, progress_callback=None, max_connections=2, lease_id=None, if_modified_since=None, if_unmodified_since=None, if_match=None, if_none_match=None, timeout=None)

此示例将容器中名为“test.txt”的 blob 文件下载到 File_path“/home/Adam/Downloaded_test.txt”,如果您愿意,也可以保留相同的名称。您可以在 https://github.com/adamsmith0016/Azure-storage 中找到包括此示例在内的更多示例。

关于django - 如何使用 get_blob_to_stream 从 azure-storage 下载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55964805/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com