Example 1: Using a Timer Trigger to Periodically Download Files from an OBS Bucket
Scenario
This example guides you through the procedure for creating a Python 2.7 function and associating a timer trigger with it to periodically download files from an OBS bucket.
For details about how to call APIs, see Calling APIs.
Prerequisites
- You have uploaded files to OBS and have recorded the file names, the bucket in which the files are stored, and the address of the bucket.
- You have created an agency in IAM to allow FunctionGraph to access OBS and have recorded the name of the agency.
General Procedure
Create a FunctionGraph function and associate a timer trigger with it to periodically download files from an OBS bucket. The procedure is as follows:
- Creating a Function: Create a function for downloading files.
- Modifying the Metadata of a Function: Modify the OBS address, OBS bucket name, and file name in the function configuration.
- Executing a Function Synchronously: Verify whether the function can successfully download files from the OBS bucket.
- Creating a Trigger: Create a timer trigger to periodically download files.
Step 1: Create a Function for Downloading Files from OBS
URI: POST /v2/{project_id}/fgs/functions
For details, see Creating a Function.
- Sample request
POST https://{Endpoint}/v2/{project_id}/fgs/functions { "code_filename": "index.zip", "code_type": "inline", "func_code": { "file": "UEsDBAoAAAAIABESwlDHSM8cOQYAAJYRAAAIAAAAaW5kZXgucHm9V91v2zYQf9dfcXAeLKeKkrYZNgTQQ9JvpG2CJsOwJ4GWaJuLRGok5VT963dHUpZsJ82GofVDQh2Pd8e7333wAI4Oj6BQpZDLM2jt4ug3okQH8MeKS9CtlLgDdiUMGFY3FSdmDlYBKwpuDFxd3CTQqRbq1lgwDS/EogMmgS25LDq4F3YFy0rNWQWG67UoeH+04boWxgglDcRKA7NQcYZS3BkU/ADjLI0WWtVoRZ2quUmLSnBpaZn7JYi6UdrC1dy8coRt/hqtr0y65DZX8794YfMVZyXX/bF33F45+ntHfuqw5n+33Ni90188PQp005l+qUwUFa3WaFm+EBXPG4aXzZCe0iothZas5nH/rTmraBHnnj2fzTA652VpMCoYDS8KnBSMiuFMFyv3aeg7aMUA6vKoYdp2UIm5Zlpwk0Zol9fCmobLMt4zbBZFt28+Xedfrq5u8+vz2/do6eTY1s3xBPzvAF6re1kphgYxoIPgfEbhQ/0OOXgn9InSXRppvsQwop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LsrM8bs0zIeNzNwl0U7XFSb3A2Xm/yVuNOA629izmfiyzSqBYRTKhmUCfjLO/L/EmCpHUfiWyt4yjAYew2Eloz97dtTsKz5ErO7yQrXSZr8kYEXNMZ7Zi5OQNR/V+F0yTolAil0Rwtnfdg3PpvggqETBqAwffxPNNMwpuF/hENYi0DLqaAl2xQbDYvxXBP/qV7BixXMSqFUV5PTiS2EaZQQp3tnBKuDeqf9JFfqQ3o0fSneqd4Z/821VBv/eizWNmNkU7ysWOb73xELwEnMKm10wB+mtfGynZrZYDZ8SFyNa32wI6TcWX3b1B/mJ14hotMSF2W1TAhB0r/3LcOcB9qxHduSzzDTE41FNHSTkxJAK2U5W4C72l2yTH31+krpsrPsJHy93EJRtYyyB4OWsd3fywMWzfVIIkpsF4il1702Q3HXPYJq4RWDsCw2xEzn15XlG3qY6sVOI+xuGuv2Us/v90HtesapoK2a5GUowTpChauFr4fIi9B7fBfpnPDprSzupm8ExPD95cTq+BTFF/wBQSwECHgMKAAAACAAREsJQx0jPHDkGAACWEQAACAAAAAAAAAAAAAAA8wIAAAAAaW5kZXgucHlQSwUGAAAAAAEAAQA2AAAAXwYAAAAA" }, "func_name": "download_file_from_obs", "handler": "index.handler", "memory_size": 256, "package": "default", "runtime": "Python2.7", "timeout": 30 }
- Sample response
{ "func_urn": "urn:fss:{project_name}:{project_id}:function:default:download_file_from_obs:latest", "func_name": "download_file_from_obs", "domain_id": "89fexxxd636", "namespace": "{project_id}", "project_name": "xxx", "package": "default", "runtime": "Python2.7", "timeout": 30, "handler": "index.handler", "memory_size": 256, "cpu": 400, "code_type": "inline", "code_filename": "index.zip", "code_size": 1707, "digest": "68891a6778848a78bd37a8c0798c91d75a5c87aee6e901303047a52edf05bf2170aac4149d79b3f6a40efe78406a83bf6d8683e7b25da4f0c07e7493aa4ccdcd", "version": "latest", "image_name": "latest-200603162219@zr2ym", "last_modified": "2020-06-03T16:22:19+08:00", "strategy_config": { "concurrency": -1 }, "StrategyConfig": {}, "enterprise_project_id": "0" }
Record the URN of the function, that is, the value of func_urn in the response.
Step 2: Modify the OBS Address, Bucket Name, and File Name in the Function Configurations
URI: PUT /v2/{project_id}/fgs/functions/{function_urn}/config
For details, see Modifying the Metadata of a Function.
- Sample request
PUT https://{Endpoint}/v2/{project_id}/fgs/functions/{function_urn}/config { "func_name": "download_file_from_obs", "handler": "index.handler", "memory_size": 256, "runtime": "Python2.7", "timeout": 30, "user_data": "{\"obs_address\":\"obs.xxx.xxx.com\",\"srcBucket\":\" xxx\",\"srcObjName\":\"xxx\"}", "xrole": "xxx" }
function_urn indicates the function URN recorded in Step 1: Create a Function for Downloading Files from OBS; obs_address indicates the OBS address; srcBucket indicates the name of an OBS bucket, srcObjName indicates a file name, and xrole indicates an agency name.
- Sample response
{ "func_urn": "urn:fss:{project_name}:{project_id}:function:default:download_file_from_obs:latest", "func_name": "download_file_from_obs", "domain_id": "89fexxxd636", "namespace": "{project_id}", "project_name": "xxx", "package": "default", "runtime": "Python2.7", "timeout": 30, "handler": "index.handler", "memory_size": 256, "cpu": 400, "code_type": "inline", "code_filename": "index.zip", "code_size": 1707, "user_data": "{\"obs_address\":\"obs.xxx.xxx.com\",\"srcBucket\":\"xxx\",\"srcObjName\":\"xxx\"}", "digest": "68891a6778848a78bd37a8c0798c91d75a5c87aee6e901303047a52edf05bf2170aac4149d79b3f6a40efe78406a83bf6d8683e7b25da4f0c07e7493aa4ccdcd", "version": "latest", "image_name": "latest-200603165355@varrp", "xrole": "xxx", "app_xrole": "xxx", "last_modified": "2020-06-03T17:25:03+08:00", "strategy_config": { "concurrency": -1 }, "StrategyConfig": {}, "enterprise_project_id": "0" }
Step 3: Test the Function
URI: POST /v2/{project_id}/fgs/functions/{function_urn}/invocations
For details, see Executing a Function Synchronously.
- Sample request
POST https://{Endpoint}/v2/{project_id}/fgs/functions/{function_urn}/invocations { "message": "download file" }
function_urn indicates the function URN recorded in Step 1: Create a Function for Downloading Files from OBS.
- Sample response
"The object downloaded successfully from OBS, and the size is 14 KB"
Step 4: Create a Timer Trigger to Periodically Download Files from OBS
URI: POST /v2/{project_id}/fgs/triggers/{function_urn}
For details, see Creating a Trigger.
- Sample request
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "event_data": { "name": "Timer-download", "schedule_type": "Rate", "schedule": "1d" }, "event_type_code": "MessageCreated", "trigger_status": "ACTIVE", "trigger_type_code": "TIMER" }
function_urn indicates the function URN recorded in Step 1: Create a Function for Downloading Files from OBS.
The preceding example request is used to download files from the specified OBS bucket every day.
- Sample response
{ "trigger_id": "461bbe95-c85b-4dc9-a306-9701e77f1d66", "trigger_type_code": "TIMER", "trigger_status": "ACTIVE", "event_data": { "name": "Timer-download", "schedule": "1d", "schedule_type": "Rate" }, "last_updated_time": "2020-06-04T10:33:30+08:00", "created_time": "2020-06-04T10:33:30+08:00" }
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