From 82f8d95e67e66b210b94135f28fd1f7dfa54052c Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Wed, 3 Mar 2021 17:28:50 +0800 Subject: [PATCH] sync --- pom.xml | 40 ++++++++++- .../demo/MinIOClientDemo.java | 72 +++++++++++++++++++ .../com/duanledexianxianxian/demo/data1.txt | 1 + src/main/resources/files/data2.txt | 1 + 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/duanledexianxianxian/demo/MinIOClientDemo.java create mode 100644 src/main/java/com/duanledexianxianxian/demo/data1.txt create mode 100644 src/main/resources/files/data2.txt diff --git a/pom.xml b/pom.xml index 2c2bd81..b14a082 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ com.duanledexianxianxian.demo demo-parent - pom + jar 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent - 2.1.6.RELEASE + 2.4.3 @@ -42,6 +42,18 @@ guava 23.0 + + + io.minio + minio + 8.1.0 + + + + org.jetbrains.kotlin + kotlin-stdlib + + @@ -50,11 +62,35 @@ + + + src/main/resources + true + + + + src/main/java + + + **/*.* + + true + + + + org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-resources-plugin + + UTF-8 + + diff --git a/src/main/java/com/duanledexianxianxian/demo/MinIOClientDemo.java b/src/main/java/com/duanledexianxianxian/demo/MinIOClientDemo.java new file mode 100644 index 0000000..53e91d8 --- /dev/null +++ b/src/main/java/com/duanledexianxianxian/demo/MinIOClientDemo.java @@ -0,0 +1,72 @@ +package com.duanledexianxianxian.demo; + +import io.minio.BucketExistsArgs; +import io.minio.MakeBucketArgs; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; +import io.minio.errors.MinioException; + +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +/** + * @author duanledexianxianxian + * @date 2021/3/2 1:25 + * @since 1.0.0 + */ +public class MinIOClientDemo { + public static void main(String[] args) { + try { + // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象 + MinioClient minioClient = + MinioClient.builder() + .endpoint("http://platform.kuopu.net:50001") + .credentials("minio", "minio123") + .build(); + + // 检查存储桶是否已经存在 + boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket("kim-dev").build()); + if (isExist) { + System.out.println("Bucket already exists."); + } else { + // 创建一个名为kim-dev的存储桶,用于存储照片的zip文件。 + minioClient.makeBucket(MakeBucketArgs.builder().bucket("kim-dev").build()); + } + + // 使用putObject上传一个文件到存储桶中。 + minioClient.putObject(PutObjectArgs.builder().bucket("kim-dev").object("data2.txt").stream( + MinIOClientDemo.class.getClassLoader().getResourceAsStream("./files/data2.txt"), -1, 1024*5*1024) + .contentType("video/mp4") + .build()); + System.out.println("this.getClass().getClassLoader().getResource(\"/files/data2.txt\") is successfully uploaded as asiaphotos.zip to `asiatrip` bucket."); + } catch (MinioException e) { + System.out.println("Error occurred: " + e); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } + //// 当前类的目录 + //System.out.println(MinIOClientDemo.class.getResource("")); + //// 当前项目的目录 + //System.out.println(MinIOClientDemo.class.getResource("/")); + //// 当前类的目录下的文件 + //System.out.println(MinIOClientDemo.class.getResource("data1.txt")); + //// 绝对路径不存在 + //System.out.println(MinIOClientDemo.class.getResource("/data1.txt")); + // + //// 当前项目的目录 + //System.out.println(MinIOClientDemo.class.getClassLoader().getResource("")); + //System.out.println(MinIOClientDemo.class.getClassLoader().getResource("/")); + //System.out.println(MinIOClientDemo.class.getClassLoader().getResource("data1.txt")); + //System.out.println(MinIOClientDemo.class.getClassLoader().getResource("/data1.txt")); + //// 当前项目的目录 + //System.out.println(MinIOClientDemo.class.getClassLoader().getResource("application.yml")); + //// 当前项目的目录 + //System.out.println(MinIOClientDemo.class.getClassLoader().getResource("./files/data2.txt")); + } + +} diff --git a/src/main/java/com/duanledexianxianxian/demo/data1.txt b/src/main/java/com/duanledexianxianxian/demo/data1.txt new file mode 100644 index 0000000..6349ddb --- /dev/null +++ b/src/main/java/com/duanledexianxianxian/demo/data1.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/src/main/resources/files/data2.txt b/src/main/resources/files/data2.txt new file mode 100644 index 0000000..6349ddb --- /dev/null +++ b/src/main/resources/files/data2.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file -- GitLab