腾讯design vue项目 上传桶 腾讯云的桶 对象存储 打包web端项目上传dist

1.说明

将腾讯design 项目上传到 腾讯云的对象存储中 ,但是发现 再这个腾讯design项目中 直接npm run build 打包以后 上传 发现 不能用 需要配置东西

2.解决

使用腾讯云的cos-nodejs-sdk-v5 插件 代码上传

cos-nodejs-sdk-v5 - npm

复制代码
npm i cos-nodejs-sdk-v5 --save

示例:

复制代码
// 引入模块
var COS = require('cos-nodejs-sdk-v5');
// 创建实例
var cos = new COS({
  SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
});

// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
var Bucket = 'test-1250000000';
// 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
// 关于地域的详情见 https://cloud.tencent.com/document/product/436/6224
var Region = 'ap-guangzhou';

// 高级上传
cos.uploadFile(
  {
    Bucket: Bucket,
    Region: Region,
    Key: '1.zip',
    FilePath: './1.zip', // 本地文件地址,需自行替换
    SliceSize: 1024 * 1024 * 5, // 触发分块上传的阈值,超过5MB使用分块上传,非必须
  },
  function (err, data) {
    console.log(err, data);
  }
);

3.真实解决

①.根目录新建uploadToCOS.js 文件

②. 代码

复制代码
const path = require('path');
const fs = require('fs');
const COS = require('cos-nodejs-sdk-v5');

// 配置腾讯云COS参数
const cos = new COS({
    SecretId: "xxxxxxxxxxxx", // 身份识别 ID
    SecretKey: "xxxxxxxxxx", // 身份密钥
});

// 获取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: 'xxxxxxxxxxx',
                    Region: 'x'x'x'xxxxx',
                    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);

③.控制台执行代码

复制代码
node uploadToCOS.js
相关推荐
李少兄9 分钟前
简单讲讲 SVG:前端开发中的矢量图形
前端·svg
前端小万10 分钟前
告别 CJS 库加载兼容坑
前端·前端工程化
恋猫de小郭10 分钟前
Flutter 3.38.1 之后,因为某些框架低级错误导致提交 Store 被拒
android·前端·flutter
JarvanMo14 分钟前
Flutter 需要 Hooks 吗?
前端
光影少年24 分钟前
前端如何虚拟列表优化?
前端·react native·react.js
Moment26 分钟前
一杯茶时间带你基于 Yjs 和 reactflow 构建协同流程图编辑器 😍😍😍
前端·后端·面试
invicinble43 分钟前
对于前端数据的生命周期的认识
前端
PieroPc1 小时前
用FastAPI 后端 和 HTML/CSS/JavaScript 前端写一个博客系统 例
前端·html·fastapi
hunter14501 小时前
2026.1.4 html简单制作
java·前端·笔记·html
鹏程十八少1 小时前
Android 深入剖析Android内存泄漏:ViewPager2与Fragment的生命周期陷阱
android·前端·app