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
- 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 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
- Log in to the SCF Console, and enter the Function Service page.
- Select Beijing region and click Create to enter the function creating page.
- 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.
- Click Next to enter the function configuration page. Keep the default configuration, and click Completed to complete the function creation.
- 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
- bucket_upload: enter
- password: the password for decompressing the zipped file. Leave it empty if you don’t want one.
- 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
- Create the function as instructed in the above steps.
- 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
- 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 decompressed files.
- Go to the SCF Console, and view the execution result. Select Function Service>Functions>Execution Log to check the printed log.