Using SCF to Decompress Zip Packages

Last updated: 2020-05-09 09:39:27


    This scenario involves the use of SCF and COS. Assume that you want to unzip (decompress) a ZIP file uploaded to COS, and upload the unzipped file back to COS using the same file name. You can extend the sample code to do more things such as decompressing files in other formats.

    As the temporary storage capacity allocated by SCF for each execution is currently 512 MB, it is recommended to keep the size of a single ZIP file below 300 MB and the size of a single decompressed file below 200 MB.


    Creating a bucket

    1. Log in to the COS Console.
    2. Create a source bucket to store the uploaded ZIP file, name it "zip-upload", select Beijing for its region, and select Private Read/Write for its access permission.
    3. Create a destination bucket to store the decompressed files, name it "unzip", select Beijing for its region, and select Private Read/Write for its access permission.

    For more information on how to create a bucket and set access permissions for it, see Creating a Bucket and Setting Access Permissions.

    Creating an SCF function

    1. Log in to the SCF Console, and enter the Function Service page.
    2. Select Beijing region and click Create to enter the function creating page.
    3. Configure the following fields:
      • Function Name: enter "unzip_to_cos".
      • Creation Method: select "Function Template".
      • Fuzzy Search: enter “unzip” as search keyword, and select one of the optional unzipping templates (Currently, they only support ZIP format. For Rar, 7z, or any other formats, please expand the function code.) Click Learn More for the function template you want, and you can view its details and download it.
    4. Click Next to enter the function configuration page. Keep the default configuration, and click Completed to complete the function creation.
    5. Click Function Code, and modify the following parameters in the function code editor as per the comments. Once completed, click Save.
      • appid: you can check it in Account information.
      • secret_id and secret_key: you can check them in API Key Management.
      • region: the region where the destination bucket resides. Enter ap-beijing.
      • bucket_upload: enter unzip-125xxxxxxx.
      • password: the password for decompressing the zipped file. Leave it empty if you don’t want one.
    6. Click Function Configuration, change the timeout period of the function to 100 seconds, and click Save. In actual executions, if the function times out, you can increase the timeout period as needed.

    Configuring a COS trigger

    1. Create the function as instructed in the above steps.
    2. Select Trigger Method>Add Trigger to add a COS trigger for the function, configure the following items, and click Save.
      • Trigger Method: select "COS trigger".
      • COS Bucket: select "zip-upload".
      • Event Type: select "All creation events". Keep the default values for other parameters.

    Testing the function

    1. Download the test sample in ZIP format.
    2. Go to the COS Console, select the created bucket "zip-upload", and click Upload File.
    3. In the Upload File window that pops up, select the test sample downloaded in step 1 and click Upload.
    4. Enter the other bucket "unzip" and you can view the decompressed files.
    5. Go to the SCF Console, and view the execution result. Select Function Service>Functions>Execution Log to check the printed log.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback