简体   繁体   中英

Unzip file in blob storage with blob storage trigger

I have a task where I need to take a zipped file from an Azure Storage Container and spit back out the unzipped contents into said container... I've created a blob trigger with python to try and accomplish this task.

From what I can tell, usually people who use python unzip files using this method

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

However, I can't seem to mix that solution with my cloud programming.

Here is what I have so far:

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()

I'm downloading the zipped file into memory and then I'm trying to use the traditional method to unzip the contents back into the container.

When doing so, the trigger doesn't return an error, but I can see when the program reaches zip_ref.extractall()

part of the code, it makes a GET request that just returns information about the file instead of actually (as far as I can tell) extracting the contents anywhere.

I'm stuck here, my overall goal is just to unzip the file found in the storage container and re-upload the contents back into the said container. Any help would be appreciated.

After reproducing from my end, I could able to achieve using the below code.

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

First I downloaded the Zip file using download_blob() then extracted the zip file using extractall(dir_path) and then uploaded the extracted files using upload_blob() .

RESULTS:

Files Inside Zip file

在此处输入图像描述

Files after extraction in Storage account

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM