python-3.x - Azure Function-使用 python 代码解压缩受密码保护的文件

标签 python-3.x azure azure-functions azure-function-app

我正在尝试解压缩存储在 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 而不是本地文件。

谢谢。

最佳答案

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)

关于python-3.x - Azure Function-使用 python 代码解压缩受密码保护的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65612148/

相关文章:

android - 如何从android上传图像到windows azure mobile服务?

c# - 使用 Azure 函数的简单发布请求

Azure 函数、部署槽和 API 管理 : Function key issue

c# - 从 Function App ILogger (C#) 在 Application Insights 中记录自定义对象

Python - 编辑 CSV 文件中的特定单元格

python - 将 Html 页面中的数据获取到 Python 数组中

c# - Azure 上的 .Net Core Web API 不提供 API 端点,仅提供默认起始页

c# - 第三方使用的 Azure 函数 REST API - 是否需要使用 ConfigureAwait(false)?

python-3.x - 对于来自 seaborn 的计数图,Matplotlib 的替代方案是什么?

python 隐式命名空间包未使用 setuptools 安装