从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 分钟前
2026机房动环监控选型指南:场景规模需求、市场发展占有率和竞争梯队报告解析
大数据·运维·网络·人工智能·嵌入式硬件·机房动环监控·机房环境安全
夜雪一千4 分钟前
Python URL解码详解:单层、双重URL解码实战与踩坑指南
linux·服务器·python
程序员_小兵4 分钟前
PCIe Switch Fabric 技术解析
服务器·c语言·嵌入式硬件·php·fabric
VIP_CQCRE5 分钟前
用 Ace Data Cloud 一站式接入 AI 视频生成:HappyHorse Videos API 实战指南
python·aigc·api·ai视频·acedatacloud
Zs05096 分钟前
服务器使用率高处理思路
服务器
我不是程序员三三7 分钟前
企业风控实战:文件外发控制软件如何补齐数据泄露的出口短板
网络
咯哦哦哦哦9 分钟前
配置VNC sever 6.11.0版本 linux(激活码)
linux·运维·服务器
kali-Myon10 分钟前
定向 FGSM 攻击:让神经网络《指猫为狗》
图像处理·python·神经网络·安全·机器学习·fgsm
傲世仙尊12 分钟前
序列化与自定义协议-把结构体拆成字节流再拼回来
linux·服务器·网络·arm开发
老歌老听老掉牙12 分钟前
旅行商问题:从数学模型到 Python 实战
开发语言·python