Azure功能-使用python代码解压缩受密码保护的文件 [英] Azure Function - Unzip password protected file using python code

查看:75
本文介绍了Azure功能-使用python代码解压缩受密码保护的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解压缩存储在Azure Blob容器中的受密码保护的文件.我想将其提取到Azure Blob本身.我已经使用Python创建了一个Azure Function应用程序(当前它是基于Timer Control事件的)以测试事物-

I am trying to unzip password protected file which is stored on Azure Blob container. I want to extract it on Azure Blob itself. I have created a Azure Function App using Python (currently it is Timer Control event based) to test things -

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

Following is my code - I am not sure what would be the correct way to achieve this

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'".

Now, when I am trying to access file using ZipFile() function - it says "No such file or directory: 'TestZip.zip' ".

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

Following is the error message - (TestZip.zip is the zip file placed on zipextract container)

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而不是本地文件.

Any help as to how would I unzip this file? The unzipping is working fine on local machine, however, not sure how would I make it run so that it refers to blob instead of local file.

谢谢.

推荐答案

zip文件存储在Azure blob存储服务器中.它是一台远程服务器.我们无法使用 ZipFile(blob.name)进行访问.首先,我们需要阅读zip文件的内容,然后将其解压缩.

The zip files are stored in Azure blob storage server. It is a remote server. We cannot use ZipFile(blob.name) to access it. We need to read the zip file's content at first then unzip it.

例如

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)

这篇关于Azure功能-使用python代码解压缩受密码保护的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆