jackieli123723 / jackieli123723.github.io

✅lilidong 个人博客
9 stars 0 forks source link

前端部署代码上传阿里云oss-cdn部署node实现方式 #53

Open jackieli123723 opened 6 years ago

jackieli123723 commented 6 years ago

node上传代码

//index.js
const fs = require('fs');
const co = require('co');
const path = require('path');
const oss = require('ali-oss');

//构建oss对象
const store = oss({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  bucket: 'bucket',
  region: 'oss-cn-shenzhen',
});

(() => {
  const root = path.resolve(__dirname, './dist');
  const files = [];
  //递归取出所有文件夹下所有文件的路径
  function readDirSync(p) {
    const pa = fs.readdirSync(p);
    pa.forEach((e) => {
      const cur_path = `${p}/${e}`;
      const info = fs.statSync(cur_path);
      if (info.isDirectory()) {
        readDirSync(cur_path);
      } else {
        files.push(cur_path);
      }
    });
  }
  readDirSync(root);

  co(function* () {
    //遍历文件
    for (let index = 0; index < files.length; index += 1) {
      const e = files[index];
     //const result = yield store.put(e.replace(root, ''), e); //放在根目录
    //下面是自定义文件路径
      const result = yield store.multipartUpload('test' + e.replace(root, ''), e, {
        progress: function* (p) {
          console.log('Progress: ' + p);
        }
      });
      //提交文件到oss,这里要注意,阿里云不需要创建新文件夹,只有有路径,没有文件夹会自动创建
      console.log(result);
    }
  });
})();

上传

node index.js