tencent cloud

Serverless Cloud Function

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Related Concepts
How It Works
Strengths
Scenarios
Related Products
Purchase Guide
Billing Overview
Billing Mode
Billable Items and Billing Modes
Function Computing Power Support
Free Tier
SCF Pricing
Billing Example
Payment Overdue
Getting Started
Creating Event Function in Console
User Guide
Quota Management
Managing Functions
Web Function Management
Log Management
Concurrence Management
Trigger Management
Function URL
A Custom Domain Name
Version Management
Alias Management
Permission Management
Running Instance Management
Plugin Management
Managing Monitors and Alarms
Network Configuration
Layer Management
Execution Configuration
Extended Storage Management
DNS Caching Configuration
Resource Managed Mode Management
Near-Offline Resource Hosting Model
Workflow
Triggers
Trigger Overview
Trigger Event Message Structure Summary
API Gateway Trigger
COS Trigger
CLS Trigger
Timer Trigger
CKafka Trigger
Apache Kafka Trigger
MQTT Trigger
Trigger Configuration Description
MPS Trigger
CLB Trigger Description
TencentCloud API Trigger
Development Guide
Basic Concepts
Testing a Function
Environment Variables
Dependency Installation
Using Container Image
Error Types and Retry Policies
Dead Letter Queue
Connecting SCF to Database
Automated Deployment
Cloud Function Status Code
Common Errors and Solutions
Developer Tools
Serverless Web IDE
Calling SDK Across Functions
Third-Party Tools
Code Development
Python
Node.js
Golang
PHP
Java
Custom Runtime
Deploying Image as Function
Web Framework Development
Deploying Framework on Command Line
Quickly Deploying Egg Framework
Quickly Deploying Express Framework
Quickly Deploying Flask Framework
Quickly Deploying Koa Framework
Quickly Deploying Laravel Framework
Quickly Deploying Nest.js Framework
Quickly Deploying Next.js Framework
Quickly Deploying Nuxt.js Framework
Quickly Deploying Django Framework
Use Cases
Overview
Solutions with Tencent Cloud Services
Business Development
TRTC Practices
COS Practices
CKafka Practice
CLS
CLB Practice
MPS
CDN
CDWPG
VOD
SMS
ES
Scheduled Task
Video Processing
Success Stories
Tencent Online Education
Online Video Industry
Tencent Online Education
Best Practice of Tencent IEG Going Global
API Documentation
History
Introduction
API Category
Making API Requests
Other APIs
Namespace APIs
Layer Management APIs
Async Event Management APIs
Trigger APIs
Function APIs
Function and Layer Status Description
Data Types
Error Codes
SDK Documentation
FAQs
General
Web Function
Billing FAQs
Network FAQs
Log FAQs
SCF utility class
Event Handling FAQs
API Gateway Trigger FAQs
Related Agreement
Service Level Agreement
Contact Us
Glossary

SDK Documentation

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 2024-12-02 16:29:12

Preparations for Development

Before installing the SDK and using TencentCloud API for the first time, you need to apply for security credentials in the Tencent Cloud console, which consists of SecretId and SecretKey. SecretId is used to identify the API requester, while SecretKey is a key used for signature string encryption and authentication by the server. Please keep your SecretKey private and avoid disclosure.

Installing SDK

API

The commonly used APIs of SCF are as follows. For more APIs, please see the API documentation.
API
Description
Creates function
Deletes function
Gets function details
Executes function
Gets function list
Updates function code
Updates function configuration

Use Cases

Python
Node.js
PHP
Take Python3.6 as an example:

# -*- coding: utf8 -*-

import json
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# Import the client models of the corresponding product module
from tencentcloud.scf.v20180416 import scf_client,models

# API name of the corresponding API
action = 'Invoke'

# API parameter. Enter the name of the function to be invoked, `RequestResponse` (sync), and `Event` (async)
action_params = {
'FunctionName': "function-name",
'InvocationType': "Event"
}

print('Start SCF')

def main_handler(event, context):
try:
# Instantiate an authentication object. The Tencent Cloud account `secretId` and `secretKey` need to be passed in as the input parameters
cred = credential.Credential("SecretId", "SecretKey")

# Instantiate the client object to request the product and the region where the function is located
client = scf_client.ScfClient(cred, "ap-shanghai")

# Call the API, initiate the request, and print the returned result
ret = client.call(action, action_params)

print(json.loads(ret)["Response"]["Result"]["RetMsg"])

except TencentCloudSDKException as err:
print(err)


Take Node.js12.16 as an example:
Note:
If you use the SDK in a layer, please specify the absolute path in the code, i.e., /opt/node_modules/tencentcloud-sdk-nodejs.
'use strict';
const tencentcloud = require("/var/user/node_modules/tencentcloud-sdk-nodejs");
// Import the client models of the corresponding product module
const ScfClient = tencentcloud.scf.v20180416.Client;
const models = tencentcloud.scf.v20180416.Models;

const clientConfig = {
// Tencent Cloud authentication information
credential: {
secretId: "secretId",
secretKey: "secretKey",
},
// Product region
region: "ap-beijing",
profile:{}
}

exports.main_handler = (event, context) => {
console.log(event)
// console.log(context)

// Instantiate the client object to request the product and the region where the function is located
const client = new ScfClient(clientConfig);

console.log("Start SCF")
// Call the API you want to access through the client object; you need to pass in the request object and the response callback function
client.Invoke({"FunctionName":"function-name","InvocationType":"Event"}, function(err, response) {
// The request returns an exception and the exception information is printed
if (err) {
console.log(err);
return;
}
// The request is returned normally, and the `response` object is printed
console.log("success");
});
};


Usage sample of SCF's built-in SDK

The version of the built-in tencentcloud-sdk-nodejs varies by Node.js runtime environment version. For more information, please see Notes on Node.js.
Take Node.js12.16 as an example:
'use strict';

const tencentcloud = require("tencentcloud-sdk-nodejs");
const Credential = tencentcloud.common.Credential;

// Import the client models of the corresponding product module
const ScfClient = tencentcloud.scf.v20180416.Client;
const models = tencentcloud.scf.v20180416.Models;

exports.main_handler = (event, context) = {
console.log(event)
// console.log(context)

// Instantiate an authentication object. The Tencent Cloud account `secretId` and `secretKey` need to be passed in as the input parameters
let cred = new Credential("SecretId", "SecretKey");

// Instantiate the client object to request the product and the region where the function is located
let client = new ScfClient(cred, "ap-beijing");

// Instantiate a request object and call `invoke()`
console.log("Start SCF")
let request = new models.InvokeRequest();
// API parameter. Enter the name of the function to be invoked, `RequestResponse` (sync), and `Event` (async)
let params = '{"FunctionName":"function-name", "InvocationType":"Event"}'
request.from_json_string(params);
// Call the API you want to access through the client object; you need to pass in the request object and the response callback function
client.Invoke(request, function(err, response) {
// The request returns an exception and the exception information is printed
if (err) {
console.log(err);
return;
}
// The request is returned normally, and the `response` object is printed
console.log(response.to_json_string());
});
};

Example:
<?php
require_once '/var/user/tencentcloud-sdk-php/TCloudAutoLoader.php'; # Pay attention to the import path
use TencentCloud\\Common\\Credential;
use TencentCloud\\Common\\Profile\\ClientProfile;
use TencentCloud\\Common\\Profile\\HttpProfile;
use TencentCloud\\Common\\Exception\\TencentCloudSDKException;
use TencentCloud\\Scf\\V20180416\\ScfClient;
use TencentCloud\\Scf\\V20180416\\Models\\InvokeRequest;
function main_handler($event, $context) {
print "good";
print "\\n";
var_dump($event);
var_dump($context);
try {
// Instantiate an authentication object. The Tencent Cloud account `secretId` and `secretKey` need to be passed in as the input parameters
$cred = new Credential("SecretId", "SecretKey");
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("scf.tencentcloudapi.com");

$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
// Instantiate the client object to request the product and the region where the function is located
$client = new ScfClient($cred, "ap-shanghai", $clientProfile);
$req = new InvokeRequest();
// API parameter. Enter the name of the function to be invoked, `RequestResponse` (sync), and `Event` (async)
$params = '{"FunctionName":"function-name", "InvocationType":"Event"}';
$req->fromJsonString($params);
$resp = $client->Invoke($req);
print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
echo $e;
}
return "hello";
}
?>


Packaging and Deployment

If you need to deploy a function in the SCF console and use the SDK to invoke other functions, you need to package the tencentcloud library and function code together into a zip file.
Please note that the execution method specified when the function is created in the console must correspond to the code file and execution function in the zip file.
If the generated zip package is larger than 50 MB, it should be uploaded through COS.
The default call rate limit for TencentCloud API is 20 calls per second. If you need to increase the limit for high concurrence, please submit a ticket for application.

API Explorer

API Explorer provides various capabilities such as online call, signature verification, SDK code generation, and quick API search, greatly improving the ease of use of TencentCloud API.

Relevant Information

You can also use Tencent SCF SDK (Tencentserverless SDK), which integrates SCF business flow APIs to simplify the function invocation method and eliminates your need to encapsulate public TencentCloud APIs. For more information, please see Calling SDK Across Functions.

Ajuda e Suporte

Esta página foi útil?

comentários