从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}")

相关推荐
云飞云共享云桌面28 分钟前
传统工作站 vs 云飞云共享云桌面:制造业设计云桌面选型深度对比
运维·服务器·前端·网络·3d·架构·制造
闵孚龙38 分钟前
动态图机制:为什么 PyTorch 调试起来更舒服
人工智能·pytorch·python
chushiyunen1 小时前
langchain4j笔记、tools
笔记·python·flask
JAVA面经实录9172 小时前
操作系统面试题
java·服务器·数据库·计算机网络·面试
程序员三藏2 小时前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
在放️2 小时前
Python 爬虫 · 第三方代理接入与合规使用
开发语言·爬虫·python
小刘|2 小时前
Spring AI Alibaba 集成和风天气 API 实战
java·服务器·前端
财经资讯数据_灵砚智能3 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年6月14日
大数据·人工智能·python·ai·信息可视化·自然语言处理·灵砚智能
森G3 小时前
61、信号与槽机制在 TCP 编程中的应用---------网络编程
网络·c++·qt·网络协议·tcp/ip
Maynor9963 小时前
我用 Codex 给自己的网站上线了一个智能体客服:从 Dify 到服务器部署,全程实战复盘
运维·服务器