Using SCF to Decompress Zip Packages
Last updated: 2020-02-26 15:35:11PDF
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: 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.
Go to the function configuration page, keep the default configuration, and click Finish To complete the function creation.
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.
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
- 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.