forked from docs/doc-exports
Reviewed-by: Eotvos, Oliver <oliver.eotvos@t-systems.com> Co-authored-by: Chen, Junjie <chenjunjie@huawei.com> Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
145 lines
13 KiB
HTML
145 lines
13 KiB
HTML
<a name="functiongraph_06_0202"></a><a name="functiongraph_06_0202"></a>
|
|
|
|
<h1 class="topictitle1">Example 1: Using a Timer Trigger to Periodically Download Files from an OBS Bucket</h1>
|
|
<div id="body0000001483455909"><div class="section" id="functiongraph_06_0202__section11773144817815"><h4 class="sectiontitle">Scenario</h4><p id="functiongraph_06_0202__p49911552162316">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.</p>
|
|
<p id="functiongraph_06_0202__p414824518258">For details about how to call APIs, see <a href="functiongraph_06_0200.html">Calling APIs</a>.</p>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0202__section1835181812285"><h4 class="sectiontitle">Prerequisites</h4><ul id="functiongraph_06_0202__ul9181173917322"><li id="functiongraph_06_0202__li41811739133214">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.</li><li id="functiongraph_06_0202__li153261947173417">You have created an agency in IAM to allow FunctionGraph to access OBS and have recorded the name of the agency.</li></ul>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0202__section780392783814"><h4 class="sectiontitle">General Procedure</h4><p id="functiongraph_06_0202__p1455819135717">Create a FunctionGraph function and associate a timer trigger with it to periodically download files from an OBS bucket. The procedure is as follows:</p>
|
|
<ol id="functiongraph_06_0202__ol1214112417578"><li id="functiongraph_06_0202__li1614132435713"><a href="functiongraph_06_0108.html">Creating a Function</a>: Create a function for downloading files.</li><li id="functiongraph_06_0202__li141125317574"><a href="functiongraph_06_0111.html">Modifying the Metadata of a Function</a>: Modify the OBS address, OBS bucket name, and file name in the function configuration.</li><li id="functiongraph_06_0202__li6333145995718"><a href="functiongraph_06_0125.html">Executing a Function Synchronously</a>: Verify whether the function can successfully download files from the OBS bucket.</li><li id="functiongraph_06_0202__li99482315813"><a href="functiongraph_06_0122.html">Creating a Trigger</a>: Create a timer trigger to periodically download files.</li></ol>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0202__section14318132594418"><a name="functiongraph_06_0202__section14318132594418"></a><a name="section14318132594418"></a><h4 class="sectiontitle">Step 1: Create a Function for Downloading Files from OBS</h4><p id="functiongraph_06_0202__p2329630164518">URI: <strong id="functiongraph_06_0202__b62341437175011">POST /v2/</strong><em id="functiongraph_06_0202__i1953254205016">{project_id}</em><strong id="functiongraph_06_0202__b286313995012">/fgs/functions</strong></p>
|
|
<p id="functiongraph_06_0202__p19789173461318">For details, see <a href="functiongraph_06_0108.html">Creating a Function</a>.</p>
|
|
<ul id="functiongraph_06_0202__ul384014146144"><li id="functiongraph_06_0202__li5840141415149">Sample request<pre class="screen" id="functiongraph_06_0202__screen1037283512141">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/do6eTY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j3QoqDsQ6Cf3ER1HthPPMw+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
|
|
}</pre>
|
|
</li><li id="functiongraph_06_0202__li66591523101411">Sample response<pre class="screen" id="functiongraph_06_0202__screen34441738161412">{
|
|
"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"
|
|
}</pre>
|
|
<p id="functiongraph_06_0202__p84801155122812">Record the URN of the function, that is, the value of <strong id="functiongraph_06_0202__b1825020265114">func_urn</strong> in the response.</p>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0202__section399354318306"><h4 class="sectiontitle">Step 2: Modify the OBS Address, Bucket Name, and File Name in the Function Configurations</h4><p id="functiongraph_06_0202__p610711614313">URI: <strong id="functiongraph_06_0202__b167810199516">PUT /v2/</strong><em id="functiongraph_06_0202__i82221528115120">{project_id}</em><strong id="functiongraph_06_0202__b10874162210512">/fgs/functions/</strong><em id="functiongraph_06_0202__i7294173275116">{function_urn}</em><strong id="functiongraph_06_0202__b141021125115116">/config</strong></p>
|
|
<p id="functiongraph_06_0202__p2878549123117">For details, see <a href="functiongraph_06_0111.html">Modifying the Metadata of a Function</a>.</p>
|
|
<ul id="functiongraph_06_0202__ul91948326327"><li id="functiongraph_06_0202__li1819483263216">Sample request<pre class="screen" id="functiongraph_06_0202__screen8438502332">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"
|
|
}</pre>
|
|
<p id="functiongraph_06_0202__p8660343193715"><strong id="functiongraph_06_0202__b3283750165119">function_urn</strong> indicates the function URN recorded in <a href="#functiongraph_06_0202__section14318132594418">Step 1: Create a Function for Downloading Files from OBS</a>; <strong id="functiongraph_06_0202__b17284125018516">obs_address</strong> indicates the OBS address; <strong id="functiongraph_06_0202__b142847507517">srcBucket</strong> indicates the name of an OBS bucket, <strong id="functiongraph_06_0202__b72851550155110">srcObjName</strong> indicates a file name, and <strong id="functiongraph_06_0202__b1228625015519">xrole</strong> indicates an agency name.</p>
|
|
</li><li id="functiongraph_06_0202__li1717424611326">Sample response<pre class="screen" id="functiongraph_06_0202__screen15146539334">{
|
|
"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"
|
|
}</pre>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0202__section16598919223"><h4 class="sectiontitle">Step 3: Test the Function</h4><p id="functiongraph_06_0202__p85377458221">URI: <strong id="functiongraph_06_0202__b15644219175210">POST /v2/</strong><em id="functiongraph_06_0202__i5252028145210">{project_id}</em><strong id="functiongraph_06_0202__b558102335220">/fgs/functions/</strong><em id="functiongraph_06_0202__i52514323525">{function_urn}</em><strong id="functiongraph_06_0202__b11240725125217">/invocations</strong></p>
|
|
<p id="functiongraph_06_0202__p8926750152615">For details, see <a href="functiongraph_06_0125.html">Executing a Function Synchronously</a>.</p>
|
|
<ul id="functiongraph_06_0202__ul7353112314276"><li id="functiongraph_06_0202__li035372318274">Sample request<pre class="screen" id="functiongraph_06_0202__screen10314164811272">POST https://{Endpoint}/v2/{project_id}/fgs/functions/{function_urn}/invocations
|
|
{
|
|
"message": "download file"
|
|
}</pre>
|
|
<p id="functiongraph_06_0202__p1081914419121"><strong id="functiongraph_06_0202__b36075675315">function_urn</strong> indicates the function URN recorded in <a href="#functiongraph_06_0202__section14318132594418">Step 1: Create a Function for Downloading Files from OBS</a>.</p>
|
|
</li><li id="functiongraph_06_0202__li179641530162718">Sample response<pre class="screen" id="functiongraph_06_0202__screen1013815219277">"The object downloaded successfully from OBS, and the size is 14 KB"</pre>
|
|
</li></ul>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0202__section1644694317390"><h4 class="sectiontitle">Step 4: Create a Timer Trigger to Periodically Download Files from OBS</h4><p id="functiongraph_06_0202__p1261412284014">URI: <strong id="functiongraph_06_0202__b20634193532">POST /v2/</strong><em id="functiongraph_06_0202__i1870112917538">{project_id}</em><strong id="functiongraph_06_0202__b1723482214537">/fgs/triggers/</strong><em id="functiongraph_06_0202__i12605112510532">{function_urn}</em></p>
|
|
<p id="functiongraph_06_0202__p17634513409">For details, see <a href="functiongraph_06_0122.html">Creating a Trigger</a>.</p>
|
|
<ul id="functiongraph_06_0202__ul6250530131010"><li id="functiongraph_06_0202__li3251330101011">Sample request<pre class="screen" id="functiongraph_06_0202__screen242525671017">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"
|
|
}</pre>
|
|
<p id="functiongraph_06_0202__p2241123015127"><strong id="functiongraph_06_0202__b180914912539">function_urn</strong> indicates the function URN recorded in <a href="#functiongraph_06_0202__section14318132594418">Step 1: Create a Function for Downloading Files from OBS</a>.</p>
|
|
<p id="functiongraph_06_0202__p12244829134111">The preceding example request is used to download files from the specified OBS bucket every day.</p>
|
|
</li><li id="functiongraph_06_0202__li1130153451013">Sample response<pre class="screen" id="functiongraph_06_0202__screen184401659191018">{
|
|
"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"
|
|
}</pre>
|
|
</li></ul>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="functiongraph_06_0201.html">Examples</a></div>
|
|
</div>
|
|
</div>
|
|
|