云函数列举COS文件
COS的操作文档以及API请看网页:https://cloud.tencent.com/document/product/436/64982 从云函数例子中选择这个模板创建工程: 创建步骤这里不细说,但是有一点很关键,就是云函数和cos的域必须一样,也就是所在地要一样: 这里主要是因为本例程使用到了触发器的缘故,如果没有触发器,那就不需要同区域了! 云函数代码修改如下:
const COS = require('cos-nodejs-sdk-v5')
const fs = require('fs')
const APPID = '13XXXX8232'
const SECRET_ID = 'AKIXXXXn7AxWt0sSlnZ4IHzRcW9WcRzHH0Sd'
const SECRET_KEY = 'wbz6XXXXc7LIyOXP0KNAgvta091cMp2y'
const REGION = 'ap-guangzhou'
const cosInst = new COS({
SecretId: SECRET_ID,
SecretKey: SECRET_KEY
})
cosInst.getObjectPromise = function(params) {
return new Promise((resolve, reject) => {
cosInst.getObject(params, function(err, data) {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
cosInst.getBuckets = function(params) {
return new Promise((resolve, reject) => {
cosInst.getBucket(params, function(err, data) {
if (err) {
reject(err)
} else {
resolve(data)
}
})
})
}
exports.main_handler = async (event, context, callback) => {
console.log('Start main handler')
let promiseArr = []
promiseArr.push(
cosInst
.getBuckets({
Bucket: 'syXXXXXXXXvice-13XXXXXXXX2',
Region: 'ap-guangzhou',
})
.then(res => {
console.log('getBuckets success1: ', res)
})
.catch(e => {
throw (
`Error getting object from bucket ${bucket}. Error message: ${JSON.stringify(
e
)}`
)
})
)
try {
await Promise.all(promiseArr)
return 'Success'
} catch(e) {
console.log(e)
return 'Fail'
}
这时候如果在COS上传一个图片,可以看到云函数这里有如下日志: START RequestId: e1651c55-2d82-4d3b-9c49-9eb744dec6eeInit Report RequestId: e1651c55-2d82-4d3b-9c49-9eb744dec6ee Coldstart: 440ms (PullCode: 93ms InitRuntime: 6ms InitFunction: 341ms) Memory: 128MB MemUsage: 28.12MB2022-03-11T09:26:31.867Z e1651c55-2d82-4d3b-9c49-9eb744dec6ee Start main handler 2022-03-11T09:26:31.942Z e1651c55-2d82-4d3b-9c49-9eb744dec6ee getBuckets success1: { Name: ‘XXXXXXXX’, Prefix: ‘’, Marker: ‘’, MaxKeys: ‘1000’, IsTruncated: ‘false’, Contents: [ { Key: ‘bom - 副本 (2).png’, LastModified: ‘2022-03-11T08:42:28.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (3).png’, LastModified: ‘2022-03-11T08:44:29.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (4).png’, LastModified: ‘2022-03-11T08:46:03.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (5).png’, LastModified: ‘2022-03-11T08:53:04.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (6).png’, LastModified: ‘2022-03-11T09:01:17.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (7).png’, LastModified: ‘2022-03-11T09:03:08.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (8).png’, LastModified: ‘2022-03-11T09:05:11.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (9).png’, LastModified: ‘2022-03-11T09:26:30.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom.png’, LastModified: ‘2022-03-11T08:35:03.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘子板BOM确定清单.png’, LastModified: ‘2022-03-11T08:40:34.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ } ], CommonPrefixes: [], statusCode: 200, headers: { ‘content-type’: ‘application/xml’, ‘content-length’: ‘3423’, connection: ‘close’, date: ‘Fri, 11 Mar 2022 09:26:31 GMT’, server: ‘tencent-cos’, ‘x-cos-bucket-region’: ‘ap-guangzhou’, ‘x-cos-request-id’: ‘NjIyYjE1YzdfYzcyZjJjMGJfNGYxYV9mNzdkNGEy’ } } Response RequestId:e1651c55-2d82-4d3b-9c49-9eb744dec6ee RetMsg:“Success” END RequestId:e1651c55-2d82-4d3b-9c49-9eb744dec6ee Report RequestId:e1651c55-2d82-4d3b-9c49-9eb744dec6ee Duration:80ms Memory:128MB MemUsage:30.275459MB 其实经过上面代码的修改,并不需要触发器就可以直接测试运行代码了,测试运行代码打印的log如下:
Invoke function GetCOSObject-1646965787 [RequestId:b269d9b3-b886-47d9-a01b-6e14a0910557] Response: “Success” Output: START RequestId: b269d9b3-b886-47d9-a01b-6e14a0910557 Event RequestId: b269d9b3-b886-47d9-a01b-6e14a0910557 2022-03-11T09:37:45.822Z b269d9b3-b886-47d9-a01b-6e14a0910557 Start main handler 2022-03-11T09:37:45.896Z b269d9b3-b886-47d9-a01b-6e14a0910557 getBuckets success1: { Name: ‘XXXXXXXX’, Prefix: ‘’, Marker: ‘’, MaxKeys: ‘1000’, IsTruncated: ‘false’, Contents: [ { Key: ‘bom - 副本 (2).png’, LastModified: ‘2022-03-11T08:42:28.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (3).png’, LastModified: ‘2022-03-11T08:44:29.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (4).png’, LastModified: ‘2022-03-11T08:46:03.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (5).png’, LastModified: ‘2022-03-11T08:53:04.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (6).png’, LastModified: ‘2022-03-11T09:01:17.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (7).png’, LastModified: ‘2022-03-11T09:03:08.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (8).png’, LastModified: ‘2022-03-11T09:05:11.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom - 副本 (9).png’, LastModified: ‘2022-03-11T09:26:30.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘bom.png’, LastModified: ‘2022-03-11T08:35:03.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ }, { Key: ‘子板BOM确定清单.png’, LastModified: ‘2022-03-11T08:40:34.000Z’, ETag: ‘“8d84c71801e6fb4b6dba4de0759f5bce”’, Size: ‘4101709’, Owner: [Object], StorageClass: ‘STANDARD’ } ], CommonPrefixes: [], statusCode: 200, headers: { ‘content-type’: ‘application/xml’, ‘content-length’: ‘3423’, connection: ‘close’, date: ‘Fri, 11 Mar 2022 09:37:45 GMT’, server: ‘tencent-cos’, ‘x-cos-bucket-region’: ‘ap-guangzhou’, ‘x-cos-request-id’: ‘NjIyYjE4NjlfZDcyZjI3MGJfZjUzYl9kYjlhZWEz’ } }
END RequestId: b269d9b3-b886-47d9-a01b-6e14a0910557 Report RequestId: b269d9b3-b886-47d9-a01b-6e14a0910557 Duration:79ms Memory:128MB MemUsage:30.8274MB Summary: Request ID: b269d9b3-b886-47d9-a01b-6e14a0910557 Run Duration: 79 Bill Duration: 79 Usage Memory: 30.827438354492188 Invoke Result: 0 效果和触发器触发的一样!
|