May 27, 2021 A minimalist getting started manual for cloud development serverless
The previous sections shared cloud functions, databases, and basically a simple application development. B ut an app should still have pictures, videos, etc., so where does that data store? T hat's cloud storage. C loud storage is called differently for each cloud vendor, some called OSS and some called COS.
Let's open the
Tencent Cloud Development Console,
pictured below is the cloud storage interface for cloud development.
The role or capabilities of cloud storage are:
The following links are based on cloud functions, if you do not understand the cloud functions, you can read to write the first cloud function.
We can use the Cloud development node .js SDK to upload images in cloud functions.
'use strict';
const tcb = require("tcb-admin-node")
const fs = require("fs")
const app = tcb.init({
env: '你的环境 ID'
})
exports.main = async (event, context) => {
// uploadFile 上传文件
let data = await app.uploadFile({
cloudPath: "/test-00001.jpg",
// fileContent: buffer 或要上传的文件可读流
// 后续会将更简单的方法,在前端直接通过 JS-SDK 上传
fileContent: fs.createReadStream('./test.png')
})
return {
status: 1,
file_id: res.fileID
}
}
With file upload, there is a file download action.
'use strict';
const tcb = require("tcb-admin-node")
const fs = require("fs")
const app = tcb.init({
env: '你的环境 ID'
})
exports.main = async (event, context) => {
// downloadFile 下载图片
let res = await app.downloadFile({
fileID: '云存储中文件的 fileID',
})
// fileContent 类型为 Buffer
return res.fileContent
}
In general, manually uploaded files can be copied to the CDN's access link. B ut if the code is uploaded in bulk, we can write program acquisitions, such as getting the user's avatar.
const tcb = require('tcb-admin-node')
const app = tcb.init({
env: '你的环境 ID'
})
exports.main = async (event, context) => {
// getTempFileURL 获取 CDN 访问地址
let res = await app.getTempFileURL({
// fileID 数组
fileList: ['fileID-1', 'fileID-2']
})
res.fileList.forEach(item => {
// 打印文件访问链接
console.log(item.tempFileURL)
})
}
Deleting files is also easier.
const tcb = require('tcb-admin-node')
const app = tcb.init({
env: '你的环境 ID'
})
exports.main = async (event, context) => {
// deleteFile 删除文件
let res = await app.deleteFile({
fileList: [
'cloud://a/b/c',
'cloud://d/e/f'
]
})
return res.fileList
}