Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


This demand requires the asynchronous operation of pulling files from COS and uploading them to another COS after being compressed on the server side, because the synchronous function has a limit on the running time of the function. When a large task runs for a long time, it will be forced to suspend when the task is not finished and the running time of the function times out

Train of thought

Two cloud functions are required, one for access request, authentication, and control entry. The other one does the real work, downloading the compression, notifying the status

The following

How do I create B asynchronous functions

This is the same as a synchronous function, except that the asynchronous option is selected when the function is created

Why use asynchronous functions

Because asynchronous functions run for a maximum of 24 hours. The synchronization function can only be used for a maximum of 900 seconds

A Synchronization function code

1. Define the function configuration

const funConfig = {
  FunctionName: 'ZipFile-ay-0903'

Copy the code

Defines the return value function used by the formatting API gateway

const reJson = function(data,state = 200){
  return {
    "isBase64Encoded": false,
    "statusCode": state,
    "headers": {"Content-Type": "application/json"},
    "body": JSON.stringify(data)
Copy the code

Get the parameter

const { getParams } = require('./common/utils'); Const {secretId, secretKey, token, bucket, region, key, sourceList, sourceConfigList, flatten, } = getParams(event, context);Copy the code

Introduce the cloud function SDK

    const tencentcloud = require("tencentcloud-sdk-nodejs");

    const ScfClient = tencentcloud.scf.v20180416.Client;

    const clientConfig = {
      credential: {
        secretId: secretId,
        secretKey: secretKey,
      region: region,
      profile: {
        httpProfile: {
          endpoint: "",

    const client = new ScfClient(clientConfig);
Copy the code

Configure the parameters to be used to call the B asynchronous function to run

const params = {
      "FunctionName": funConfig.FunctionName,
      "InvocationType": "Event",
      "ClientContext": JSON.stringify({
        "secretId": secretId,
        "secretKey": secretKey,
        "bucket": bucket,
        "region": region,
        "key": key,
        "sourceList": sourceList
Copy the code

Details of calling the B asynchronous function

/ / get function running state const funDetailState = {} try {const data = await client. GetReservedConcurrencyConfig ({" FunctionName ": Funconfig. FunctionName}) console.log(' Get predicted concurrency: --',data); funDetailState = data; } catch (error) { // return reJson(error) }Copy the code

Call the B asynchronous function and return the status

try { const data = await client.Invoke(params) data.funDetailState = funDetailState console.log('data-type',typeof data)  return reJson(data) } catch (error) { return reJson(error) }Copy the code

Code B Asynchronous function code

Asynchronous function parameters synchronize function code.

The problem

Unable to get reserved memory for the current function…

Always return null

The real environment is also returned as NULL

The official document states that it is possible to miss it, but does not say why, nor does it say how to fix it

Solution Queue execution

Serial guarantees that only one task will be executed at a time


  • 1. No authentication
  • 2, no check, direct call, easy to be squeezed dead B function
  • 3. The resource running memory needs to be applied and has not been applied yet