Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Cloud Storage: Upload, Download, CDN Address, Delete Files


May 27, 2021 A minimalist getting started manual for cloud development serverless


Table of contents


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.
Cloud Development Cloud Storage: Upload, Download, CDN Address, Delete Files

The role or capabilities of cloud storage are:

  • You can set access rights, such as public pictures or private photo albums, you can click on the permission settings to set;
  • Image beds that can be uploaded as images, such as storage icons, images, svgs...
  • Can be used as some material or material warehouse, such as storage pdf, word, excel...
  • Cloud storage for cloud development is CDN-accelerated by default;

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.

Upload the file

We can use the Cloud development node .js SDK to upload images in cloud functions.

  1. 'use strict';
  2. const tcb = require("tcb-admin-node")
  3. const fs = require("fs")
  4. const app = tcb.init({
  5. env: '你的环境 ID'
  6. })
  7. exports.main = async (event, context) => {
  8. // uploadFile 上传文件
  9. let data = await app.uploadFile({
  10. cloudPath: "/test-00001.jpg",
  11. // fileContent: buffer 或要上传的文件可读流
  12. // 后续会将更简单的方法,在前端直接通过 JS-SDK 上传
  13. fileContent: fs.createReadStream('./test.png')
  14. })
  15. return {
  16. status: 1,
  17. file_id: res.fileID
  18. }
  19. }

Download the file

With file upload, there is a file download action.

  1. 'use strict';
  2. const tcb = require("tcb-admin-node")
  3. const fs = require("fs")
  4. const app = tcb.init({
  5. env: '你的环境 ID'
  6. })
  7. exports.main = async (event, context) => {
  8. // downloadFile 下载图片
  9. let res = await app.downloadFile({
  10. fileID: '云存储中文件的 fileID',
  11. })
  12. // fileContent 类型为 Buffer
  13. return res.fileContent
  14. }

Gets the CDN access path for the file

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.

  1. const tcb = require('tcb-admin-node')
  2. const app = tcb.init({
  3. env: '你的环境 ID'
  4. })
  5. exports.main = async (event, context) => {
  6. // getTempFileURL 获取 CDN 访问地址
  7. let res = await app.getTempFileURL({
  8. // fileID 数组
  9. fileList: ['fileID-1', 'fileID-2']
  10. })
  11. res.fileList.forEach(item => {
  12. // 打印文件访问链接
  13. console.log(item.tempFileURL)
  14. })
  15. }

Delete the file

Deleting files is also easier.

  1. const tcb = require('tcb-admin-node')
  2. const app = tcb.init({
  3. env: '你的环境 ID'
  4. })
  5. exports.main = async (event, context) => {
  6. // deleteFile 删除文件
  7. let res = await app.deleteFile({
  8. fileList: [
  9. 'cloud://a/b/c',
  10. 'cloud://d/e/f'
  11. ]
  12. })
  13. return res.fileList
  14. }