hexo image 用阿里云存储

Posted by Roy on 2017-03-26
技术
203字预计1分

第一步

安装 hexo-admin,co,ali-oss

1
2
3
4
cd 项目目录
npm install hexo-admin
npm install co
npm install ali-oss

第二步

open /node_modules/hexo-admin/api.js 在头部加入代码

1
2
var co = require('co')
var oss = require('ali-oss')

在 module.exports 内初始化阿里云

1
2
3
4
5
6
7
8
9
module.exports = function (app, hexo) {
//初始化阿里云
var client = new oss({
region: '你的region',
accessKeyId: '你的accessKeyId',
accessKeySecret: '你的accessKeySecret'
});
}

在 400 行代码处找到如下代码 注释掉或者删除

1
2
3
4
5
6
7
8
9
10
11
12
13
hexo.log.d(`saving image to ${outpath}`)
fs.writeFile(outpath, buf, function (err) {
if (err) {
console.log(err)
}
hexo.source.process().then(function () {
res.done({
src: path.join(hexo.config.root + filename),
msg: msg
})
});
})

在400行处添加代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var objectKey = "hexoResource/" + Date.now() + filename
co(function* () {
client.useBucket('server-19860911');
var result = yield client.put(objectKey, buf);
// console.log("|||||||||||||======="+result);
var returnFileName = "https://server-19860911.oss-cn-beijing.aliyuncs.com/" + objectKey
hexo.source.process().then(function () {
res.done({
src: returnFileName,
msg: msg
})
});
}).catch(function (err) {
console.log("|||||||||||||"+err);
});


Kommentare: