Building a Program
Download and use the image watermarking program package provided in this example.
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.
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)
# Uploads 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.
- Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
- Click Create Function.
- Set the function information.
- Set the basic information, as shown in Figure 2.
For Function Name, enter fss_examples_image_watermark.
For App, select default.
For Description, enter Image watermarking.
For Agency, select serverless_trust created in Creating an Agency.
- 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_watermark.zip.
- Click Create Function.
- Set the basic information, as shown in Figure 2.
- On the details page of the fss_examples_image_watermark function, click the Code tab, and add the pillow-6.2.2 dependency.

- On the fss_examples_image_watermark page, select the Configuration tab and set the environment information, as shown in Figure 4.
For Memory, select 128.
For Timeout, enter 3.
For Environment Variables, define parameter obs_output_bucket in the index.py file as the key and bucket hugb-bucket-output created in Creating OBS Buckets as the value. obs_region indicates the region where the OBS bucket obs_output_bucket is located. The following figure is for reference only. Replace the following value with the actual value.
- Click Save.
Last Article: Preparation
Next Article: Adding an Event Source




Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.