安装 Node.js SDK 前,需要先获取安全凭证。在第一次使用云 API 之前,用户首先需要在腾讯云控制台上申请安全凭证,安全凭证包括 SecretId 和 SecretKey, SecretId 是用于标识 API 调用者的身份,SecretKey 是用于加密签名字符串和服务器端验证签名字符串的密钥。SecretKey 必须严格保管,避免泄露。
Node.js 8.9 版本
通过 npm 获取安装是使用 NODEJS SDK 的推荐方法,npm 是 Node.js 的包管理工具。关于 npm 详细可参考 npm 官网 。
npm install tencentcloud-sdk-nodejs --save
接口名称 | 接口功能 |
---|---|
CreateFunction | 创建函数 |
DeleteFunction | 删除函数 |
GetFunction | 获取函数详细信息 |
GetFunctionLogs | 获取函数运行日志 |
Invoke | 运行函数 |
ListFunctions | 获取函数列表 |
UpdateFunctionCode | 更新函数代码 |
UpdateFunctionConfiguration | 更新函数配置 |
'use strict';
const tencentcloud = require("/var/user/tencentcloud-sdk-nodejs");
const Credential = tencentcloud.common.Credential;
// 导入对应产品模块的client models。
const ScfClient = tencentcloud.scf.v20180416.Client;
const models = tencentcloud.scf.v20180416.Models;
exports.main_handler = (event, context, callback) => {
console.log("Hello World")
console.log(event)
// console.log(context)
callback(null, event);
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
let cred = new Credential("AKIxxxxxxPDpqj3C", "75rxxxxxxyJSODrMkx");
// 实例化要请求产品的client对象,以及函数所在的地域
let client = new ScfClient(cred, "ap-shanghai");
// 实例化一个请求对象,获取函数列表
console.log("Start ListFunctions")
let req = new models.ListFunctionsRequest();
// 通过client对象调用想要访问的接口,需要传入请求对象以及响应回调函数
client.ListFunctions(req, function(err, response) {
// 请求异常返回,打印异常信息
if (err) {
console.log(err);
return;
}
// 请求正常返回,打印response对象
console.log(response.to_json_string());
});
// 实例化一个请求对象,调用invoke()
console.log("Start Invoke")
let request = new models.InvokeRequest();
// 接口参数,输入需要调用的函数名,RequestResponse(同步) 和 Event(异步)
let params = '{"FunctionName":"test_python", "InvocationType":"RequestResponse"}'
request.from_json_string(params);
// 通过client对象调用想要访问的接口,需要传入请求对象以及响应回调函数
client.Invoke(request, function(err, response) {
// 请求异常返回,打印异常信息
if (err) {
console.log(err);
return;
}
// 请求正常返回,打印response对象
console.log(response.to_json_string());
},"test_python","RequestResponse");
};
如果需要在云函数控制台中部署函数,并使用 SDK 调用其他函数,则需要把 tencentcloud 的库和函数代码一起打包成 zip 文件。
您也可以使用腾讯云云函数 SDK(Tencentserverless SDK),该 SDK 集成云函数业务流接口,简化云函数的调用方法,使您无需再进行公有云 API 接口的封装。详情请参见 函数间调用 SDK。
本页内容是否解决了您的问题?