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

demand

This time, the file from COS needs to be compressed on the server and uploaded to another COS

concept

Cloud API

Cloud.tencent.com/document/pr… With cloud apis, you can quickly manipulate cloud products with little code. To facilitate you to quickly and easily call the cloud API to manage Tencent cloud resources, the cloud API provides API Explorer, Tencent Cloud command line tool TCCLI, SDK, cloud API platform and API Inspector tool.

Tencent Cloud Developer Kit (SDK) 3.0

Cloud.tencent.com/document/sd… The system supports CVM, VPC, and CBS. All cloud service products will be added to the system in the future. The new SDK realizes the unification, and has the advantages of the same use method, the same interface call method, the same error code and the same return package format of each language VERSION SDK.

SDK for cloud functions

Cloud.tencent.com/document/ap…

Process thinking

process

1, enterserverless.cloud.tencent.com/start?c=scf

Create cloud functions using official Demo

Configuring cloud Functions

Select zip file compression to deploy which Node 12.16 creates cloud functions after you are done, you will get

2. Add a trigger

Select a new API service

3. Access the trigger address

Parameters for

{"secretId": "your secretId" "secretKey":" your secretKey" "bucket": "nod69424", "region": "aphou", "key": "zip-test0907.zip", "sourceList": [ {"url": "http://node-zip-1255469424.cos.ap-guangzhou.myqcloud.com/cos/WechatIMG56%E7%9A%84%E5%89%AF%E6%9C%AC%202.jpeg"} ] }Copy the code

An error is:

"errno": 403,
    "error": "requestId 74ededec75838b55856b0d204d545c3a , Invalid scf response format. please check your scf response format."
Copy the code

View the document cloud.tencent.com/document/pr…

However, in demo, objects are returned directly

So let’s add a method

const asReJson = (data,state = 200)=>{
  const promise = new Promise(function(resolve, reject) {
    resolve({
      "isBase64Encoded": false,
      "statusCode": state,
      "headers": {"Content-Type": "application/json"},
      "body": JSON.stringify(data)
    })
  })
  return promise
}

Copy the code

Calling this method

    return asReJson({
      code: 0,
      message: 'cos zip file success',
      data: {
        Location,
        Bucket,
        Key,
        ETag,
      },
    },200)
Copy the code

Can return to normal

{
    "code": 0,
    "message": "cos zip file success",
    "data": {
        "Location": "node-zip-.cos.ap-guangzhou.myqcloud.com/zip-test0907.zip",
        "Bucket": "node-zip-",
        "Key": "zip-test0907.zip",
        "ETag": "\"761b3110e232f1a2bf7a9eecda5ee6d4-3\""
    }
}

Copy the code

Figure (Figure response raw JSON format)

disadvantages

1, response time due to the cloud function has a response time limit of 1-900 seconds

So if it is large and large, it will fail to compress over a long period of time, which is better for short tasks