You can deploy a function by using Serverless Devs and the Function Compute console and use the function to automatically decompress ZIP files that meet decompression rules after the files are uploaded to Object Storage Service (OSS).
Usage notes
We recommend that you encode the names of files or folders in UTF-8 or GB 2312. Otherwise, the names of the decompressed files or folders may be garbled, or the decompression may be interrupted.
When you upload a compressed file to an OSS bucket, do not select archive or cold archive storage. Otherwise, functions cannot be executed as expected.
We recommend that you use the standard zip command-line utility to compress files to prevent decompression failures.
The size of a compressed file that you want to upload to an OSS bucket must be less than or equal to 1 GB. Otherwise, the file may fail to be decompressed.
The time to decompress a ZIP file cannot be longer than two hours. If a ZIP file fails to be decompressed in two hours, the decompression task is stopped.
By default, the timeout period of a function to decompress OSS objects is two hours. You can adjust the execution timeout period based on your business requirements.
Procedure
Use the Function Compute console
Prerequisites
Function Compute is activated. For more information, see Step 1 in Quickly create a function.
OSS is activated and a bucket is created. For more information, see Get started by using the OSS console.
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 for creating the application.
In this topic, Use a Template to Create an Application is selected.
Click the Hot Templates tab. On the tab, move the pointer to the card that is used to create an application to automatically decompress uploaded files, such as start-unzip-oss, and click Create Now.
On the Create Application page, configure the parameters and click Create and Deploy Default Environment.
The following table describes the parameters that require manual setting. Retain the default values for other parameters.
Parameter
Description
Basic Configurations
Deployment Type
Select Directly Deploy.
Role Name
AliyunFCServerlessDevsRole is selected by default. If the existing policies do not meet your requirements, click Add Policy. In the dialog box that appears, select the policy that you want to add and click Add.
Advanced Settings
Region
Select the region to which you want to deploy the application.
Service name
Set the name of the service to which the application to be created belongs.
Function name
Set the name of the function to which the application to be created belongs.
RAM's ARN
Select the role that is used to trigger the function. Make sure that the AliyunOSSFullAccess and AliyunFCDefaultRolePolicy policies are attached to the role.
OSS bucket name
Select an existing bucket.
Prefix
Specify the prefix contained in the names of the uploaded files to be decompressed. The default value is src. Do not set the same prefix for two triggers in the same bucket.
Decompress destination directory
Set the directory to which decompressed files are saved. The default value is dst.
NoteTo prevent loop triggering, set this directory to a value different from the Prefix value.
Whether to use the compressed file name as the path directory
Specify whether to use the name of the compressed file as the name of the directory to which the decompressed file is saved. Take note of the following items:
true: uses the name of the compressed file as the directory name. For example, the Decompress destination directory is set to target and the compressed file name is source.zip, then the path of the decompressed file is target/source/....
false: does not use the name of the compressed file as the directory name. In the preceding example, the decompressed file path is target/....
Trigger RAM role ARN
The default value is AliyunOSSEventNotificationRole. OSS uses this role to send event notifications to invoke functions.
Test the application.
Upload a ZIP file to a directory that matches the specified Prefix. The associated function is triggered to decompress the file to the directory specified by the Decompress destination directory parameter.
For example, if you set Prefix to source, set Decompression Destination Directory to target, when you upload a ZIP file to source or source1, the function is triggered to decompress the file to target.
(Recommended) Use Serverless Devs
Prerequisites
Function Compute is activated. For more information, see Step 1 in Quickly create a function.
OSS is activated and a bucket is created. For more information, see Get started by using the OSS console.
Serverless Devs is installed and configured. For more information, see Install Serverless Devs and Configure Serverless Devs.
Procedure
Run the following command to download a demo project:
git clone git@github.com:awesome-fc/decompress-oss.git
Run the following command to go to the project directory:
cd decompress-oss
Edit the configuration information in the s.yaml file and save the file.
Change the value of the
bucketName
parameter fromyour-bucket
to the name of the bucket that you created.(Optional) Modify the values of the
Prefix
,Suffix
, andPROCESSED_DIR
parameters for an OSS trigger.
NoteIn this example,
src/
is the prefix of the OSS trigger. You can set the Prefix parameter tosrc
. By default, the decompressed file is stored indst/
.Run the following command to deploy the project:
sudo s fc-decompress-oss-invoke-fc-EnhancedInstance deploy -y
Sample 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 trigge * 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-t
Upload compressed files with an extension of .zip, .tar, or .gz to the specified directory in the bucket and trigger the function to decompress the files.
In this example, the default directory is src/. The decompressed files are automatically generated in the specified directory dst/.
References
If you need to decompress only ZIP files that are uploaded to OSS, you can configure automatic decompression for ZIP files in the OSS console. For more information, see Use Function Compute to automatically decompress files.
If you want to associate more than 10 triggers with a bucket, you can create an EventBridge-based OSS trigger. For more information, see Configure an EventBridge-based OSS trigger.
If you want to use Function Compute to download objects as a package, see Use Function Compute to download multiple objects as a package.