vue3将打包后的文件上传到oss脚本编写

安装依赖

npm i ali-oss -D

创建node upload_oss.js

const OSS = require('ali-oss')
const fs = require('fs')
const path = require('path')
const os = require('os')
const DIST_PATH = path.join(__dirname, '/dist')

const client = new OSS({
  accessKeyId: 'LTAI5tC6kWm346RJ8u1RJnqc', // oss右上角: AccessKey管理
  accessKeySecret: 'B6eZA5c72NwpDhi5MQjf3HpmVW0WoA', // 此帐号只有upload-oss-vue的权限
  bucket: 'upload-oss-vue',
  region: 'oss-cn-guangzhou'
})


// 删除目录
const deleteDir = async dir =>  {
  const result = await client.list({
    'max-keys': 1000 // 最大查询1k条
  })

  if( result.objects.length === 0) return true

  await client.deleteMulti(result.objects.map(obj => obj.name), {
    quiet: true
  })
  console.log('删除完成')
  return true
}


const uploadDir = (dir,oss_prefix = '/') => {
  const docs = fs.readdirSync(dir)
  docs.forEach(doc => {
     const curr_src = dir + '/' +doc
     const fs_src = fs.statSync(curr_src)
     if( fs_src.isDirectory()){ // 文件夹 递归
      uploadDir(curr_src,oss_prefix + doc + '/')
      return
     }
     client.put( oss_prefix + doc,curr_src)
  })

}


(async () => {
  const del_res = await deleteDir(DIST_PATH)
  if( del_res !== true) throw new Error('删除失败')
  uploadDir(DIST_PATH)
  console.log('上传完成')
})()

node

vue3将打包后的文件上传到oss脚本编写

确保你有dist 目录。

vue3将打包后的文件上传到oss脚本编写

优化一键上传

编辑package.json,添加prod 命令。

   "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview --port 4173",
    "prod":"vite build && node upload_oss"
  }

npm run prod

vue3将打包后的文件上传到oss脚本编写

本作品采用《CC 协议》,转载必须注明作者和本文链接
专心学习不瞎搞
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!