30 lines
683 B
TypeScript
30 lines
683 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: { name: string }) => {
|
|
return ossBase + "/" + res.name;
|
|
});
|
|
}
|