文档首页/ 函数工作流 FunctionGraph/ API参考/ 应用示例/ 示例1:创建函数和Timer触发器实现定时从OBS桶中下载文件
更新时间:2024-03-21 GMT+08:00
分享

示例1:创建函数和Timer触发器实现定时从OBS桶中下载文件

场景描述

本章节以Python 2.7为例,指导您通过API创建FunctionGraph函数和Timer触发器,实现定时从OBS桶中下载文件。

API的调用方法请参见如何调用API

前提条件

  • 已在OBS服务中上传文件,并记录文件名、文件所在的OBS桶名,以及OBS地址的链接。
  • 已配置具有OBS服务访问权限的委托,并记录委托名。

总体思路

创建FunctionGraph函数和Timer触发器,实现定时从OBS桶中下载文件,步骤如下:

  1. 创建函数:创建下载文件的函数。
  2. 修改函数的metadata信息:修改函数配置信息中的OBS地址、OBS桶名和文件名。
  3. 同步执行函数:确认函数能够成功从OBS桶中下载文件。
  4. 创建触发器:创建Timer触发器,实现定时下载文件。

步骤1:创建下载文件的函数

URI:POST /v2/{project_id}/fgs/functions

API文档详情请参见:创建函数

  • 请求示例
    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/rTmraBHnnj2fzTA652VpMCoYDS8KnBSMiuFMFyv3aeg7aMUA6vKoYdp2UIm5Zlpwk0Z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EeLVW/fzqy3cAh4CBtPHaCG+d0ZknoaenM8RlIj3QoqDsQ6Cf3ER1HthPPMw+cWx3J8G6DTNy+E2GM2v+NdLsrM8bs0zIeNzNwl0U7XFSb3A2Xm/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
    }
  • 响应示例
    {
     "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"
    }

    记录函数的URN,即响应示例中的“func_urn”信息。

步骤2:修改函数配置信息中的OBS地址、OBS桶名和文件名

URI:PUT /v2/{project_id}/fgs/functions/{function_urn}/config

API文档详情请参见:修改函数的metadata信息

  • 请求示例
    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.example.example.com\",\"srcBucket\":\" xxx\",\"srcObjName\":\"xxx\"}",
     "xrole": "xxx"
    }

    function_urn为步骤1:创建下载文件的函数中记录的函数URN,obs_address为OBS地址,srcBucket为OBS桶名,srcObjName为文件名,xrole为委托名。

  • 响应示例
    {
     "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.example.example.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"
    }

步骤3:确认函数能够成功从OBS桶中下载文件

URI:POST /v2/{project_id}/fgs/functions/{function_urn}/invocations

API文档详情请参见:同步执行函数

  • 请求示例
    POST  https://{Endpoint}/v2/{project_id}/fgs/functions/{function_urn}/invocations
    {
     "message": "download file"
    }

    function_urn为步骤1:创建下载文件的函数中记录的函数URN。

  • 响应示例
    "The object downloaded successfully from OBS, and the size is 14 KB"

步骤4:创建Timer触发器,实现定时下载文件

URI:POST /v2/{project_id}/fgs/triggers/{function_urn}

API文档详情请参见:创建触发器

  • 请求示例
    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为步骤1:创建下载文件的函数中记录的函数URN。

    上述请求示例表示每天从OBS桶中下载文件。

  • 响应示例
    {
     "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"
    }

相关文档