腾讯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
相关推荐
夜郎king11 分钟前
HTML5 SVG 实现日出日落动画与实时天气可视化
前端·html5·svg 日出日落
跳动的梦想家h1 小时前
环境配置 + AI 提效双管齐下
java·vue.js·spring
夏幻灵1 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_1 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝1 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions2 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发2 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_2 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞052 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、2 小时前
Websocket能携带token过去后端吗
前端·后端·websocket