gpt4 book ai didi

python - Azure blob存储目录下载(win,python)

转载 作者:行者123 更新时间:2023-12-02 08:11:57 26 4
gpt4 key购买 nike

我的目标是下载 Blob 存储的一部分,该存储看起来像其他目录中的目录。

官方Download to a file path函数请求一个我不知道如何使用的 self 参数,并且其他堆栈溢出问题经常建议 outdated solutions implementing BlockBlobService 。我还找到了this但好像不行。

一般来说,我似乎必须循环blob_list,但由于不同的原因,我在本地重新创建blob存储中“目录”的结构时遇到了麻烦。有时我有 .parquet 文件,它们实际上是具有快速压缩等功能的文件夹。到目前为止我有这段代码:

import pathlib
blob_service_client = BlobServiceClient.from_connection_string(conn_str)
container_client = blob_service_client.get_container_client(container="container_name")
blob_list = container_client.list_blobs("dir_1/sub_dir_2/desired_dir/")
local_directory = pathlib.Path("local_directory/")

for blob in blob_list:
local_file_path = local_directory / pathlib.Path(blob.name)
local_file_path.parent.mkdir(parents=True, exist_ok=True)
with open(local_file_path, "wb") as local_file:
print('yes it is downloaded')
blob_client = container_client.get_blob_client(blob)
blob_data = blob_client.download_blob()
blob_data.readinto(local_file)

当前在 win 计算机上下载子目录以镜像 azure blob 存储上的结构的最佳方法是什么?

最佳答案

从我这边复制后,我可以使用os模块来实现这一点。下面是对我有用的完整代码。

from azure.storage.blob import BlobServiceClient
import os

ACCOUNT_NAME = "<YOUR_ACCOUNT_NAME>"
CONTAINER_NAME = "<CONTAINER>"

blob_service_client = BlobServiceClient.from_connection_string("<STORAGE_ACCOUNT_CONNECTION_STRING>")
container_client = blob_service_client.get_container_client(CONTAINER_NAME)

local_path = "C:\\Users\\<YOUR_PATH>\\Downloads\\folder"
for blob in container_client.list_blobs():
blob_client = container_client.get_blob_client(blob)
blob_props = blob_client.get_blob_properties()
download_path = os.path.join(local_path+"\\"+"\\".join(blob.name.split("/")[1:-1]), blob.name.split("/")[-1])
if(os.path.exists(local_path+"\\"+"\\".join(blob.name.split("/")[1:-1]))):
with open(download_path, "wb") as download_file:
download_stream = blob_client.download_blob()
download_file.write(download_stream.readall())
print(f"Downloaded blob: {blob.name}")
else:
os.makedirs(local_path+"\\"+"\\".join(blob.name.split("/")[1:-1]))
with open(download_path, "wb") as download_file:
download_stream = blob_client.download_blob()
download_file.write(download_stream.readall())
print(f"Downloaded blob: {blob.name}")

在存储帐户中:

enter image description here

这里的路径是从 dir2 到文件位置。

本地结果:

enter image description here

关于python - Azure blob存储目录下载(win,python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75724115/

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