开发者社区> 问答> 正文

函数计算的执行环境

当函数被调用时,函数的代码会在一个受限的环境中执行:

环境配置备注
执行用户普通用户(无root权限)
代码目录env["FC_FUNC_CODE_PATH"]
可写目录/tmp(其他目录是只读的)
操作系统Linux 4.4.24-2.al7.x86_64
网络允许访问公网及同区域的阿里云服务(例如OSS/TableStore等)内网

用户可以通过这个 Web Shell工具来体验函数的执行环境。
不同编程语言的环境中,还会包含一些常用的库,具体参见每个语言的编程文档:

使用代码目录


如果用户将一些配置文件或者数据文件与代码一起打包上传,并且需要在代码中访问这些文件的话,就需要使用FC_FUNC_CODE_PATH这个环境变量来获取文件的绝对路径,下面分别给出使用Python和Node.js的示例:
Python:
  1. [backcolor=transparent]import[backcolor=transparent] os
  2. [backcolor=transparent]def[backcolor=transparent] handler[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent]):
  3. [backcolor=transparent]    cfg_file [backcolor=transparent]=[backcolor=transparent] os[backcolor=transparent].[backcolor=transparent]environ[backcolor=transparent][[backcolor=transparent]'FC_FUNC_CODE_PATH'[backcolor=transparent]][backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]'/config.json'
  4. [backcolor=transparent]    [backcolor=transparent]print[backcolor=transparent] cfg_file

Node.js:
  1. [backcolor=transparent]exports[backcolor=transparent].[backcolor=transparent]handler [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent],[backcolor=transparent] callback[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]  cfgFile [backcolor=transparent]=[backcolor=transparent] process[backcolor=transparent].[backcolor=transparent]env[backcolor=transparent][[backcolor=transparent]'FC_FUNC_CODE_PATH'[backcolor=transparent]][backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]'/config.json'[backcolor=transparent];
  3. [backcolor=transparent]  console[backcolor=transparent].[backcolor=transparent]log[backcolor=transparent]([backcolor=transparent]cfgFile[backcolor=transparent]);
  4. [backcolor=transparent]  callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'done'[backcolor=transparent]);
  5. [backcolor=transparent]}


使用内网域名


在函数中访问其他云服务,建议使用内网域名,一方面使用内网域名能够有更好的性能;另一方面可以避免公网流量收费。下面分别给出使用Python和Node.js使用内网域名访问OSS的示例:
Python:
  1. [backcolor=transparent]import[backcolor=transparent] oss2
  2. [backcolor=transparent]def[backcolor=transparent] my_handler[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent]):
  3. [backcolor=transparent]    creds [backcolor=transparent]=[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials
  4. [backcolor=transparent]    auth [backcolor=transparent]=[backcolor=transparent] oss2[backcolor=transparent].[backcolor=transparent]StsAuth[backcolor=transparent]([backcolor=transparent]creds[backcolor=transparent].[backcolor=transparent]accessKeyId[backcolor=transparent],[backcolor=transparent] creds[backcolor=transparent].[backcolor=transparent]accessKeySecret[backcolor=transparent],[backcolor=transparent] creds[backcolor=transparent].[backcolor=transparent]securityToken[backcolor=transparent])
  5. [backcolor=transparent]    bucket [backcolor=transparent]=[backcolor=transparent] oss2[backcolor=transparent].[backcolor=transparent]Bucket[backcolor=transparent]([backcolor=transparent]auth[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'oss-cn-shanghai-internal.aliyuncs.com'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'my-bucket'[backcolor=transparent])
  6. [backcolor=transparent]    bucket[backcolor=transparent].[backcolor=transparent]put_object[backcolor=transparent]([backcolor=transparent]'my-object'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'hello world'[backcolor=transparent])

Node.js:
  1. [backcolor=transparent]var[backcolor=transparent] [backcolor=transparent]OSSClient[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] require[backcolor=transparent]([backcolor=transparent]'ali-oss'[backcolor=transparent]).[backcolor=transparent]Wrapper[backcolor=transparent];
  2. [backcolor=transparent]exports[backcolor=transparent].[backcolor=transparent]handler [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent],[backcolor=transparent] callback[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]  console[backcolor=transparent].[backcolor=transparent]log[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
  4. [backcolor=transparent]  [backcolor=transparent]var[backcolor=transparent] ossClient [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OSSClient[backcolor=transparent]({
  5. [backcolor=transparent]    accessKeyId[backcolor=transparent]:[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials[backcolor=transparent].[backcolor=transparent]accessKeyId[backcolor=transparent],
  6. [backcolor=transparent]    accessKeySecret[backcolor=transparent]:[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials[backcolor=transparent].[backcolor=transparent]accessKeySecret[backcolor=transparent],
  7. [backcolor=transparent]    stsToken[backcolor=transparent]:[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials[backcolor=transparent].[backcolor=transparent]securityToken[backcolor=transparent],
  8. [backcolor=transparent]    region[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'oss-cn-shanghai'[backcolor=transparent],
  9. [backcolor=transparent]    internal[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]true[backcolor=transparent],
  10. [backcolor=transparent]    bucket[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'my-bucket'[backcolor=transparent],
  11. [backcolor=transparent]  [backcolor=transparent]});
  12. [backcolor=transparent]  ossClient[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]'my-object'[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Buffer[backcolor=transparent]([backcolor=transparent]'hello world'[backcolor=transparent]))
  13. [backcolor=transparent]    [backcolor=transparent].[backcolor=transparent]then[backcolor=transparent]([backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]res[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  14. [backcolor=transparent]      callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] res[backcolor=transparent]);
  15. [backcolor=transparent]    [backcolor=transparent]}).[backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]err[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  16. [backcolor=transparent]      callback[backcolor=transparent]([backcolor=transparent]err[backcolor=transparent]);
  17. [backcolor=transparent]    [backcolor=transparent]});
  18. [backcolor=transparent]};

展开
收起
boxti 2017-10-20 10:12:21 2483 0
1 条回答
写回答
取消 提交回答
  • 学习了

    2019-11-12 11:03:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Hologres Serverless之路:揭秘弹性计算组 立即下载
Serverless开发平台: 让研发效能再提升 立即下载
Serverless 引领云上研发新范式 立即下载