fc-nodejs-sdk (英语)
联合国 http://doxmate.cool/aliyun/fc-nodejs-sdk/api.html 页面存档备份,存于互联网档案馆
通知
我们建议使用fc2, fc与fc2的主要区别是:
用户返回的响应是以下对象.
{
'headers': headers,
'data': data,
}
对于引用函数,数据是您代码返回的结果。 默认情况下,如果想要获得原始缓冲,数据由utf8解码,那么在引用函数时,只需在选项中设置 {rawBuf: true} .
实例见以下代码:, 对于其他类人鱼,数据为对象.
安装正式的 fc2 发布版本 :
npm install @alicloud/fc2 --save
安装旧版本
fc 版本在 1.x 分支中,您可以安装 fc 像这样使用 “ npm ”
npm install @alicloud/fc --save
许可证
实例
答应
'use strict';
var FCClient = require('@alicloud/fc2');
var client = new FCClient('<account id>', {
accessKeyID: '<access key id>',
accessKeySecret: '<access key secret>',
region: 'cn-shanghai',
timeout: 10000 // Request timeout in milliseconds, default is 10s
});
var serviceName = '<service name>';
var funcName = '<function name>';
client.createService(serviceName).then(function(resp) {
console.log('create service: %j', resp);
return client.createFunction(serviceName, {
functionName: funcName,
handler: 'index.handler',
memorySize: 128,
runtime: 'nodejs4.4',
code: {
zipFile: fs.readFileSync('/tmp/index.zip', 'base64'),
},
});
}).then(function(resp) {
console.log('create function: %j', resp);
return client.invokeFunction(serviceName, funcName, 'event');
}).then(function(resp) {
console.log('invoke function: %j', resp);
}).catch(function(err) {
console.error(err);
});
aync/awa (节点 qq 7.6)
'use strict';
const FCClient = require('@alicloud/fc2');
var client = new FCClient('<account id>', {
accessKeyID: '<access key id>',
accessKeySecret: '<access key secret>',
region: 'cn-shanghai',
});
var serviceName = '<service name>';
var funcName = '<function name>';
async function test () {
try {
var resp = await client.createService(serviceName);
console.log('create service: %j', resp);
resp = await client.createFunction(serviceName, {
functionName: funcName,
handler: 'counter.handler',
memorySize: 128,
runtime: 'nodejs4.4',
initializer: 'counter.initializer',
code: {
zipFile: fs.readFileSync('/tmp/counter.zip', 'base64'),
},
});
console.log('create function: %j', resp);
// by default , resp is decoded by utf8
resp = await client.invokeFunction(serviceName, funcName, null);
console.log('invoke function: %j', resp);
// respWithBuf is returned as buffer if isRawBuf is set to be true in opts
respWithBuf = await client.invokeFunction(serviceName, funcName, null, {}, 'LATEST', {rawBuf:true});
uResp = await client.updateFunction(serviceName, funcName, {
description: 'updated function desc',
initializationTimeout: 60,
});
console.log('update function: %j', resp);
} catch (err) {
console.error(err);
}
}
test().then();
自定义信头
我们提供两种方式来定制请求信头.
一种方法是通过客户端构建器传递信头. 您应当将通过构建器的页眉视为默认自定义页眉, 因为所有请求都会使用此页眉 .
var client = new FCClient('<account id>', {
accessKeyID: '<access key id>',
accessKeySecret: '<access key secret>',
region: 'cn-shanghai',
headers: {
'x-fc-invocation-type': 'Async'
}
});
await client.invokeFunction(serviceName, funcName, 'event');
另一种方法是通过函数的参数传递头. 需要通过特定函数的页眉时,请使用这种方式.
await client.invokeFunction(serviceName, funcName, 'event', {
'x-fc-invocation-type': 'Async'
});
在使用两种方式时,会合并标题. 但对于有相同密钥的头头,由函数参数提供的头头会比起由构造器提供的头头.
API 光谱
见: https://help.aliyun.com/document detail/52877.html (中文(简体) )
测试
ACCOUNT_ID=<ACCOUNT_ID> ACCESS_KEY_ID=<ACCESS_KEY_ID> ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET> make test