Azure 函数 - 使用 python 代码解压缩受密码保护的文件

Azure Function - Unzip password protected file using python code

我正在尝试解压缩存储在 Azure Blob 容器中的受密码保护的文件。我想在 Azure Blob 本身上提取它。我已经使用 Python 创建了一个 Azure Function App(目前它是基于定时器控制事件的)来测试东西 -

以下是我的代码 - 我不确定实现此目的的正确方法是什么

import datetime
import os, uuid
import azure.functions as func
import azure.storage.blob
from zipfile import ZipFile
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

def test_func():
    #get connection string to storage account
    connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')

    # BlobServiceClient object which will be used refer to a container client
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)

    # Create a unique name for the container
    container_name = "zipextract"
    container_client =  blob_service_client.get_container_client(container_name)

    blob_list = container_client.list_blobs()

    for blob in blob_list:
        print("----> " + blob.name)
        with ZipFile(blob.name) as zf:
            zf.extractall(pwd=b'password')   

现在,当我尝试使用 ZipFile() 函数访问文件时 - 它显示“没有这样的文件或目录:'TestZip.zip'”。

以下是错误信息 -(TestZip.zip 是放在 zipextract 容器中的 zip 文件)

Result: Failure Exception: FileNotFoundError: [Errno 2] No such file or 
directory: 'TestZip.zip' Stack: File "/azure-functions- 
host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 
271, in _handle__function_load_request func = loader.load_function( File 
"/azure-functions- 
host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", 
line 32, in call return func(*args, **kwargs) File "/azure-functions- 
host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 76, 
in load_function mod = importlib.import_module(fullmodname) File 
"/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module 
return _bootstrap._gcd_import(name[level:], package, level) File "<frozen 
importlib._bootstrap>", line 1014, in _gcd_import File "<frozen 
importlib._bootstrap>", line 991, in _find_and_load File "<frozen 
importlib._bootstrap>", line 961, in _find_and_load_unlocked File "<frozen 
importlib._bootstrap>", line 219, in _call_with_frames_removed File "<frozen 
importlib._bootstrap>", line 1014, in _gcd_import File "<frozen 
importlib._bootstrap>", line 991, in _find_and_load File "<frozen 
importlib._bootstrap>", line 975, in _find_and_load_unlocked File "<frozen 
importlib._bootstrap>", line 671, in _load_unlocked File "<frozen 
importlib._bootstrap_external>", line 783, in exec_module File "<frozen 
importlib._bootstrap>", line 219, in _call_with_frames_removed File 
"/home/site/wwwroot/TimerTrigger1/__init__.py", line 39, in <module> 
test_func() File "/home/site/wwwroot/TimerTrigger1/__init__.py", line 33, in 
test_func with ZipFile(blob.name) as zf: File 
"/usr/local/lib/python3.8/zipfile.py", line 1251, in __init__ self.fp = 
io.open(file, filemode)

关于如何解压缩此文件的任何帮助?解压缩在本地机器上运行良好,但是,我不确定如何使它 运行 以便它引用 blob 而不是本地文件。

谢谢。

ZipFile(blob.name) 正在本地文件系统中查找文件。

您需要download the blob到本地文件系统。

# Download blob to local file
local_file_path = os.path.join(".", blob.name)
with open(local_file_path, "wb") as download_file:
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name)
    download_file.write(blob_client.download_blob().readall())

# read local zip file
with ZipFile(local_file_path) as zf:
    zf.extractall(pwd=b'password')

zip 文件存储在 Azure blob 存储服务器中。它是一个远程服务器。我们不能使用 ZipFile(blob.name) 来访问它。我们需要先读取zip文件的内容然后解压。

例如

blob_service_client = BlobServiceClient.from_connection_string(conn_str)
container_client = blob_service_client.get_container_client('input')
blob_client = container_client.get_blob_client('sampleData.zip')
des_container_client = blob_service_client.get_container_client('output')
with io.BytesIO() as b:
    download_stream = blob_client.download_blob(0)
    download_stream.readinto(b)
    with zipfile.ZipFile(b, compression=zipfile.ZIP_LZMA) as z:
        for filename in z.namelist():
            if not filename.endswith('/'):
                print(filename)
                with z.open(filename, mode='r', pwd=b'') as f:
                    des_container_client.get_blob_client(
                        filename).upload_blob(f)