zoukankan      html  css  js  c++  java
  • Electron-Vue 使用 oss 实现上传、下载

    一、安装阿里云 OSS

    npm install ali-oss

    二、使用 require 进行引用

    let OSS = require('ali-oss');

    三、上传文件

    async doUpload(){
      const _this = this;
      let client = new OSS({
        region:_this.region,
        accessKeyId: _this.keyId,
        accessKeySecret: _this.keySecret,
        bucket:_this.bucket
      });
    
      try {
        let result = await client.put('srv2.0-nl/test1.jpg', 'C:\Users\Administrator\Desktop\光速4.jpg')
        console.log('上传成功');
        console.log(result);
      } catch(e) {
        console.log('出错了');
        console.log(e);
      }
    }

    四、分片上传

    const progress = async function(p) {
      //  进度百分比(0-1之间小数)
      console.log(p);
    }
    async multipartUpload() {
      const _this = this;
      let client = new OSS({
        region:_this.region,
        accessKeyId: _this.keyId,
        accessKeySecret: _this.keySecret,
        bucket:_this.bucket
      });
    
      try {
        let result = await client.multipartUpload('srv2.0-nl/test1.jpg', 'C:\Users\Administrator\Desktop\光速3.jpg', {
          progress,
          meta: {
            year: 2017,
            people: 'test'
          }
        });
        console.log(result);
        let head = await client.head('srv2.0-nl/test1.jpg');
        console.log(head);
      } catch (e) {
        // 捕获超时异常
        if (e.code === 'ConnectionTimeoutError') {
          console.log("Woops,超时啦!");
          // do ConnectionTimeoutError operation
        }
        console.log(e)
      }
    }

    说明:

    meta 参数是一个用户自定义的元数据,通过 head 接口可以获取到 object 的 meta 数据。

    progress 参数是一个进度回调函数,用于获取上传进度。progress可以是一个 async 函数。

    五、下载文件

    async download() {
      const _this = this;
      let client = new OSS({
        region:_this.region,
        accessKeyId: _this.keyId,
        accessKeySecret: _this.keySecret,
        bucket:_this.bucket
      });
    
      try {
        let result = await client.get('srv2.0-nl/test1.jpg', 'C:\Users\Administrator\Desktop\下载.jpg');
        console.log(result);
      } catch (e) {
        console.log(e);
      }
    }
  • 相关阅读:
    all the tops
    es6 and typescript
    [leetcode]question5: Longest Palindromic Substring
    webpack and publish lib
    HTTPClient to use http/https protocol to send request
    《算法导论》-分治法-笔记
    《Linux C编程一站式学习》-笔记
    WIN7中同时打开多个独立Excel窗口
    RAD Studio XE6之Tpanel
    vb中StatusBar1.Panels(3).Text = Format(Date, "yyyy年mm月dd日")是什么意思
  • 原文地址:https://www.cnblogs.com/zerotoinfinity/p/14184793.html
Copyright © 2011-2022 走看看