minio本地文件上传/远程url上传

本文介绍了如何在Node.js环境中,通过Minio库实现本地文件和远程图片的上传,包括处理文件读取、上传方法、跨域请求以及Vue配置。同时展示了进度条的更新和错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 本地文件上传
<input type="file" id="fileInput" multiple @change="handleFileUpload">
// 进度条
<el-progress class="progress" :percentage="Number(percentage)" v-if="percentage && percentage < 100" :color="customColors"></el-progress>
  • 存储桶连接
// 内网访问速度更快,根据环境切换
let endPoint = localStorage.getItem('endPoint') || '外网地址'
let Minio = require('minio')
let client = null;
let params = {
  endPoint: endPoint, //后端提供
  useSSL: false,
  accessKey: 'accessKey', //后端提供
  secretKey: 'secretKey' //后端提供
}
if (endPoint === '内网地址') {
  params.port = 9000; //端口号 看是否有端口号,没有则不需要
} else {
  delete params.port;
}
client = new Minio.Client(params);
  • 本地文件上传
    async handleFileUpload (event) {
      if (this.loading) {
        return
      }
      const files = event.target.files;
      this.loading = true;
      for (let index = 0; index < files.length; index++) {
        const files2 = files[index]
        const params = {
          files: files2,
          prefix: '需要上传至minio的路径地址'
        }
        this.percentage = '0'
        this.uploadFile(params, files.length)
      }
    },
  • 上传方法
    async uploadFile (params, listLength) {
      let file = params.files
      let objectName = params.prefix
      const fileName = objectName + file.name
      const reader = new FileReader()
      reader.onload = () => {
        const fileData = new Uint8Array(reader.result)
        const buffer = Buffer.from(fileData)
        client.putObject('你的存储桶名字', fileName, buffer, (err, etag) => {
          if (err) {
            console.error(err)
            return
          }
          this.successNum++; // 多文件上传,进度条
          if (listLength === this.successNum) {
            this.percentage = 100;
            setTimeout(() => {
               this.maskMode = false
               this.percentage = 0;
               this.successNum = 0;
               this.operatorType = 0
               this.getList()
            }, 300);
          } else {
            this.percentage = (((this.successNum) / (listLength + 1)) * 100).toFixed(2);
          }
          if (listLength === this.successNum) {
            this.loading = false
            this.getList()
          }
        })
      }
      reader.readAsArrayBuffer(file)
    },
  • 远程url上传方法
    uploadImg (imgs, taskName) {
      this.percentage = 0;
      this.successNum = 0;
      this.maskMode = true;
      // 获取地址形式图片的URL
      let imageUrl = imgs; // 这里是第三方地址形式图片的URL
      // 发起HTTP GET请求获取图片数据
      for (let index = 0; index < imageUrl.length; index++) {
        const element = imageUrl[index];
        //图片名称 昵称_流程_字段_随机数.png
        getImage(element.img).then(res => {
          let file = new File([res], element.imgName, { type: res.type });
          const params = {
            files: file,
            prefix: this.itemList[1].name + '/' + taskName + '/',
          }
          this.uploadFile(params, imageUrl.length)
        })
      }
    },
  • 远程图片请求方法
/**
 *
 * @param url 图片地址
 */
export async function getImage (url) {
  const { data } = await axios({
    method: "GET",
    url: "/upload" + url,
    responseType: "blob"
  })
  return data;
}
  • 跨域处理
// vue.config.js文件
module.exports = {
  devServer: {
    hot: true,
    disableHostCheck: true,
    proxy: {
      '/upload': {
        //代理图片下载的接口
        target: "下载图片的地址源",
        changeOrigin: true,
        pathRewrite: {
          '^/upload': ''
        }
      }
    }
  },
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值