31 lines
689 B
TypeScript
31 lines
689 B
TypeScript
|
import OSS from "ali-oss";
|
||
|
import { v4 as uuid } from "uuid";
|
||
|
import { mRequest } from "./request";
|
||
|
|
||
|
const ossBase = "https://aihlp.com.cn";
|
||
|
|
||
|
|
||
|
export default async function uploadOss(file: File, root: string) {
|
||
|
const path = `/admin/${root}/${uuid()}_${file.name}`;
|
||
|
const res = await mRequest<{
|
||
|
accessKeyId: string;
|
||
|
accessKeySecret: string;
|
||
|
region: string;
|
||
|
bucket: string;
|
||
|
}>('GET', '/api/uploadKey')
|
||
|
const client = new OSS({
|
||
|
...res
|
||
|
})
|
||
|
return client
|
||
|
.put(path, file, {
|
||
|
mime: file.type,
|
||
|
headers: {
|
||
|
"Content-Type": file.type,
|
||
|
},
|
||
|
})
|
||
|
.then((res) => {
|
||
|
console.log(res);
|
||
|
return ossBase + "/" + res.name;
|
||
|
});
|
||
|
}
|