import OSS from "ali-oss"; import Parse from "parse"; export class ProvierOssAli{ client:OSS|undefined async initClient(){ if(this.client) return // 获取STS临时口令 let result:any = await Parse.Cloud.run("aliOssSTS"); this.client = new OSS({ // yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。 region: "oss-cn-beijing", // 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。 accessKeyId: result?.AccessKeyId, accessKeySecret: result?.AccessKeySecret, // 从STS服务获取的安全令牌(SecurityToken)。 stsToken: result?.SecurityToken, // 填写Bucket名称。 bucket: result?.ALI_OSS_BUCKET || "hep-textbook", }); } constructor(){ } async upload(file:File,onProcess:Function){ await this.initClient(); let now = new Date(); let fname = `${Parse.User.current()?.id}/${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}/${now.getTime()}-${file?.name}`; const r1 = await this.client?.put(fname, file); console.log(file) console.log('put success: %j', r1); return r1 } }