import OSS from "ali-oss"
import { ElMessage } from "element-plus";

class UploadFile {
  constructor() {
    this.bucket = 'birdseye';
    this.region = 'oss-cn-guangzhou'
    this.credentials = null;
    this.ossClient = null
    this.getCredential()
  }

  async put(fileName, file){
    if(!this.credentials){
      alert("初始化失败！")
      return;
    }
    let result = await this.ossClient.put(fileName, file)
    return result.name;
  }
  //获取授权
  getCredential() {
    let that = this;
    return VE_API.ali.credential()
        .then( ({data})=> {
          that.credentials = data;
          that.initOSSClient()
        })
  }
  //初始化oss对象
  initOSSClient() {
    let that = this;
    const { accessKeyId, accessKeySecret, securityToken } = this.credentials;
    this.ossClient = new OSS({
      accessKeyId: accessKeyId,
      accessKeySecret: accessKeySecret,
      stsToken: securityToken,
      bucket: that.bucket,
      region: that.region
    });
  }
}


export default UploadFile;
