一、脚本部署

安装依赖 : npm i ali-oss

新建该脚本(oss.js)放在你hexo项目的根目录下,和public同级

该脚本有四个参数需要自己的配置(在你的oss控制台查):access keyaccess secretregionBucket

const fs = require('fs')
const path = require('path')
const util = require('util')
const OSS = require('ali-oss')

const promisifyReaddir = util.promisify(fs.readdir)
const promisifyStat = util.promisify(fs.stat)

// 阿里 OSS access key 拥有对 OSS 的全部权限
const ALIOSSKEY = {
    key: '*****************',
    secret: '*****************'
}

const client = new OSS({
    // 请填写你的 Bucket 对应的 region
    region: 'oss-cn-shanghai',
    accessKeyId: ALIOSSKEY.key,
    accessKeySecret: ALIOSSKEY.secret,
    // 请填写对应的 Bucket 名字
    bucket: 'MewBucket'
})

const publicPath = path.resolve(__dirname, './public')



async function run(proPath = '') {
    const dir = await promisifyReaddir(`${publicPath}${proPath}`)

    for (let i = 0; i < dir.length; i++) {
        const stat = await promisifyStat(path.resolve(`${publicPath}${proPath}`, dir[i]))

        if (stat.isFile()) {
            const fileStream = fs.createReadStream(path.resolve(`${publicPath}${proPath}`, dir[i]))
            console.log(`上传文件: ${proPath}/${dir[i]}`)
            const result = await client.putStream(`${proPath}/${dir[i]}`, fileStream)
            console.log(result)
        } else if (stat.isDirectory()) {
            await run(`${proPath}/${dir[i]}`)
        }
    }
}

run()

然后直接运行 node oss 就能直接把public下所有文件上传到你的bucket

上一篇 下一篇