更新时间:2025-05-28 gmt 08:00
示例2:上传代码到obs桶,并使用obs链接创建函数-j9九游会登录
场景描述
本章节以python 2.7为例,指导您在本地编码,然后将代码文件上传到obs桶,实现通过obs链接创建函数。
api的调用方法请参见。
前提条件
已在obs服务中创建obs桶。
约束与限制
请确保函数与obs桶创建在同一区域下。
总体思路
在本地进行函数编码后,将代码文件上传到obs桶,并使用obs链接创建函数。步骤如下:
- 创建函数工程:在本地进行编码实现函数功能。
- 上传工程到obs桶:将代码文件压缩为zip格式并上传到已创建的obs桶中,记录代码文件的obs链接。
- 创建函数:调用api通过obs链接创建函数。
步骤1:创建函数工程
- 编写打印helloworld的代码。
打开文本编辑器,编写helloworld函数,代码如下,文件命名为“helloworld.py”,保存文件。
def printhello(): print 'hello world!' - 定义functiongraph函数。
打开文本编辑器,定义函数,代码如下,文件命名为“index.py”,保存文件(与“helloworld.py”保存在同一文件夹下)。
import json import helloworld def handler (event, context): output =json.dumps(event) helloworld.printhello() return output
步骤2:上传工程到obs桶
- 在函数工程中,选中“helloworld.py”和“index.py”文件,打包压缩为“fss_examples_python2.7.zip”。
- 上传“fss_examples_python2.7.zip”到obs桶,记录obs链接。
步骤3:调用创建函数api,使用obs链接创建函数
uri:post /v2/{project_id}/fgs/functions
api文档详情请参见:创建函数
- 请求示例
post https://{endpoint}/v2/{project_id}/fgs/functions { "code_type": "obs", "code_url": "https://test.obs.example.example.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 }“code_url”为2中记录的obs链接。
- 响应示例
{ "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.example.example.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" }
父主题:
相关文档
意见反馈
文档内容是否对您有帮助?
提交成功!非常感谢您的反馈,我们会继续努力做到更好!
您可在查看反馈及问题处理状态。
系统繁忙,请稍后重试
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨