vue 静态文件上传到七牛
1.七牛官网申请账号
具体申请流程不详细赘述了.最好申请一个自己的专属文件域名.
去阿里云配置CNAME
2.修改项目index.js
var path = require('path')
module.exports = {
build: {
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
// assetsPublicPath: '/',
assetsPublicPath: 'https://file.cfun.vip/file/',
//修改为自己的域名
productionSourceMap: true,
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: true,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
},
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
//assetsPublicPath: '/',
assetsPublicPath: 'https://file.cfun.vip/file/',
proxyTable: {},
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false
}
}
最后打包后可以看到所有静态文件的前缀都换了地址
3.创建脚本上传
在Vue项目根目录新建一个qiniu.js文件.
需要安装node环境运行
var fs = require('fs');
var path = require('path');
var qiniu = require("qiniu");
//自己七牛云的秘钥
var accessKey = 'XXXXX'
var secretKey = 'XXXXXX';
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
var config = new qiniu.conf.Config();
// 空间对应的机房,zone_z1代表华北,其他配置参见七牛云文档
config.zone = qiniu.zone.Zone_z0;
// 是否使用https域名
//config.useHttpsDomain = true;
// 上传是否使用cdn加速
//config.useCdnDomain = true;
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
main()
function main(){
displayFile('./dist')
}
//upload('static/css/app.qwer.css',"./dist/static/css/app.qwer.css")
function upload(key,localFile) {
//这里base-html是存储空间名
var Bucket = `cfun:${key}`;
var options = {
scope: Bucket,
MimeType:0,
};
var putPolicy = new qiniu.rs.PutPolicy(options);
var uploadToken = putPolicy.uploadToken(mac);
formUploader.putFile(uploadToken, key, localFile, putExtra, function (respErr,
respBody, respInfo) {
if (respErr) {
throw respErr;
}
if (respInfo.statusCode == 200) {
console.log(respBody);
} else {
console.log(respInfo.statusCode);
console.log(respBody);
if (respBody.error) {
console.log(respBody.error)
}
}
});
}
//遍历文件夹
function displayFile(param) {
//转换为绝对路径
//var param = path.resolve(param);
fs.stat(param, function (err, stats) {
//如果是目录的话,遍历目录下的文件信息
if (stats.isDirectory()) {
fs.readdir(param, function (err, file) {
file.forEach((e) => {
//遍历之后递归调用查看文件函数
//遍历目录得到的文件名称是不含路径的,需要将前面的绝对路径拼接
var absolutePath = path.join(param, e);
//var absolutePath = path.resolve(path.join(param, e));
displayFile(absolutePath)
})
})
} else {
//file/这里是空间里的文件前缀
var key ='file/' +param.split('dist/')[1];
var localFile = './' + param;
//console.log(key,localFile);
upload(key,localFile)
}
})
}
4.命令.即可上传文件
node qiniu.js
之后可以在你的对应域名的空间下看到对应的惊天文件
5.打开网站
发现运行速度快了很多,
注意:
node环境
七牛的对应安装
七牛https是要付费
赶紧动手做起来吧,有什么问题大家再一起探讨
解决的痛点
虽然是个人网站博客,但是你会发现首次进来打包生成的css文件虽然就几百K,但是加载依旧很耗时(服务器腾讯云最低配),所以就把静态文件都放到七牛上加速.如果哪位小伙伴有更好的解决办法可以私聊.
本作品采用《CC 协议》,转载必须注明作者和本文链接
我在用 VuePress 做个人博客,纯静态托管 Github,图片使用的是 Github 的 CDN,速度很快且免费:My blog
不过github的静态资源文件托管应该没有国内的七牛快吧,毕竟国内很多大公司也都是用七牛的.很少听说github托管的
@代码的坏味道
Github 的 cdn 是 jsdelivr,全球优势不用说,关键是免费而且无限流量,毕竟个人博客没收入,能省钱就省钱。
刚学的 vue,代码写的太烂了 :see_no_evil:
大神,请教下,那个上传的代码我这跑不起来啊