我希望下载文件并从Azure读取该文件。容器名称为:“cont”。我们在容器内有一些文件夹,我们需要从名为“special”的文件夹下载。在此文件夹中,我们有6个不同的文件夹,aaa,bbb,ccc,ddd,mmm,ppp。我们需要在特定时间从mmm和ppp下载period.in此文件夹文件的格式为“zip”。例如,ppp 2023-01-09 11:00:00.zip。这是我的代码。在此代码中,只有1个文件从其他文件夹ddd下载
import os
from datetime import datetime
from dateutil import tz
from azure.storage.blob import BlobServiceClient
start_time_str = input("Enter start time (format: yyyy-mm-dd hh:mm:ss): ")
end_time_str = input("Enter end time (format: yyyy-mm-dd hh:mm:ss): ")
start_time = datetime.fromisoformat(start_time_str).replace(tzinfo=tz.tzlocal())
end_time = datetime.fromisoformat(end_time_str).replace(tzinfo=tz.tzlocal())
print("Start time:", start_time)
print("End time:", end_time)
# Create a BlobServiceClient object
connection_string = <"connection string">
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_name = "sp"
container_client = blob_service_client.get_container_client(container_name)
local_path = "C:/Users/aaa/Downloads/"
for blob in container_client.list_blobs():
blob_client = container_client.get_blob_client(blob)
blob_props = blob_client.get_blob_properties()
last_modified = blob_props.last_modified.astimezone(tz.tzlocal())
if last_modified >= start_time and last_modified <= end_time:
download_path = os.path.join(local_path, blob.name.split("/")[-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}")
1条答案
按热度按时间9q78igpj1#
这是因为你没有提到任何具体的文件夹下载文件。从我这边复制后,我可以得到这个工作使用下面的代码。
结果: