网站建设哪些公司好,恩施市住房和城乡建设局网站,做gif网站,手游代理联运平台1、在项目根目录创建uploadToCOS.js文件 #xff08;建议起简单的名字 方便以后上传输入命令方便#xff09; 2、uploadToCOS.js文件代码编写
const path require(path);
const fs require(fs);
const COS require(cos-nodejs-sdk-v5);// 配置腾讯云COS参数
const cos n…1、在项目根目录创建uploadToCOS.js文件 建议起简单的名字 方便以后上传输入命令方便 2、uploadToCOS.js文件代码编写
const path require(path);
const fs require(fs);
const COS require(cos-nodejs-sdk-v5);// 配置腾讯云COS参数
const cos new COS({SecretId: , // 身份识别 IDSecretKey: , // 身份密钥
});// 获取dist目录下的所有文件
const dirPath path.resolve(__dirname, dist);// 遍历目录并上传文件
function traverseDirectory(dirPath, prefix ) {const files fs.readdirSync(dirPath);files.forEach((file) {const filePath path.join(dirPath, file);const relativePath path.relative(dirPath, filePath);const cosKey path.join(prefix, relativePath).replace(/\\/g, /); // 使用 / 替换 \确保在 COS 上是正斜杠if (fs.statSync(filePath).isDirectory()) {// 如果是目录则继续遍历子目录并传入新的前缀traverseDirectory(filePath, cosKey);} else {// 如果是文件则上传文件fs.readFile(filePath, (err, data) {if (err) {console.error(\n读取文件 ${relativePath} 失败, err);return;}const params {Bucket: ,Region: ,Key: cosKey,Body: data, // 使用文件内容进行上传};cos.putObject(params, function (err, data) {if (err) {console.log(data);console.error(\n上传文件 ${relativePath} 失败, err);} else {console.log(data);console.log(\n上传文件 ${relativePath} 成功);}});});}});
}// 开始遍历上传
traverseDirectory(dirPath);填入你自己的 SecretId、SecretKey、Bucket、Region 3、在控制台输入
node uploadToCOS.js
直到所有文件传输完成 记得在此之前开放存储桶跨域访问权限
这个可以看我另一篇文章
(151条消息) 上传图片到腾讯云对象存储桶cos 【腾讯云对象存储桶】【cos】【el-upload】【vue3】【上传头像】【删除】_白开水为啥没味的博客-CSDN博客