antd upload 文件上传到oss

5,419 阅读1分钟
import OSS from 'ali-oss';
import moment from 'moment';
import $uuid from 'uuid/v1';

loadClient = async () => {
  return new OSS({
    region: '<Your region>',
    stsToken: '<Your securityToken>',
    accessKeyId: '<Your AccessKeyId>',
    accessKeySecret: '<Your AccessKeySecret>',
    bucket: '<Your bucket name>'
  });
};

putBlob = async ({ file }) => {
  const client = await this.loadClient();
  try {
    //object-name可以自定义为文件名(例如file.txt)或指定目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
    //client.put('object-name', 'local-file')
    // key要唯一 加uuid和时间戳毫秒级
    const key=$uuid()+moment.unix(moment()) / 1000+'.xlsx'
    let result = await client.put(key, file);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
};

<Upload accept=".xlsx"
    showUploadList={false}
    name="file"
    customRequest={this.putBlob}>
  <Button>
    上传文件
  </Button>
</Upload>

来源链接:help.aliyun.com/document_de…