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>
76 lines
6.4 KiB
HTML
76 lines
6.4 KiB
HTML
<a name="functiongraph_06_0204"></a><a name="functiongraph_06_0204"></a>
|
|
|
|
<h1 class="topictitle1">Example 3: Creating a Function by Uploading Code to an OBS Bucket</h1>
|
|
<div id="body0000001433696546"><div class="section" id="functiongraph_06_0204__section11773144817815"><h4 class="sectiontitle">Scenario</h4><p id="functiongraph_06_0204__p49911552162316">This example guides you through the procedure for uploading local code to an OBS bucket and creating a Python 2.7 function using the link URL of the OBS bucket.</p>
|
|
<p id="functiongraph_06_0204__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_0204__section1835181812285"><h4 class="sectiontitle">Prerequisites</h4><p id="functiongraph_06_0204__p1617616635011">An OBS bucket has been created.</p>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0204__section780392783814"><h4 class="sectiontitle">General Procedure</h4><p id="functiongraph_06_0204__p1455819135717">After writing function code in your local environment, upload the code file to an OBS bucket and use the link URL of the OBS bucket to create a function. The procedure is as follows:</p>
|
|
<ol id="functiongraph_06_0204__ol1214112417578"><li id="functiongraph_06_0204__li176151455143716">Write code in your local environment to create a function project.</li><li id="functiongraph_06_0204__li10531103311382">Compress the code file into a ZIP package, upload the package to the OBS bucket, and record the link URL of this bucket.</li><li id="functiongraph_06_0204__li99482315813">Call the API in <a href="functiongraph_06_0108.html">Creating a Function</a> to create a function using the link URL of the OBS bucket.</li></ol>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0204__section14741921773"><h4 class="sectiontitle">Step 1: Create a Function Project</h4><ol id="functiongraph_06_0204__ol7720173017415"><li id="functiongraph_06_0204__li1272093094112">Write code for printing text <strong id="functiongraph_06_0204__b14783105445816">helloworld</strong>.<p id="functiongraph_06_0204__p14755162734219">Open a text editor, compile a HelloWorld function, and save the code file as <strong id="functiongraph_06_0204__b74206587589">helloworld.py</strong>. The code is as follows:</p>
|
|
<pre class="screen" id="functiongraph_06_0204__screen11795531164318">def printhello():
|
|
print 'Hello world!' </pre>
|
|
</li><li id="functiongraph_06_0204__li188288813429">Define a FunctionGraph function.<p id="functiongraph_06_0204__p9194425443"><a name="functiongraph_06_0204__li188288813429"></a><a name="li188288813429"></a>Open a text editor, define a function, and save the function file as <strong id="functiongraph_06_0204__b1195569185918">index.py</strong> under the same directory as the <strong id="functiongraph_06_0204__b8955119125920">helloworld.py</strong> file. The function code is as follows:</p>
|
|
<pre class="screen" id="functiongraph_06_0204__screen0893153994618">import json
|
|
import helloworld
|
|
|
|
def handler (event, context):
|
|
output =json.dumps(event)
|
|
helloworld.printhello()
|
|
return output</pre>
|
|
</li></ol>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0204__section17199193504710"><h4 class="sectiontitle">Step 2: Upload the Project to an OBS Bucket</h4><ol id="functiongraph_06_0204__ol24191851124820"><li id="functiongraph_06_0204__li74191151154818">In the function project, select the <strong id="functiongraph_06_0204__b1626951965917">helloworld.py</strong> and <strong id="functiongraph_06_0204__b8269171913593">index.py</strong> files and compress them into <strong id="functiongraph_06_0204__b1826991975916">fss_examples_python2.7.zip</strong>.</li><li id="functiongraph_06_0204__li1966633145116"><a name="functiongraph_06_0204__li1966633145116"></a><a name="li1966633145116"></a>Upload the <strong id="functiongraph_06_0204__b895113224599">fss_examples_python2.7.zip</strong> package to the OBS bucket and record the link URL of the OBS bucket.</li></ol>
|
|
</div>
|
|
<div class="section" id="functiongraph_06_0204__section20413632205210"><h4 class="sectiontitle">Step 3: Call the Function Creation API to Create a Function Using the Link URL of the OBS Bucket</h4><p id="functiongraph_06_0204__p14668451125116">URI: <strong id="functiongraph_06_0204__b18262164055910">POST /v2/</strong><em id="functiongraph_06_0204__i719604615596">{project_id}</em><strong id="functiongraph_06_0204__b76323421596">/fgs/functions</strong></p>
|
|
<p id="functiongraph_06_0204__p19789173461318">For details, see <a href="functiongraph_06_0108.html">Creating a Function</a>.</p>
|
|
<ul id="functiongraph_06_0204__ul104224515521"><li id="functiongraph_06_0204__li5840141415149">Sample request<pre class="screen" id="functiongraph_06_0204__screen795591013534">POST https://{Endpoint}/v2/{project_id}/fgs/functions
|
|
{
|
|
"code_type": "obs",
|
|
"code_url": "https://test.obs.xxx.xxx.com/fss_examples_python2.7.zip",
|
|
"func_name": "create_function_from_obs",
|
|
"handler": "index.handler",
|
|
"memory_size": 256,
|
|
"package": "default",
|
|
"runtime": "Python2.7",
|
|
"timeout": 30
|
|
}</pre>
|
|
<p id="functiongraph_06_0204__p159479396915"><strong id="functiongraph_06_0204__b97338520595">code_url</strong> indicates the link URL of the OBS bucket recorded in <a href="#functiongraph_06_0204__li1966633145116">2</a>.</p>
|
|
</li><li id="functiongraph_06_0204__li687731534">Sample response<pre class="screen" id="functiongraph_06_0204__screen1560121485319">{
|
|
"func_urn": "urn:fss:{project_name}:{project_id}:function:default:create_function_from_obs:latest",
|
|
"func_name": "create_function_from_obs",
|
|
"domain_id": "0503xxxa960",
|
|
"namespace": "{project_id}",
|
|
"project_name": "xxx",
|
|
"package": "default",
|
|
"runtime": "Python2.7",
|
|
"timeout": 30,
|
|
"handler": "index.handler",
|
|
"memory_size": 256,
|
|
"cpu": 400,
|
|
"code_type": "obs",
|
|
"code_url": "https://test.obs.xxx.xxx.com/fss_examples_python2.7.zip",
|
|
"code_filename": "fss_examples_python2.7.zip",
|
|
"code_size": 436,
|
|
"digest": "3af770ada27514564b1a20d964cba4b35f432fa40f9fc4f4f7c1f0d2f42eac6cb4db1358c195235966b05f66b4664e7bf31c3f384a9066b3d1fcc3e96b4c3f65",
|
|
"version": "latest",
|
|
"image_name": "latest-200619100734@gjf4p",
|
|
"last_modified": "2020-06-19T10:07:34+08:00",
|
|
"strategy_config": {
|
|
"concurrency": -1
|
|
},
|
|
"StrategyConfig": {},
|
|
"enterprise_project_id": "0"
|
|
}</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>
|
|
|