Updated on 2024-03-26 GMT+08:00

Building a Program

Download fss_examples_image_thumbnail.zip (SHA-256 verification package) to create an image compressing function from scratch.

Creating a Deployment Package

This example uses a Python function to compress images. For details about function development, see Developing Functions in Python. Figure 1 shows the sample code directory. The service code is not described.

Figure 1 Sample code directory
Under the directory, index.py is a handler file. The following code is a snippet of the handler file. Parameter output_bucket is the address for storing compressed images and must be configured when you create a function.
def handler(event, context):
    ak = context.getAccessKey()
    sk = context.getSecretKey()
    if ak == "" or sk == "":
        context.getLogger().error('Failed to access OBS because no temporary '
                                  'AK, SK, or token has been obtained. Please '
                                  'set an agency.')
        return 'Failed to access OBS because no temporary AK, SK, or token ' \
               'has been obtained. Please set an agency. '
 
    obs_endpoint = context.getUserData('obs_endpoint')
    if not obs_endpoint:
        return 'obs_endpoint is not configured'
 
    output_bucket = context.getUserData('output_bucket')
    if not output_bucket:
        return 'output_bucket is not configured'
 
    compress_handler = ThumbnailHandler(context)
    records = event.get("Records", None)
    return compress_handler.run(records[0])

Creating a Function

When creating a function, specify an agency with OBS access permissions so that FunctionGraph can invoke the OBS service.

  1. Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
  2. Click Create Function.
  3. Set the function information.

    After setting the basic information, click Create Function.
    • For Function Name, enter fss_examples_image_thumbnail.
    • For Agency, select serverless_trust created in Creating an Agency.
    • For Runtime, select Python3.6

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

    1. On the Code tab, choose Upload > Local ZIP, upload the sample code fss_examples_image_thumbnail.zip, and click OK.
    2. Choose Configuration > Basic Settings, set the following parameters, and click Save.
      • For Memory, select 256.
      • For Execution Timeout, enter 40.
      • For Handler, retain the default value index.handler.
      • For App, retain the default value default.
      • For Description, enter Image compressing.
    3. Choose Configuration > Environment Variables, set environment variables, and click Save.

      output_bucket: the output bucket parameter defined in index.py. Set the value to your-bucket-output, the bucket created in Creating OBS Buckets.

      obs_endpoint: the bucket address parameter defined in index.py. Set the value to obs.region.myhuaweicloud.com.

      Table 1 Environment variable description

      Environment Variable

      Description

      obs_endpoint

      OBS endpoint. To obtain the OBS endpoint, see Regions and Endpoints.

      output_bucket

      OBS bucket for storing output images.

Selecting a Dependency

The sample code depends on the Pillow package, which should be imported as dependencies. The procedure is as follows:

  1. Go to the fss_examples_image_thumbnail details page, click the Code tab, and click Add in the Dependencies area at the bottom.
  2. Add public dependency pillow-7.1.2.

    Figure 2 Adding dependencies

You do not need to configure the reference after adding a dependency, as it is preconfigured in the function code of the demo package.