Building a Program
Download fss_examples_image_thumbnail.zip (SHA-256 verification package) to create an image compressing function from scratch.
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): ak = context.getAccessKey() sk = context.getSecretKey() if ak == "" or sk == "": context.getLogger().error('Failed to access OBS because no temporary ' 'AK, SK, or token has been obtained. Please ' 'set an agency.') return 'Failed to access OBS because no temporary AK, SK, or token ' \ 'has been obtained. Please set an agency. ' obs_endpoint = context.getUserData('obs_endpoint') if not obs_endpoint: return 'obs_endpoint is not configured' output_bucket = context.getUserData('output_bucket') if not output_bucket: return 'output_bucket is not configured' compress_handler = ThumbnailHandler(context) records = event.get("Records", None) return compress_handler.run(records[0])
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 Function.
- Function Type: Select Event Function.
- For Function Name, enter fss_examples_image_thumbnail.
- For Agency, select serverless_trust created in Creating an Agency.
- For Runtime, select Python3.6
- On the fss_examples_image_thumbnail details page, configure the following information:
- On the Code tab, choose Upload > Local ZIP, upload the sample code fss_examples_image_thumbnail.zip.
- Choose Configuration > Basic Settings, set the following parameters, and click Save.
- For Memory, select 256.
- For Execution Timeout, enter 40.
- For Handler, retain the default value index.handler.
- For App, retain the default value default.
- For Description, enter Image compressing.
- Choose Configuration > Environment Variables, set environment variables, and click Save.
output_bucket: the output bucket parameter defined in index.py. Set the value to your-bucket-output, the bucket created in Creating OBS Buckets.
obs_endpoint: the bucket address parameter defined in index.py. Set the value to obs.region.myhuaweicloud.com.
Table 1 Environment variable description Environment Variable
Description
obs_endpoint
OBS endpoint. To obtain the OBS endpoint, see Regions and Endpoints.
output_bucket
OBS bucket for storing output images.
Selecting a Dependency
The sample code depends on the Pillow package, which should be imported as dependencies. The procedure is as follows:
- Go to the fss_examples_image_thumbnail details page, click the Code tab, and click Add in the Dependencies area at the bottom.
- Add public dependency pillow-7.1.2.
Figure 2 Adding dependencies
You do not need to configure the reference after adding a dependency, as it is preconfigured in the function code of the demo package.
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