数据工厂 - 如何解压缩受密码保护的文件而无需外部代码

问题描述 投票:0回答:1

我有一个正在运行的管道,其中有一个复制数据活动,该活动解压缩我在其中提供文件路径的文件。当遇到受密码保护的 zip 文件时,该过程将失败。

我能够在执行管道时对密码进行硬编码,但不确定 ADF 是否有适当的流程来处理该过程。

azure azure-data-factory
1个回答
0
投票

根据这个

目前数据工厂不支持读取和生成受密码保护的文件。

另一种解决方法是使用 Azure 函数解压缩受密码保护的 zip 文件。

使用以下代码创建 HTTP 触发器 Azure 函数:

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 recommended 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")

Azure 数据工厂运行它。欲了解更多信息,您可以参考这个SO答案

© www.soinside.com 2019 - 2024. All rights reserved.