Help Center/ FunctionGraph/ Best Practices/ Data Processing Practices/ Using FunctionGraph to Compress Images in OBS
Updated on 2025-07-10 GMT+08:00

Using FunctionGraph to Compress Images in OBS

Introduction

This practice is applicable to compressing a single image or a batch of images. High-quality images consume significant storage/bandwidth, slowing website/app loading. Using OBS and FunctionGraph, you can build an image compression solution to automatically process images in buckets, optimizing storage and resource efficiency.

Constraints

  • OBS Application Service trigger is available only in CN North-Beijing4, CN North-Ulanqab1, and CN East-Shanghai1. When creating a function or an OBS bucket, select one of the preceding regions.
  • Ensure that the created function and OBS bucket are in the same region.

Resource and Cost Planning

Table 1 lists the resources and costs required for using FunctionGraph to compress images in OBS.

Table 1 Resource and cost planning

Resource

Description

Billing

OBS

  • Product type: object storage
  • Region: CN North-Beijing4
  • Storage policy: Single-AZ storage
  • Storage class: Standard
  • Bucket policy: Private
  • Quantity: 2

FunctionGraph

  • Function type: Event function
  • Region: CN North-Beijing4
  • Quantity: 1
  • Billing mode: Pay-per-use
  • The first 1 million invocations are free of charge in a month. For details about the billing items, see Pay-per-Use Billing.

Procedure

The following table describes how to use FunctionGraph to compress images in OBS.

Table 2 Procedure

Step

Description

Step 1: Creating Two OBS Buckets

Create two OBS buckets. The source bucket is used to store the original image, and the target bucket is used to store the compressed image.

Step 2: Creating a Cloud Service Agency

Create a cloud service agency to authorize FunctionGraph to access other cloud services so that FunctionGraph can work with OBS.

Step 3: Creating an Image Compression Function

Create a function from scratch, configure the code environment, and create an OBS Application Service trigger to automatically compress images uploaded or updated in the OBS source bucket.

Step 4: Verifying Image Compression

Verify that the image is compressed in the target bucket.

Step 1: Creating Two OBS Buckets

  1. Log in to the OBS console, choose Object Storage.
  2. Click Create Bucket.
  3. On the displayed Create Bucket page, set the OBS source bucket parameters by referring to Table 3.

    Table 3 OBS source bucket configuration

    Parameter

    Requirements

    Example Value

    Region

    Mandatory

    Region where the bucket is located. Select a region close to your service to reduce network latency and improve access speed. After a bucket is created, its region cannot be changed. Currently, the OBS Application Service trigger supports only CN North-Beijing4, CN North-Ulanqab1, and CN East-Shanghai1.

    CN North-Beijing4

    Bucket Name

    Mandatory.

    Bucket name, which must be globally unique. After a bucket is created, its name cannot be changed.

    your-bucket-input

    Data Redundancy Policy

    Mandatory.

    • Multi-AZ storage: Data is stored in multiple AZs to achieve higher reliability.
    • Single-AZ storage: Data is stored in a single AZ, which costs less.
    • After a bucket is created, its data redundancy policy cannot be changed.

    Single-AZ storage

    Storage Class

    Mandatory.

    • Standard: For storing a large number of hot files or small files that are frequently accessed (multiple times per month on average) and require fast access. Both single-AZ and multi-AZ storage are supported.
    • Infrequent Access: For storing data that is less frequently accessed (less than 12 times per year on average), but when needed, the access has to be fast. Both single-AZ and multi-AZ storage are supported.
    • Archive: For archiving data that is rarely accessed (once a year on average) and does not require fast access. Only single-AZ storage is supported.

    Standard

    Bucket Policy

    Mandatory.

    Controls read and write permissions for the bucket.

    • Private: Only users granted permissions by the bucket ACL can access the bucket.
    • Public Read: Anyone can read objects in the bucket.
    • Public Read/Write: Anyone can read, write, or delete objects in the bucket.

    Private

    Enterprise Project

    Mandatory.

    Enterprise projects let you manage cloud resources by projects. The default project is default.

    default

    Properties

    Optional.

    For details, see Creating a Bucket.

    • Direct Reading: allows you to download data from the Archive storage class without restoring them in advance. Direct reading is a billable function.
    • Server-Side Encryption: the OBS server encrypts the objects uploaded from the client before storing them. If this feature is enabled, you must specify an encryption key.
    • WORM: When enabled, you can configure a retention policy for the current bucket. The object version which the retention policy is applied to cannot be deleted within a specified period.
    • Tags: Tags are used to identify and classify OBS buckets.
    • Direct Reading: disabled
    • Server-Side Encryption: disabled
    • WORM: disabled
    • Tags: -

  4. Repeat 3 to create the target bucket. Set the Bucket Name to your-bucket-output and keep other parameters the same as those of the source bucket.
  5. View your-bucket-input and your-bucket-output in the bucket list.

Step 2: Creating a Cloud Service Agency

  1. Log in to IAM console. In the navigation pane, choose Agencies. On the displayed page, click Create Agency in the upper right corner.
  2. Set the following parameters:

    • Agency Name: Enter serverless_trust.
    • Agency Type: Select Cloud service.
    • Cloud Service: Select FunctionGraph.
    • Validity Period: Select Unlimited.
    • Description: Retain the default value.

  3. Click OK. The system displays a message indicating that the creation is successful. Click Authorize.
  4. On the Select Policy/Role page, search for and select the OBS Administrator policy, and click Next.

    Figure 1 Selecting a policy

  5. Set the minimum authorization scope. Select All resources for Scope and click OK.

    The OBS Administrator policy does not support specifying specific region or project resources.

  6. The system displays a message indicating that the authorization is successful. Click Finish to return to the agency list. If serverless_trust is displayed in the list, the agency is created successfully.

Step 3: Creating an Image Compression Function

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. Select Create from scratch, set the function information by referring to Table 4, and click Create Function.

    Table 4 Configuring function parameters

    Parameter

    Requirements

    Example Value

    Function Type

    Mandatory.

    • Event Function: triggered by triggers.
    • HTTP Function: triggered once HTTP requests are sent to specific URLs.

    Event Function

    Region

    Mandatory.

    Region where the code is deployed. The region must be the same as that of the OBS bucket.

    CN North-Beijing4

    Function Name

    Mandatory.

    Function name, which contains letters, digits, underscores (_), and hyphens (-). It must start with a letter and end with a letter or digit. The length cannot exceed 60 characters.

    fss_examples_image_thumbnail

    Enterprise Project

    Mandatory.

    Enterprise project to which the function is added. The enterprise project must be the same as that of the OBS bucket. The default enterprise project is default.

    default

    Agency

    This parameter is optional but mandatory in this practice.

    Name of the agency used by FunctionGraph to access other cloud services. Select the agency created in Step 2: Creating a Cloud Service Agency.

    serverless_trust

    Runtime

    Mandatory.

    Development language and language version of the function. CloudIDE supports Node.js, Python, and PHP only.

    Python3.6

  4. On the fss_examples_image_thumbnail details page, configure the following information:

    1. Download the sample code fss_examples_image_thumbnail_eg.zip.
    2. On the Code tab page, select Upload > Local ZIP, add the downloaded fss_examples_image_thumbnail_eg.zip file, and click OK. The code is automatically deployed.
    3. Click Add at the bottom of the page, add the public dependency package pillow-7.1.2, retain the default version 1, and click OK.
    4. On the Configuration > Basic Settings tab page, modify the following configuration:
      • For Execution Timeout, enter 40.
      • For Memory, select 256.

      Click Save.

    5. Choose Configuration > Environment Variables, click Edit Environment Variable. In the dialog box that is displayed, click Add, add information in Table 5, and click OK.
      Table 5 Environment variables

      Key

      Value

      Description

      output_bucket

      your-bucket-output

      Name of the OBS bucket where compressed images are stored.

      obs_endpoint

      obs.cn-north-4.myhuaweicloud.com

      OBS endpoint in CN North-Beijing4 (For other regions, see Regions and Endpoints.)

    6. On the Configuration > Triggers tab page, click Create Trigger. In the displayed dialog box, configure basic information by referring to Table 6 and click OK.
      Table 6 Configuring trigger parameters

      Parameter

      Requirements

      Example Value

      Trigger Type

      Mandatory.

      Add an OBS Application Service trigger to trigger the function when an operation is performed on an OBS bucket.

      OBS Application Service

      Trigger Name

      Mandatory.

      Name of the trigger to be created. Only letters, digits, underscores (_), and hyphens (-) are allowed. The value cannot start with a digit or hyphen (-). Maximum length: 128 characters.

      Image

      Bucket Name

      Mandatory.

      Select the created OBS bucket to store the original images.

      your-bucket-input

      Event Type

      Mandatory.

      Triggering event type. In this practice, the function is triggered by uploading or updating bucket objects.

      Create or override bucket objects via UI or Put request Create or override bucket objects via Post request

      Object Name Prefix

      Optional.

      Enter a keyword for limiting notifications to those about objects whose names start with the matching characters. This limit can be used to filter the names of OBS objects.

      Leave this parameter blank.

      Object Name Suffix

      Optional.

      Enter a keyword for limiting notifications to those about objects whose names end with the matching characters. This limit can be used to filter the names of OBS objects.

      Leave this parameter blank.

      Object Name Encoding

      Mandatory.

      Specifies whether to encode the object name.

      Enabled by default.

Step 4: Verifying Image Compression

  1. Log in to the OBS console, click the your-bucket-input bucket. The Objects tab page is displayed.
  2. Click Upload Object, set Storage Class to Standard, and upload an image to be compressed. After the upload is successful, the page shown in Figure 2 is displayed.

    Figure 2 Uploading an image

  3. Go to the Objects page of the your-bucket-output bucket and view the size of the compressed image.

    Figure 3 Viewing compressed images

    To avoid unnecessary storage fees, you can delete the images stored in the two OBS buckets as required after the practice. Deleted data cannot be restored. Exercise caution when performing this operation.