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

相关推荐
布局呆星1 分钟前
面向对象中的封装-继承-多态
开发语言·python
sxy_97614 分钟前
AX86u官方固件温度监控(CPU,WIFI芯片)
python·docker·curl·nc·nas·温度·ax86u
诗词在线4 分钟前
适合赞美风景的诗词名句汇总
python·风景
学嵌入式的小杨同学7 分钟前
【嵌入式 C 语言实战】栈、队列、二叉树核心解析:存储原理 + 应用场景 + 实现思路
linux·c语言·网络·数据结构·数据库·后端·spring
2401_8414956410 分钟前
【LeetCode刷题】删除链表的倒数第N个结点
数据结构·python·算法·leetcode·链表·遍历·双指针
2501_9436953315 分钟前
中专学历如何通过数据分析晋升店长
运维·服务器
舰长11519 分钟前
SSLITLS协议信息泄露漏洞(CVE-2016-2183)【原理扫描】
网络
Non-existent98720 分钟前
地理空间数据处理指南 | 实战案例+代码TableGIS
人工智能·python·数据挖掘
optimistic_chen20 分钟前
【Docker入门】Docker Registry(镜像仓库)
linux·运维·服务器·docker·容器·镜像仓库·空间隔离
智算菩萨30 分钟前
互联网的神经网络:OSI七层分层模型深度讲解
网络