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.
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.
- 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_thumbnail.
For App, select default.
For Description, enter Image compressing.
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_thumbnail.zip.
- Click Create Function.
- Set the basic information, as shown in Figure 2.
- 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.
The value of obs_address should be in the format "obs.{region}.myhuaweicloud.com". For the region name, see Regions and Endpoints.
- 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:
- 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.

- Select the required dependencies and click OK.
- Click Save in the upper right corner.
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.