ai-bot/app/_lib/upload.ts

35 lines
868 B
TypeScript

import OSS from "ali-oss";
import { v4 as uuid } from "uuid";
import { mRequest } from "./request";
const ossBase = "https://aihlp.com.cn";
const accessKeyId = process.env.ALIYUN_RAM_ACCESS_KEY_ID || ''
const accessKeySecret = process.env.ALIYUN_RAM_ACCESS_KEY_SECRET || ''
console.log('id');
console.log(accessKeyId);
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;
});
}