当函数被调用时,函数的代码会在一个受限的环境中执行:
用户可以通过这个
Web Shell工具来体验函数的执行环境。
不同编程语言的环境中,还会包含一些常用的库,具体参见每个语言的编程文档:
使用代码目录
如果用户将一些配置文件或者数据文件与代码一起打包上传,并且需要在代码中访问这些文件的话,就需要使用FC_FUNC_CODE_PATH这个环境变量来获取文件的绝对路径,下面分别给出使用Python和Node.js的示例:
Python:
- [backcolor=transparent]import[backcolor=transparent] os
- [backcolor=transparent]def[backcolor=transparent] handler[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent]):
- [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'
- [backcolor=transparent] [backcolor=transparent]print[backcolor=transparent] cfg_file
Node.js:
- [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]{
- [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];
- [backcolor=transparent] console[backcolor=transparent].[backcolor=transparent]log[backcolor=transparent]([backcolor=transparent]cfgFile[backcolor=transparent]);
- [backcolor=transparent] callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]'done'[backcolor=transparent]);
- [backcolor=transparent]}
使用内网域名
在函数中访问其他云服务,建议使用内网域名,一方面使用内网域名能够有更好的性能;另一方面可以避免公网流量收费。下面分别给出使用Python和Node.js使用内网域名访问OSS的示例:
Python:
- [backcolor=transparent]import[backcolor=transparent] oss2
- [backcolor=transparent]def[backcolor=transparent] my_handler[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent]):
- [backcolor=transparent] creds [backcolor=transparent]=[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials
- [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])
- [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])
- [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:
- [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];
- [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]{
- [backcolor=transparent] console[backcolor=transparent].[backcolor=transparent]log[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]var[backcolor=transparent] ossClient [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]OSSClient[backcolor=transparent]({
- [backcolor=transparent] accessKeyId[backcolor=transparent]:[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials[backcolor=transparent].[backcolor=transparent]accessKeyId[backcolor=transparent],
- [backcolor=transparent] accessKeySecret[backcolor=transparent]:[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials[backcolor=transparent].[backcolor=transparent]accessKeySecret[backcolor=transparent],
- [backcolor=transparent] stsToken[backcolor=transparent]:[backcolor=transparent] context[backcolor=transparent].[backcolor=transparent]credentials[backcolor=transparent].[backcolor=transparent]securityToken[backcolor=transparent],
- [backcolor=transparent] region[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'oss-cn-shanghai'[backcolor=transparent],
- [backcolor=transparent] internal[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]true[backcolor=transparent],
- [backcolor=transparent] bucket[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]'my-bucket'[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]});
- [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]))
- [backcolor=transparent] [backcolor=transparent].[backcolor=transparent]then[backcolor=transparent]([backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]res[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] res[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}).[backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]err[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] callback[backcolor=transparent]([backcolor=transparent]err[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]});
- [backcolor=transparent]};