简体   繁体   English

自动从 azure 存储解压缩受密码保护的 Zip 文件?

[英]Unzip Password Protected Zip file automatically from azure storage?

I'm just wondering is there a way to extract a password protected zip file from Azure Storage.我只是想知道有没有办法从 Azure 存储中提取受密码保护的 zip 文件。 I tried using a python Azure Function to no avail but had a problem reading the location of the file.我尝试使用 python Azure Function 无济于事,但在读取文件位置时遇到问题。

Would the file have to stored on a shared location temporarily in order to achieve?文件是否必须临时存储在共享位置才能实现?

Just looking for a bit of direction here am I missing a step maybe?只是在这里寻找一点方向,我可能错过了一步吗?

Regards, James问候,詹姆斯

Azure blob storage provides storing functionality only, there is no running env to perform unzip operation. Azure blob 存储仅提供存储功能,没有运行环境来执行解压缩操作。 So basically, we should download.zip file to Azure function, unzip it and upload files in.zip file 1 by 1. So basically, we should download.zip file to Azure function, unzip it and upload files in.zip file 1 by 1.

For a quick test, I write an HTTP trigger Azure function demo that unzipping a zip file with password-protected, it works for me on local: For a quick test, I write an HTTP trigger Azure function demo that unzipping a zip file with password-protected, it works for me on local:

import azure.functions as func
import uuid
import os
import shutil
from azure.storage.blob import ContainerClient
from zipfile import ZipFile

storageAccountConnstr = '<storage account conn str>'
container = '<container name>'

#define local temp path, on Azure, the path is recommanded under /home 
tempPathRoot = 'd:/temp/'
unZipTempPathRoot = 'd:/unZipTemp/'


def main(req=func.HttpRequest) -> func.HttpResponse:
    reqBody = req.get_json()
    fileName = reqBody['fileName']
    zipPass =  reqBody['password']

    container_client = ContainerClient.from_connection_string(storageAccountConnstr,container)

    #download zip file 
    zipFilePath = tempPathRoot + fileName
    with open(zipFilePath, "wb") as my_blob:
       download_stream = container_client.get_blob_client(fileName).download_blob()
       my_blob.write(download_stream.readall())

    #unzip to temp folder
    unZipTempPath = unZipTempPathRoot + str(uuid.uuid4())
    with ZipFile(zipFilePath) as zf:
        zf.extractall(path=unZipTempPath,pwd=bytes(zipPass,'utf8'))

    #upload all files in temp folder
    for root, dirs, files in os.walk(unZipTempPath):
        for file in files: 
            filePath = os.path.join(root, file)
            destBlobClient = container_client.get_blob_client(fileName + filePath.replace(unZipTempPath,''))
            with open(filePath, "rb") as data:
                destBlobClient.upload_blob(data,overwrite=True)
    
    #remove all temp files 
    shutil.rmtree(unZipTempPath)
    os.remove(zipFilePath)

    return func.HttpResponse("done")

Files in my container:我的容器中的文件: 在此处输入图像描述

Result:结果: 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

Using blob triggers will be better to do this as it will cause time-out errors if the size of your zip file is huge.使用 blob 触发器会更好地执行此操作,因为如果 zip 文件的大小很大,它将导致超时错误。

Anyway, this is only a demo that shows you how to do this.无论如何,这只是一个演示,向您展示如何做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 解压缩受密码保护的 zip 文件 - How do I unzip a password protected zip file using Python 为什么 python 无法解压缩由 winrar 使用 zip 方法创建的受密码保护的 zip 文件? - why can't python unzip a password protected zip file created by winrar using the zip method? 时间触发的 Python 中的 Azure 函数从 url 获取 zip 文件,解压缩,然后将文件输出到 Azure 存储中的 blob 容器 - Azure Function in Python that is on a time trigger get zip file from a url, unzip, then output file to blob container in Azure storage 如何在Python 3中打开受密码保护的zip文件 - How to open password protected zip file in Python 3 用python打开受密码保护的zip文件 - Open Password Protected zip file with python 使用Python的密码保护的zip文件 - Password protected zip file using Python 如何创建受密码保护的 Zip 文件? - How to Create Password Protected Zip file? 从 Memory 中的 FTP 下载 Zip 文件并解压缩 - Download Zip File From FTP in Memory and Unzip it 无法使用 python zipfile 库使用密码解压缩 a.zip 文件 - Unable to unzip a .zip file with a password with python zipfile library 使用请求和HTTPBasicAuth从受密码保护的站点检索zip文件 - Using requests and HTTPBasicAuth to retrieve a zip file from a password-protected site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM