Updated on 2024-08-21 GMT+08:00

Building a Program

Download watermark.zip to create an image watermarking function from scratch.

Creating a Deployment Package

This example uses a Python function to watermark 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 obs_output_bucket is the address for storing watermarked images and must be configured when you create a function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def handler(event, context):
    srcBucket, srcObjName = getObjInfoFromObsEvent(event)
    outputBucket = context.getUserData('obs_output_bucket')

    client = newObsClient(context)
    # download file uploaded by user from obs
    localFile = "/tmp/" + srcObjName
    downloadFile(client, srcBucket, srcObjName, localFile)

    outFileName, outFile = watermark_image(localFile, srcObjName)
    # Upload converted files to a new OBS bucket.
    uploadFileToObs(client, outputBucket, outFileName, outFile)

    return 'OK'

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. Click Create from scratch and configure the function information.

    After setting the basic information, click Create.
    • Function Type: Select Event Function.
    • For Function Name, enter fss_examples_image_watermark.
    • For Agency, select serverless_trust created in Creating an Agency.
    • For Runtime, select Python 3.6.

  4. Go to the fss_examples_image_watermark details page, click the Code tab, click Add in the Dependencies area at the bottom, and add the public dependency pillow-7.1.2.

    Figure 2 Adding a dependency

  5. On the fss_examples_image_watermark details page, configure the following information:

    1. On the Code tab, choose Upload > Local ZIP, upload the sample code watermark.zip.
    2. Choose Configuration > Basic Settings, set the following parameters, and click Save.
      • For Memory, select 128.
      • For Execution Timeout, enter 3.
      • For Handler, retain the default value index.handler.
      • For App, retain the default value default.
      • For Description, enter Image watermarking.
    3. Choose Configuration > Environment Variables, set environment variables, and click Save. The following figure is for reference only. Replace the following values with the actual values.

      obs_output_bucket: the output bucket parameter defined in index.py. Set the value to hugb-bucket-output, the bucket created in Creating OBS Buckets for storing watermarked images.

      obs_region: region where the OBS bucket obs_output_bucket resides, for example, eu-west-101.
      Figure 3 Adding environment variables
      Table 1 Environment variable description

      Environment Variable

      Description

      obs_region

      Region to which the OBS bucket belongs. The value must be the same as the region to which the function belongs.

      obs_output_bucket

      OBS bucket for storing watermarked images.