安装插件:
javascript
npm i ali-oss moment
点击按钮传递input中获取的doi,以当前时间为txt名,doi为txt内容,传递txt文件到指定oss地址:
javascript
import moment from "moment";
import OSS from "ali-oss";
const handleClick = async () => {
const accessKeyId = "your_id";
const accessKeySecret = "your_secret";
const client = new OSS({
region: "your_region", // 地域
accessKeyId,
accessKeySecret,
bucket: "your_bucket", // OSS Bucket 名称
});
// 获取当前时间作为文件名
const currentTime = moment().format("YY-MM-DD hh:mm:ss");
// 创建纯文本 Blob 对象
const contentBlob = new Blob([doi], { type: "text/plain" });
// 上传 txt 文件到 OSS 指定地址
client
.put(`xxx/xxx/${currentTime}.txt`, contentBlob)
.then(function (result) {
console.log("Upload Success:", result);
})
.catch(function (err) {
console.error("Upload Failed:", err);
});
};