Using SCF to Decompress Zip Packages

Last updated: 2020-02-26 15:35:11



SCF and COS are used in this example where a .zip file you uploaded to COS needs to be decompressed and the zip package name needs to be returned to COS as the folder name. You can extend the sample code to do more things such as decompressing files in other formats.

As the temporary storage space allocated by SCF for each execution is currently 512 MB, it is recommended to keep the size of a single zip package below 300 MB and size of a single extracted 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 extracted 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. On the Create a Function page, Configure the following information:

    • Function Name: Named "unzip_to_cos".
    • Creation Method: Select "Template function".
    • Template Search: Enter the search keyword "unzip" and select the "unzip_to_cos" template (this template currently only supports the zip format. If you need to process other formats such as rar or 7z, you need to expand the code by yourself). You can move the cursor over the template function and click Learn More to view the template function details. The function code can be downloaded.
  4. Go to the function configuration page, keep the default configuration, and click Finish To complete the function creation.

  5. Click function Code. In the function Code Editor, you need to modify the following parameters according to the comments. Click Save after the modification is completed.

    • Appid: You can check it in Account information .
    • Secret-id and secret-key. You can get this from API Key Management .
    • The region to which region: destination bucket belongs. This is ap-beijing.
    • Bucket_upload: unzip-125xxxxxxx should be entered here.
    • The decompression password of the password: package is left blank if the decompression key is not set.
  6. Click Function Configuration And change the timeout period of the function to 100 seconds. 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 and configure the following items. After configuring the following information, Click Save.
    • Trigger Method: Select "COS trigger".
    • COS Bucket: Select "zip-upload".
    • Event Type: Select "All creation events" and 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 extracted files.
  5. Go to the SCF Console and view the execution result. Select Function Service > Function > Execution Log to see the printed log information.