栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mini2使用教程(mini配置)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mini2使用教程(mini配置)

第一章 MinIO介绍与安装

MinIO是一个高性能对象存储方案,支持分布式部署,它的API可以与 Amazon S3 云存储服务兼容。

安装MinIO

为了简单的学习 MinIO,这里通过Docker的方式来安装。对于生产环境,官方推荐使用集群的方式来安装。

docker run 
  -p 9000:9000 
  -p 9001:9001 
  -e "MINIO_ROOT_USER=minio" 
  -e "MINIO_ROOT_PASSWORD=minio12345" 
  quay.io/minio/minio server /data --console-address ":9001"

启动成功后,控制台会打印 API(endpoint) 的地址和 console 地址。

WARNING: Detected Linux kernel version older than 4.0.0 release, there are some known potential performance problems with this kernel version. MinIO recommends a minimum of 4.x.x linux kernel version for best performance
API: http://172.17.0.2:9000  http://127.0.0.1:9000 

Console: http://172.17.0.2:9001 http://127.0.0.1:9001 

documentation: https://docs.min.io

Maven依赖

    io.minio
    minio
    8.3.7


    com.squareup.okhttp3
    okhttp
    4.9.0

YML配置
minio:
  endpoint: http://172.17.0.2:9000 # API地址
  bucketName: bucket-demo1
  accessKey: minio
  secretKey: minio12345
创建MinIO操作服务类
package com.example.qiniuyundemo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Service
public class MinIoService implements FileUploader {

    @Value("${minio.endpoint}")
    protected String endpoint;

    @Value("${minio.bucketName}")
    protected String bucketName;

    @Value("${minio.accessKey}")
    protected String accessKey;

    @Value("${minio.secretKey}")
    protected String secretKey;

    @Autowired
    private ObjectMapper objectMapper;

    private MinioClient client;

    @PostConstruct
    public void init() {
        client = MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    @Override
    public String upload(String filename, String key) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        ObjectWriteResponse response = client.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(System.currentTimeMillis()+".png")
                        .filename(filename)
                        .build()
        );
        String bucket = response.bucket();
        String region = response.region();
        String object = response.object();
        System.out.println(bucket + "-" + region + "-" + object);
        return null;
    }


    @Override
    public String getFile(String key) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        GetObjectResponse response = client.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(key)
                        .build()
        );

        return String.join("/", endpoint, response.bucket(), response.object());
    }
}

单元测试
@SpringBootTest
class MinIoServiceTest {

    @Autowired
    private FileUploader fileUploader;

    @Test
    void upload() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String filename = "C:\imgs\mybatis1.png";
        fileUploader.upload(filename, null);
    }

    @Test
    void getFile() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String url = fileUploader.getFile("1647745868109.png");
        System.out.println(url);
    }
}
参考

MinIO Docker Quickstart Guide高性能分布式存储服务Minio安装配置入门

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775818.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号