最棒的开源OSS存储 MinIO,真香!

最棒的开源OSS存储 MinIO,真香!

隔壁王叔叔
2021-02-19 / 0 评论 / 369 阅读 / 正在检测是否收录...

直接用docker-compose部署 不懂的自行了解。

创建docker-compose.yml

version: '3.7'

services:
  minio:
    image: minio/minio:edge
    container_name: minio
    ports:
      - "9000:9000"
    restart: always
    command: server /data
    environment:
      MINIO_ROOT_USER: minio
      MINIO_ROOT_PASSWORD: minioadmin
    logging:
      options:
        max-size: "50M"
      driver: json-file
    volumes:
      - /data/minio_data:/data

访问 http://127.0.0.1:9000 输入 账号 minio 密码 minioadmin

点击右下角加好创建一个桶,在桶下面创建一个目录ticket/ 点击左侧桶设置规则ticket/* read and write 规则如图

不设置的话获取到的链接不是开放式链接,并且分享后只有最多7天的访问限制。
minio设置

然后就可以写代码了我用的java

添加maven包

  <dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>3.0.10</version>
  </dependency>

下面是代码部分

package com.boot;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.util.Base64;
import com.alibaba.fastjson.JSONObject;
import io.minio.MinioClient;

public class MinioUtil {

  /**
   *
   * @Title: uploadImage
   * @Description:上传图片
   * @param inputStream
   * @param suffix
   * @return
   * @throws Exception
   */
  public static JSONObject uploadImage(InputStream inputStream, String suffix) throws Exception {
    return upload(inputStream, suffix, "image/jpeg");
  }


  /**
   * @Title: uploadVideo
   * @Description:上传视频
   * @param inputStream
   * @param suffix
   * @return
   * @throws Exception
   */
  public static JSONObject uploadVideo(InputStream inputStream, String suffix) throws Exception {
    return upload(inputStream, suffix, "video/mp4");
  }


  /**
   * @Title: uploadVideo
   * @Description:上传文件
   * @param inputStream
   * @param suffix
   * @return
   * @throws Exception
   */
  public static JSONObject uploadFile(InputStream inputStream, String suffix) throws Exception {
    return upload(inputStream, suffix, "application/octet-stream");
  }
  
  /**
   * 上传字符串大文本内容
   *
   * @Title: uploadString
   * @Description:描述方法
   * @param str
   * @return
   * @throws Exception
   */
  public static JSONObject uploadString(String str) throws Exception {
    if (!StrUtil.isNotBlank(str)) {
      return new JSONObject();
    }
    InputStream inputStream = new ByteArrayInputStream(str.getBytes());
    return upload(inputStream, null, "text/html");
  }


  /**
   * @Title: upload
   * @Description:上传主功能
   * @return
   * @throws Exception
   */
  private static JSONObject upload(InputStream inputStream, String suffix, String contentType) throws Exception {
    JSONObject map = new JSONObject();
    String minio_url = "http://127.0.0.1:9000";
    String minio_name = "minio";
    String minio_pass = "minioadmin";
    String minio_bucketName = "myapp";
    MinioClient minioClient = new MinioClient(minio_url, minio_name, minio_pass);

    // 检查存储桶是否已经存在
    boolean isExist = minioClient.bucketExists(minio_bucketName);
    if(isExist) {
      System.out.println("Bucket already exists.");
    } else {
      // 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。
      minioClient.makeBucket(minio_bucketName);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    String ymd = sdf.format(new Date());
    String objectName = "ticket/"+ymd + "/" + UUID.randomUUID().toString() + (suffix != null ? suffix : "")+".png";
    minioClient.putObject(minio_bucketName, objectName, inputStream, contentType);
    String url = minioClient.getObjectUrl(minio_bucketName, objectName);
    map.put("flag", "0");
    map.put("mess", "上传成功");
    map.put("url", url);
    map.put("path", minio_bucketName + "/" + objectName);
    return map;
  }
  
  public static void main(String[] args) {
    String imagePath = "C:\\Users\\wy\\Downloads\\qrcode_258.png";
    String base64Str = Base64.byteArrayToBase64(FileUtil.readBytes(imagePath));
    System.out.println(base64Str);

    byte[] imageBytes = Base64.base64ToByteArray(base64Str);

    InputStream inputStream = new ByteArrayInputStream(imageBytes);

    try{
      JSONObject jsonObject = uploadImage(inputStream, "user_4");
      System.out.println(JSONUtil.toJsonStr(jsonObject));
    }catch (Exception e){
      e.printStackTrace();
    }
  }
}

这东西很适合不想花钱用oss产品的小规模项目。

0

评论 (0)

取消