All Products
Search
Document Center

Function Compute (2.0):Use Function Compute to automatically decompress files in OSS

Last Updated:Feb 02, 2024

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

image

Use the Function Compute console

Prerequisites

Procedure

  1. Log on to the Function Compute console. In the left-side navigation pane, click Applications.

  2. On the Applications page, click Create Application.

  3. On the Create Application page, select a method for creating the application.

    In this topic, Use a Template to Create an Application is selected.

  4. 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.

  5. 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.

    Note

    To 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.

  6. 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

Procedure

  1. Run the following command to download a demo project:

    git clone git@github.com:awesome-fc/decompress-oss.git
  2. Run the following command to go to the project directory:

    cd decompress-oss
  3. Edit the configuration information in the s.yaml file and save the file.

    • Change the value of the bucketName parameter from your-bucket to the name of the bucket that you created.

    • (Optional) Modify the values of the Prefix, Suffix, and PROCESSED_DIR parameters for an OSS trigger.

    Note

    In this example, src/ is the prefix of the OSS trigger. You can set the Prefix parameter to src. By default, the decompressed file is stored in dst/.

  4. 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
  5. 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