This tutorial shows how to deploy a function by using Serverless Devs or the Function Compute console. Uploading a ZIP file that matches specified rules to Object Storage Service (OSS) triggers the function to automatically decompress the file.
Usage notes
Use UTF-8 or GB 2312 encoding for your file and folder names. Otherwise, the names of the decompressed files or folders may become garbled, or the decompression process may be interrupted.
When you upload a compressed file to a bucket, do not select the archive storage or cold archive storage type. Otherwise, the function cannot be triggered.
Use a standard ZIP command-line tool to create compressed files to prevent decompression failures.
Each file within the compressed archive must not exceed 1 GB. Otherwise, decompression may fail.
The time limit for decompressing a single archive is 2 hours. A task that exceeds this limit will fail.
The default execution timeout for the function is 2 hours. You can adjust this timeout to meet your business requirements.
How it works
Automate decompression with the Function Compute console
Prerequisites
Procedure
Log on to the Function Compute console. In the left-side navigation pane, click Applications.
On the Applications page, click Create Application.
On the Create Application page, select a method to create the application.
This tutorial demonstrates the Use a Template to Create an Application option.
In the templates section, select the Hot Templates tab. Find the start-unzip-oss card, hover over it, and then click Create Now.
On the Create Application page, configure the following parameters and then click Create and Deploy Default Environment.
The following table describes the key parameters. You can use the default values for other parameters.
Parameter
Description
Basic configurations
Deployment Type
Select Directly Deploy.
Role Name
The default value is AliyunFCServerlessDevsRole. If the current permissions are insufficient, click Add Policy, select a policy in the dialog box, and then click Add.
Advanced settings
Region
Select the region where you want to deploy the application.
Service name
Specify the service name for the application.
Function name
Specify the function name for the application.
RAM role ARN
Select the role that OSS uses to trigger the function. Make sure that the role has the AliyunOSSFullAccess and AliyunFCDefaultRolePolicy permission policies attached.
OSS bucket name
Select an existing bucket.
Prefix
Set the file prefix. The default is src. Different triggers in the same bucket cannot have the same file prefix.
Decompress destination directory
Specify the target directory for the decompressed file. The default is dst.
NoteTo prevent a trigger loop, ensure this directory differs from the Prefix.
Keep file name as directory
Determines whether to create a subdirectory named after the compressed file. Valid values:
true: Retains the name of the compressed file as a directory in the destination path. For example, if the decompress destination directory is target and the compressed file name is source.zip, the file path after decompression is target/source/....
false: Extracts the files directly to the target directory. In the preceding example, the file path after extraction is target/....
Trigger RAM role ARN
The default value is AliyunOSSEventNotificationRole. OSS uses this role to send event notifications to invoke the function.
Test the application.
Uploading a .zip file to a path that starts with the specified Prefix triggers the application's function, which automatically decompresses the file and saves the results to the specified Decompress destination directory.
For example, you set the prefix to source and the decompress destination directory to target. When you upload a .zip file to source or source1, the function is triggered, and the file is automatically decompressed and saved to target.
(Recommended) Automate decompression with Serverless Devs
Prerequisites
Activate Function Compute. For more information, see Quickly create a function.
Activate OSS and create a bucket. For more information, see Get started with the OSS console.
Install and configure Serverless Devs. For more information, see Install Serverless Devs and Configure Serverless Devs.
Procedure
Run the following command to clone the sample project:
git clone git@github.com:awesome-fc/decompress-oss.gitRun the following command to navigate to the project directory:
cd decompress-ossEdit the configurations in the s.yaml file and save the file.
In the file, change the value of the
bucketNameparameter fromyour-bucketto the name of the bucket that you created.Optional: Modify the parameters for the OSS trigger prefix
Prefix, the suffixSuffix, and the directory for storing decompressed filesPROCESSED_DIRas needed.
NoteIn this example, the OSS trigger prefix defaults to
src/, but you can also set it tosrc. The destination directory for decompressed files defaults todst/.Run the following command to deploy the project:
sudo s fc-decompress-oss-invoke-fc-EnhancedInstance deploy -ySample output:
Checking Service, Function, Triggers (18.63s) Creating Service, Function, Triggers (13.44s) There is auto config in the service: enhancedInstance-decompress Tips for next step ====================== * Display information of the deployed resource: s info * Display metrics: s metrics * Display logs: s logs * Invoke remote function: s invoke * Remove Service: s remove service * Remove Function: s remove function * Remove Trigger: s remove trigger * Remove CustomDomain: s remove domain fc-decompress-oss-invoke-fc-EnhancedInstance: region: cn-hangzhou service: name: enhancedInstance-decompress function: name: oss-invoke-fc runtime: python3 handler: index.handler memorySize: 4096 timeout: 7200 triggers: - type: oss name: zip-t - type: oss name: gz-t - type: oss name: tar-tUpload a compressed file, such as a .zip, .tar, or .gz file, to the specified directory in the bucket to trigger the decompression function.
In this example, the default directory is src/, and the extracted files are automatically generated in dst/.
References
If you only need to decompress ZIP files uploaded to OSS, you can configure automatic ZIP file decompression directly in the OSS console. For more information, see Upload a ZIP package and automatically decompress it.
If you need more than 10 triggers for a single bucket, you can create an EventBridge-based OSS trigger. For more information, see Configure an EventBridge-based OSS trigger.
If you need to use Function Compute to package and download objects from OSS as a ZIP file, see Download multiple files as a ZIP package.