Using SCF to Decompress Zip Packages
Last updated: 2019-10-25 16:42:41PDF
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
- Log in to the COS Console.
- 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.
- 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.
Creating an SCF Function
- Log in to the SCF console and enter the Function Service page.
- Select Beijing region and click Create to enter the function creating page.
- 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.
- Configure information and click Next. Go to the function configuration page, keep the default configuration, and click Completed to complete the function creation.
- 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.
- 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
- Create the function as instructed in the above steps.
- 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
- Download the test sample in .zip format.
- Go to the COS Console, select the created bucket "zip-upload", and click Upload File.
- In the Upload File window that pops up, select the test sample downloaded in step 1 and click Upload.
- Enter the other bucket "unzip" and you can view the extracted files.
- Go to the SCF Console and view the execution result. Select Function Service > Function > Execution Log to see the printed log information.