python - 使用 Blob 存储触发器将文件解压缩到 Blob 存储中

标签 python azure azure-functions unzip azure-blob-trigger

我有一个任务,需要从 Azure 存储容器中获取一个压缩文件,并将解压缩的内容吐回到所述容器中...我已经使用 python 创建了一个 blob 触发器来尝试完成此任务。

据我所知,通常使用 python 解压文件的人都会使用这种方法

import zipfile
with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
    zip_ref.extractall(directory_to_extract_to)

但是,我似乎无法将该解决方案与我的云编程混合起来。

这是我到目前为止所拥有的:

import logging

import azure.functions as func
import zipfile
from azure.storage.blob import ContainerClient
from io import BytesIO

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    if myblob.name.endswith('.zip'):
        blob_name = myblob.name.split('/')[1]
        container_str_url = 'my_url'
        container_client = ContainerClient.from_container_url(container_str_url)
        #blob client accessing specific blob
        blob_client = container_client.get_blob_client(blob= blob_name)
        #download blob into memory
        stream_downloader = blob_client.download_blob()
        stream = BytesIO()
        stream_downloader.readinto(stream)

        with zipfile.ZipFile(stream, 'r') as zip_ref:
            zip_ref.extractall()

我正在将压缩文件下载到内存中,然后尝试使用传统方法将内容解压缩回容器中。

这样做时,触发器不会返回错误,但我可以看到程序何时到达 zip_ref.extractall()

作为代码的一部分,它发出一个 GET 请求,仅返回有关文件的信息,而不是实际(据我所知)在任何地方提取内容。

我被困在这里,我的总体目标只是解压缩存储容器中找到的文件并将内容重新上传回所述容器。任何帮助将不胜感激。

最佳答案

从我这边复制后,我可以使用下面的代码来实现。

import logging
import azure.functions as func
from azure.storage.blob import BlobServiceClient
import zipfile
import os

blob_service_client = BlobServiceClient.from_connection_string("<YOUR_CONNECTION_STRING>")
dir_path = r'<PATH_OF_EXTRACTED_FILES>'

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")

    container_client = blob_service_client.get_container_client("<INPUT_BLOB_CONTAINER>")
    blob_client = container_client.get_blob_client("<ZIP_FILE_NAME>")

    // Downloading Zip to local system
    with open("sample1.zip", "wb") as my_blob:
        download_stream = blob_client.download_blob()
        my_blob.write(download_stream.readall())
        
    // Extracting Zip Folder to path
    with zipfile.ZipFile("sample1.zip", 'r') as zip_ref:
        zip_ref.extractall(dir_path)
        
    // Reading and uploading Files to Storage account
    fileList = os.listdir(dir_path)
    for filename in fileList:
        container_client_upload = blob_service_client.get_container_client("<OUTPUT_BLOB_CONTAINER>")
        blob_client_upload = container_client_upload.get_blob_client(filename)

        f = open(dir_path+'\\'+filename, 'r')
        byt = f.read()
        blob_client_upload.upload_blob(byt, blob_type="BlockBlob")

首先,我使用 download_blob() 下载 Zip 文件,然后使用 extractall(dir_path) 提取 zip 文件,然后使用 upload_blob() 上传提取的文件.

结果:

Zip 文件内的文件

enter image description here

在存储帐户中提取后的文件

enter image description here

关于python - 使用 Blob 存储触发器将文件解压缩到 Blob 存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74395710/

相关文章:

python - Django 项目 : production server, 使用 python 3

python - 如何在两个字典中找到匹配值的字典键?

asp.net - SendGrid 不从 Azure Windows 2012 VM 发送电子邮件

azure - LINQ to SQL 与 SQL Azure

azure - 使用 Terraform 和函数扩展版本 4 创建函数应用程序

Python 关键字参数解包并返回字典

azure - 在具有多个属性的 Graph API 查询中使用 $expand?

postgresql pg_database_size 在随机时间抛出异常

Azure函数应用程序: Authentication Breaks Development Portal

python - subprocess.Popen().pid 返回父脚本的 pid