Rancher部署Minio高性能对象存储及简单工具类

Rancher部署Minio高性能对象存储及简单工具类

1. Rnacher部署Minio

Rancher部署Minio高性能对象存储及简单工具类_第1张图片
Rancher部署Minio高性能对象存储及简单工具类_第2张图片
Rancher部署Minio高性能对象存储及简单工具类_第3张图片
Rancher部署Minio高性能对象存储及简单工具类_第4张图片

2.JAVA使用Minio

            <!--Minio文件上传-->
            <dependency>
                <groupId>io.minio</groupId>
                <artifactId>minio</artifactId>
                <version>${minio.version}</version>
            </dependency>

变量的通过注入方式获取application.yml中值 也可以通过其他方式获取

@Component
public class MinioFileUtils {


    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.bucketName}")
    private String defaultBucketName;

    @PostConstruct
    private MinioClient client() {
        try {
            minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey)
                    .build();
        } catch (Exception e) {

        }

        createBucket(defaultBucketName);
        return minioClient;
    }

    /**
     * 创建Bucket
     *
     * @param bucketName
     */
    public void createBucket(String bucketName) {
        try {
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        } catch (Exception e) {

        }

    }

    public static String[] getDateFolder(String folder) {
        String[] retVal = new String[3];
        LocalDate localDate = LocalDate.now();
        retVal[0] = localDate.getYear() + "";
        int month = localDate.getMonthValue();
        retVal[1] = month < 10 ? "0" + month : month + "";
        int day = localDate.getDayOfMonth();
        retVal[2] = day < 10 ? "0" + day : day + "";
        String[] newRetVal = null;
        if (folder != null && !"".equals(folder)) {
            newRetVal = (String[]) Arrays.copyOf(retVal, retVal.length + 1);
            System.arraycopy(newRetVal, 0, newRetVal, 1, newRetVal.length - 1);
            newRetVal[0] = folder;
        }

        return newRetVal == null ? retVal : newRetVal;
    }

    public static String[] getDateFolder() {
        return getDateFolder((String)null);
    }


    public Response upload(String fileName, String fileType, MultipartFile file) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        String[] folders = MinioFileUtils.getDateFolder();

        String filePath=String.join("/", folders) + "/" +fileName;

        InputStream inputStream = file.getInputStream();
        ObjectWriteResponse res=minioClient.putObject(
                PutObjectArgs.builder().bucket(defaultBucketName).object(filePath).contentType(fileType)
                        .stream(
                                inputStream, inputStream.available(), -1)
                        .build());
        return SingleResponse.of(filePath);
    }
}

你可能感兴趣的:(Java,rancher,java,数据结构)