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.
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.
- Log in to the FunctionGraph console, and choose Functions > Function List in the navigation pane.
- Click Create Function.
- Click Create from scratch and configure the function information.
After setting the basic information, click Create.
- For Function Type, select Event Function.
- For Function Name: Enter a function name, for example, fss_examples_image_watermark.
- For Agency, select serverless_trust created in Creating an Agency.
- For Runtime, select Python 3.6.
- 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
- On the fss_examples_image_watermark details page, configure the following information:
- On the Code tab, choose Upload > Local ZIP, upload the sample code watermark.zip.
- 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.
- 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.
Table 1 Environment variables Key
Value
Description
obs_output_bucket
bucket-output-fg
Key: OBS bucket parameter for storing output watermark images, which is defined in the index.py file.
Value: OBS bucket created in Creating OBS Buckets for storing output watermark images.
obs_region
For example, cn-north-4.
Key: region of the OBS bucket where the output watermark image is stored, which is defined in the index.py file. It must be the same as the region where the function is located.
Value: region where the OBS bucket obs_output_bucket is located. For details, see Regions and Endpoints.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot