Help & DocumentationCloud Object StorageBest PracticeUsing SCF to Decompress Zip Packages

Using SCF to Decompress Zip Packages

Last updated: 2019-10-25 16:42:41

PDF

Scenario

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.

Directions

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: "unzip_to_cos".
    • Creation Method: Select "Template function". After completing the configuration, click Save.
    • 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. Configure information and click Next. Go to the function configuration page, keep the default configuration, and click Completed to complete the function creation.
  5. Click Function Code. In this function, you need to modify the following parameters according to the comments in the function code editor. After the modification is completed, click Save.
    • appid: Available in Account Information.
    • secret_id、secret_key: Available in API Key Management.
    • region: The region of the target bucket, here is ap-beijing.
    • bucket_upload: which should be "unzip bucket" here.
    • password: which is the password of the zip package and optional.
  6. Click Function Configuration and change the timeout period of the function to 100 seconds. Finally 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 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.