看了几天七牛云nodejs的SDK文档,如今尝试写一个上传文件到七牛云的demo,不足之处请各位大佬指正!
模拟先后端分离,后端向前端发送七牛云的上传凭证token,前端得到凭证后将图片上传到七牛云,并得到返回的图片url地址
既然要上传文件到七牛云,那咱们至少须要一个七牛云空间啊html
注册一个七牛云帐号,而后绑定支付宝认证一下就OK了
完成以后先建立一个对象储存空间,名字随意
重点来了,这里有几项是要用到的
一、 空间名称bucket, 我这里就是 lytton
二、 SK 和 AK ,在控制面板的密匙管理
三、 储存空间的外链域名,在储存空间能够找到
AK 和 SK前端
外链域名vue
既然选择使用vue,那就直接用vue-cli建立项目了
vue init webpack qiniuupload
├── build
├── config
├── index.html
├── node_modules
├── package.json
├── README.md
├── src
└── staticnode
由于要用到后端,因此在src 目录里面建立一个server来当后端,一个client来当前端用
lytton@lytton-ubuntu:~/桌面/demo/qiniuupload/src$ tree -L 1
.
├── App.vue
├── client
├── main.js
├── router
└── serverwebpack
而后在server目录下ios
npm init npm i express qiniu --save
固然这个server的做用很简单,就只作一件事,当前端请求要上传图片到七牛云的时候,像前端发送一个上传凭证的token
建立后端程序app.jsgit
// 引入包 const express = require('express') const bodyparse = require('body-parser') // 建立服务 const app = express() // 解析数据 app.use(bodyparse.json()) // 引入七牛云配置 const qnconfig = require('./config.js') // 处理请求 app.get('/token', (req, res, next) => { // console.log(qnconfig.uploadToken) res.status(200).send(qnconfig.uploadToken) }) // 监听3000端口 app.listen(3000, () => { console.log('this server are running on localhost:3000!') })
建立config.js以用来生成上传凭证github
这里就须要用到上面的bucket ,AK,SK
/* 七牛云配置 */ const qiniu = require('qiniu') // 建立上传凭证 const accessKey = 'YOXpF0XvM_3yVDsz5C-hWwrFE5rtDAUQC3XjBQEG' const secretKey = 'CmrhUV2xHf1d8nPCsws9wwm7jKypCPA0lRVm-7lS' const mac = new qiniu.auth.digest.Mac(accessKey, secretKey) const options = { scope: 'lytton', expires: 7200 } const putPolicy = new qiniu.rs.PutPolicy(options) const uploadToken = putPolicy.uploadToken(mac) module.exports = { uploadToken }
server的目录结构web
lytton@lytton-ubuntu:~/桌面/demo/qiniuupload/src/server$ tree -L 1
.
├── app.js
├── config.js
├── node_modules
└── package.jsonvue-router
而后控制台 nodemon app.js,固然若是没装nodemon的话,就 node app.js 同样的
打开浏览器 localhost:3000
而后服务端就不用管了
首先在根目录安装element-ui axios
而后在main.js里面引入
而后在client文件夹下面建立一个upload.vue
并在router里面引入
import Vue from 'vue' import Router from 'vue-router' import UpLoad from '@/client/upload' Vue.use(Router) export default new Router({ routes: [ { path: '/', component: UpLoad } ] })
到这里总体结构也就完成了,src 的目录结构
lytton@lytton-ubuntu:~/桌面/demo/qiniuupload/src$ tree -L 2 . ├── App.vue ├── client │ └── upload.vue ├── main.js ├── router │ └── index.js └── server ├── app.js ├── config.js ├── node_modules └── package.json
这里前端向后端访问属于跨域访问,前端是跑在8080端口的,后端是跑在3000端口的,因此首先要解决一下跨域问题
打开config文件夹下的index.js
添加跨域代理访问
module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/up': { target: 'http://localhost:3000', changeOrigin: true, pathRewrite: { '^/up': '/' } } },
代码比较简单,基本从element-ui 官网拷贝过来就能够用了
<template> <!-- upload --> <div class="upload"> <el-upload class="avatar-uploader" :action= domain :http-request = upqiniu :show-file-list="false" :before-upload="beforeUpload"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script> export default { data () { return { imageUrl: '', token: {}, // 七牛云的上传地址,根据本身所在地区选择,我这里是华南区 domain: 'https://upload-z2.qiniup.com', // 这是七牛云空间的外链默认域名 qiniuaddr: 'p3z6q1uw1.bkt.clouddn.com' } }, methods: { // 上传文件到七牛云 upqiniu (req) { console.log(req) const config = { headers: {'Content-Type': 'multipart/form-data'} } let filetype = '' if (req.file.type === 'image/png') { filetype = 'png' } else { filetype = 'jpg' } // 重命名要上传的文件 const keyname = 'lytton' + new Date() + Math.floor(Math.random() * 100) + '.' + filetype // 从后端获取上传凭证token this.axios.get('/up/token').then(res => { console.log(res) const formdata = new FormData() formdata.append('file', req.file) formdata.append('token', res.data) formdata.append('key', keyname) // 获取到凭证以后再将文件上传到七牛云空间 this.axios.post(this.domain, formdata, config).then(res => { this.imageUrl = 'http://' + this.qiniuaddr + '/' + res.data.key // console.log(this.imageUrl) }) }) }, // 验证文件合法性 beforeUpload (file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG) { this.$message.error('上传头像图片只能是 JPG 格式!') } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!') } return isJPG && isLt2M } } } </script> <style scoped> .upload { width: 600px; margin: 0 auto; } .avatar-uploader .el-upload { border: 5px dashed #ca1717 !important; border-radius: 6px; cursor: pointer; position: relative; overflow: hidden; } .avatar-uploader .el-upload:hover { border-color: #409EFF; } .avatar-uploader-icon { font-size: 28px; color: #8c939d; width: 178px; height: 178px; line-height: 178px; text-align: center; } .avatar { width: 178px; height: 178px; display: block; } </style>
首先打开localhost:8080
而后选择上传图片
成功返回图片了
看一下七牛云空间
多了一张刚刚上传的且已经重命名的图片
只作了文件的上传操做,若是是要修改,下载的话,须要写更多了,凭证也不能写得这么简单,学习仍是要多看文档和demo
github: https://github.com/lyttonlee/...