Building a Program

Download and use the image compressing program package provided in this example.

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 obs_output_bucket is the address for storing compressed images and must be configured when you create a function.
def handler (event, context):
    srcBucket, srcObjName = getObjInfoFromObsEvent(event)
    obs_address = context.getUserData('obs_address')
    outputBucket = context.getUserData('obs_output_bucket') 
    if obs_address is None:
        obs_address = '100.125.15.200'
    if outputBucket is None:
        outputBucket = 'casebucket-out'

    print "*** srcBucketName: " + srcBucket
    print "*** srcObjName:" + srcObjName
    print "*** obs_address: " + obs_address
    print "*** output bucket: " + outputBucket

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

    outFile, outputFileName = image_thumbnail(srcObjName)

    # Upload converted files to a new OBS bucket.
    uploadFileToObs(client, outputBucket, outFile, outputFileName)
    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. Set the function information.

    1. Set the basic information, as shown in Figure 2.

      For Function Name, enter fss_examples_image_thumbnail.

      For App, select default.

      For Description, enter Image compressing.

      For Agency, select serverless_trust created in Creating an Agency.

      Figure 2 Basic information
    2. Set the code information, as shown in Figure 3.

      For Runtime, select Python 2.7.

      For Handler, enter index.handler.

      For Code Entry Mode, select Upload ZIP file, and upload the sample code package fss_examples_image_thumbnail.zip.

      Figure 3 Code information
    3. Click Create Function.

  4. On the fss_examples_image_thumbnail page, select the Configuration tab and set the environment information, as shown in Figure 4.

    For Memory, select 256.

    For Timeout, enter 40.

    For Environment Variables, define parameter obs_output_bucket in the index.py file as the key and bucket your-bucket-output created in Creating OBS Buckets as the value.

    The key obs_address indicates the address of the OBS bucket defined in the index.py file to store processed images. The value obs.cn-north-1.myhuaweicloud.com indicates the region where the bucket has been created.

    Figure 4 Defining environment variables

    The value of obs_address should be in the format "obs.{region}.myhuaweicloud.com". For the region name, see Regions and Endpoints.

  5. Click Save in the upper right corner.

Selecting a Dependency

The sample code depends on OBS and Pillow packages, which need to be imported as dependencies. The procedure is as follows:

  1. On the fss_examples_image_thumbnail details page, click the Code tab and then click Select in the same row as Dependencies. The Select Dependency dialog box is displayed, as shown in the following figure.

  2. Select the required dependencies and click OK.
  3. Click Save in the upper right corner.