Skip to content

使用阿里云OSS对象存储


下面是一个图片上传的演示案例,通过 ctx 获取到图片,以数据流的方式上传到云,返回图片的访问地址。

import fs from 'fs'
import { Context, Controller, ShowSuccess, Utils } from 'think-ts-lib'

export default class HelloController extends Controller {

  // 上传图片到OSS
  async putStream(ctx: Context) {
    // 读取图片成流
    // @ts-ignore
    const reader = fs.createReadStream(ctx.request.files.file.filepath)
    // @ts-ignore
    const fileName = ctx.request.files.file.originalFilename
    const result = Utils.OSS().putStream(fileName, reader)
    return ShowSuccess(result)
  }

}
import fs from 'fs'
import { Context, Controller, ShowSuccess, Utils } from 'think-ts-lib'

export default class HelloController extends Controller {

  // 上传图片到OSS
  async putStream(ctx: Context) {
    // 读取图片成流
    // @ts-ignore
    const reader = fs.createReadStream(ctx.request.files.file.filepath)
    // @ts-ignore
    const fileName = ctx.request.files.file.originalFilename
    const result = Utils.OSS().putStream(fileName, reader)
    return ShowSuccess(result)
  }

}

阿里OSS,NodeJS版文档

https://help.aliyun.com/document_detail/32068.html

公共云下OSS Region和Endpoint对照表

https://help.aliyun.com/document_detail/31837.html?spm=5176.21213303.J_6704733920.10.3f0553c9VGjrT3&scm=20140722.S_help%40%40文档%40%4031837.S_0%2Bos.ID_31837-RL_ossDAScnDASzhangjiakou-LOC_main-OR_ser-V_2-P0_1

备案号:冀ICP备20015584号-2