欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

又拍云 Node.js 实现文件上传、删除

程序员文章站 2022-06-24 08:57:52
前端 Vue.js + 后端 Node.js 实现又拍云(https://www.upyun.com/) 上传、删除文件 ......

node.js 服务端

使用 node.js + express.js 实现 服务端

const express = require("express");
const app = express();
const axios = require('axios');

app.set('port', process.env.port || 8082);

// 静态资源目录,这里放在了根目录,生产环境不允许这样
app.use(express.static(__dirname));

// 启动一个端口为 8082 的服务器
app.listen(app.get('port'), () => {
  console.log("http://localhost:" +  app.get('port'));
});

准备 base64、hmac-sha1、md5 实现签名认证

详见:http://docs.upyun.com/api/authorization/#_5

const crypto = require("crypto");

// md5
function md5(value) {
  return crypto
    .createhash("md5")
    .update(value)
    .digest("hex");
}

// base64
function base64(value) {
  return buffer.from(value).tostring("base64");
}

// hmacsha1
function hmacsha1(secret, value) {
    return crypto.createhmac('sha1', secret).update(value, 'utf-8').digest().tostring('base64');
}

上传、删除接口

const date = new date().togmtstring();
const bucketname = "";  // 空间名
const key = ""; // 操作员
const secret = ""; // 密码
const upyunurl = 'http://v0.api.upyun.com/'

// upload
app.get("/api/token/upload", (req, res) => {
  let filename = (math.random() * 100000000) >>> 0;
  let expiration = ((date.now() / 1000) >>> 0) + 30 * 60;  // 请求的过期时间,unix utc 时间戳,单位秒。建议设为 30 分钟 http://docs.upyun.com/api/form_api/
  let method = "post";

  let policy = base64(
    json.stringify({
      bucket: bucketname,
      // "save-key": "/" + filename + "{.suffix}",
      "save-key": "/{filename}{.suffix}",
      expiration: expiration
    })
  );

  let authorization =
    "upyun " +
    key +
    ":" +
    hmacsha1(md5(secret), method + "&/" + bucketname + "&" + policy);

  res.json({
    msg: "ok",
    code: 200,
    data: {
      authorization: authorization,
      policy: policy
    }
  });
});

// delete
app.get('/api/token/del', (req, res) => {
  let item = req.query.item;
  let method = "delete"
  let authorization = "upyun " +
    key +
    ":" + 
    hmacsha1(md5(secret), method + '&/' + bucketname + item + '&'+ date);


  axios({
    url: upyunurl + bucketname + item,
    method: 'delete',
    headers: {
      'authorization': authorization,
      'date': date
    }
  }).then(response => {
    res.json({
      msg: "ok",
      code: 200,
      data: {}
    });  
  }).catch(err => {
    console.log('err', err)
  })
})

跨域接口调用

const cors = require('cors');

// cors @see https://github.com/expressjs/cors
app.use(cors());

前端

前端使用 vue.js 实现

引入 bootstrap.css

<link rel="stylesheet" type="text/css" href="https://unpkg.com/bootstrap@4.1.3/dist/css/bootstrap.css">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<!-- html -->
<div id="app">
  <div class="card" style="margin: 50px auto; width: 300px;">
    <div class="card-body">
      <h5 class="card-title">upyun upload & delete</h5>
      <div class="card-text">
        <div class="form-group">
          <label for="file">upload</label>
          <input type="file" id="file" class="form-control-file" @change="onchange">
          <div class="form-text text-muted">
            <ul>
               <li v-for="(item, index) in files">
                 {{item}} <a href="javascript:;" @click="ondel(item, index)">del</a>
               </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

引入 vue.js、axios

<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

js

const upurl = 'http://v0.api.upyun.com/'  // +空间名,如:http://v0.api.upyun.com/yun-temp
    const baseapi = 'http://localhost:8082/api/'
    let uploadinput;

    let app = new vue({
      el: '#app',
      data: {
        files: []
      },
      methods: {
        onchange: function () {
          gettoken(token => {
            let formdata = new formdata();
            formdata.append("file", uploadinput.files[0])
            formdata.append('policy', token.policy)
            formdata.append("authorization", token.authorization)

            axios({
              method: 'post',
              url: upurl,
              data: formdata
            }).then(res => {
              res = res || {}

              if (res.status !== 200) {
                console.log('error')
                return
              }

              let data = res.data || {}
              this.files.push(data.url)
              alert('success')
            }).catch(err => {
              console.log(err);
            });
          });
        },
        ondel: function (item, index) {
          this.files.splice(index, 1)
          axios.request({
            url: baseapi + 'token/del',
            method: 'get',
            params: {
              item: encodeuri(item)
            }
          }).then(res => {
            alert('deleted.')
          }).catch(err => {
            console.log(err)
          })
        }
      },
      mounted () {
        uploadinput = $('file')
      }
    })

    // dom 获取元素
    function $ (el) {
      return document.getelementbyid(el)
    }

    // 获取 token
    function gettoken (fn) {
      let token = window.localstorage.getitem('token');
      token = json.parse(token) || {};
      let nowtime = date.now();

      if (nowtime < token.expired && token.authorization && token.policy) {
        fn(token)
        return
      }

      axios({
        method: 'get',
        url: baseapi + 'token/upload'
      })
      .then(res => {
        let data = res.data || {}
        data = data.data || {}
        const authorization = data.authorization
        const policy = data.policy
        const expired = ((date.now() / 1000) >>> 0) + 30 * 60;

        token = {
          authorization,
          policy,
          expired
        }

        fn(token)
        window.localstorage.setitem('token', json.stringify(token))
      });
    }

项目源码

https://github.com/givebest/upyun-upload-delete-node.js

转载请注明出处: