从blob 下载zip文件到本地并解压

def UnzipFile(req: func.HttpRequest) -> func.HttpResponse:

local_download_path="\\temp"

extract_to = "\\unzip"

req_body = req.get_json()

filePath = req_body.get("filePath")

logging.info(f'filePath:{filePath}')

创建服务客户端和文件系统客户端

service_client = get_service_client(os.getenv("CONNECTION_STRING"))

file_system_client = service_client.get_file_system_client(os.getenv("CONTAINER"))

logging.info(f'创建服务客户端和文件系统客户端 file_system_client')

try:

确保下载目录和解压目录存在

os.makedirs(os.path.join( os.getcwd(), local_download_path), exist_ok=True)

os.makedirs(os.path.join( os.getcwd(), extract_to), exist_ok=True)

except Exception as e:

logging.info(f'get local file path:{e}')

try:

下载压缩文件

local_zip_path = os.path.join( os.getcwd(), local_download_path, os.path.basename(filePath))

download_file(file_system_client, filePath, local_zip_path)

extract_to = os.path.join( os.getcwd(), extract_to)

except Exception as e:

logging.info(f'download_file failed:{e}')

logging.info(f'local_zip_path:{local_zip_path}')

logging.info(f'extract_to:{extract_to}')

解压文件

unzip_file(local_zip_path, extract_to)

data = {

'result': 'success',

'message': 'process success.',

'data': \[\],

'code': 200

}

将 Python 字典转换为 JSON 字符串

json_string = json.dumps(data, indent=4) # indent 参数用于格式化输出

return func.HttpResponse(

json_string,

status_code=200

)

def get_service_client(connection_string):

service_client = DataLakeServiceClient.from_connection_string(connection_string)

return service_client

def download_file(file_system_client, file_path, local_path):

try:

file_client = file_system_client.get_file_client(file_path)

download = file_client.download_file()

with open(local_path, "wb") as local_file:

local_file.write(download.readall())

print(f"Downloaded file to {local_path}")

except Exception as e:

print(e)

def unzip_file(zip_path, extract_to):

with zipfile.ZipFile(zip_path, 'r') as zip_ref:

zip_ref.extractall(extract_to)

print(f"Extracted all files to {extract_to}")

相关推荐
MoRanzhi12032 分钟前
第3篇 · 使用 PyQt5 实现 2048 桌面版游戏:MVC 架构与撤销和重做
python·mvc·pyqt5·桌面应用开发·gui开发·2048游戏·撤销与重做
xiaoye-duck3 分钟前
《C++ 项目:手写大模型接入 SDK》云端 API 调用基础与 Apifox 调试实战
网络·c++·语言模型
蚰蜒螟7 分钟前
从 Java SocketChannel 到 TCP 挥手:一次 close() 的完整调用链与引用计数解析
java·python·tcp/ip
Smoothcloud润云18 分钟前
GPU算力平台跑知识库问答,模型回答总跑偏?先排查检索链路
人工智能·python·深度学习·gpu算力·gpu
mklpo14720 分钟前
python3:如何按照实际执行需求导入模块,而不是启动时统一导入?(Python3.15)。2026-10-20
开发语言·人工智能·python
张文君26 分钟前
vmware克隆后ubuntu26.04 ip怎么更改
linux·运维·服务器
迪康coolmu38 分钟前
基于迪康端点安全一体化管理系统的 AI 工具使用审计实战
运维·网络·人工智能·安全·电脑
minglie11 小时前
zynq联网配置
网络·数据库·php
小此方1 小时前
Linux网络(二十九):从内网穿透到P2P打洞:NAT转换、代理机制与局域网通信的底层全景解析
linux·网络·p2p