Minio之通过JavaSDK操作Minio进行文件上与传下载的详细案例
文章目录
1. Minio SDK
1. Minio发布支持的SDK
SDK:Software Development Kits
MinIO发布以下软件开发工具包(SDK):
2. JavaSDK文档链接
各语言sdk地址:https://min.io/docs/minio/linux/developers/minio-drivers.html
Java快速开始地址:https://min.io/docs/minio/linux/developers/java/minio-java.html#minio-java-quickstart
Java Client API:https://min.io/docs/minio/linux/developers/java/API.html
2. Java SDK操作Minio
1. 引入依赖
- maven
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.1</version>
</dependency>
- gradle
dependencies {
implementation("io.minio:minio:8.5.1")
}
- jar
Download the latest JAR file for version 8.5.1 of the SDK from the Sonatype Maven Central Repository.
- 完整的pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yuan</groupId>
<artifactId>yuan-minio-client8</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.minio/minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2. 上传案例
先判断bucket
yuantest
是否存在,如果不存在则创建,否则直接上传文件到yuanteset
下更多Java Client API请查看:https://min.io/docs/minio/linux/developers/java/API.html,里面有详细的说明与代码案例
FileUploader.java
类如下
package com.yuan.demo;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* 来源于官网案例:https://min.io/docs/minio/linux/developers/java/minio-java.html#minio-java-quickstart
*/
public class FileUploader {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
// Create a minioClient with the MinIO server playground, its access key and secret key.
MinioClient minioClient =
MinioClient.builder()
//.endpoint("https://play.min.io")
//.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
//minio服务器地址
//1.accessKey与secretKey配置方式1:直接使用minio的登录用户名与密码,如下面使用默认的minioadmin/minioadmin
//.credentials("minioadmin", "minioadmin")
//2.accessKey与secretKey配置方式2(推荐):在minio控制台中的`User-->Access Key`中创建一个即可
.credentials("Da2FVnOqZ41k15Ha", "TGN6lY7ZB2Ft3b5u7i94zlCiRpt357dO")
.build();
// Make 'yuantest' bucket if not exist.
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("yuantest").build());
if (!found) {
// Make a new bucket called 'asiatrip'.
minioClient.makeBucket(MakeBucketArgs.builder().bucket("yuantest").build());
} else {
System.out.println("Bucket 'yuantest' already exists.");
}
// Upload 'D:\aa\files\Java开发手册(黄山版).pdf' as object name 'Java开发手册(黄山版).pdf' to bucket
// 'yuantest'.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("yuantest")
.object("Java开发手册(黄山版).pdf")
.filename("D:\\aa\\files\\Java开发手册(黄山版).pdf")
.build());
System.out.println("'D:\\aa\\files\\Java开发手册(黄山版).pdf' is successfully uploaded as object 'Java开发手册(黄山版).pdf' to bucket 'yuantest'.");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
System.out.println("HTTP trace: " + e.httpTrace());
}
}
}
3. 文件下载案例
FileDownloader.java
如下
package com.yuan.demo;
import io.minio.*;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* 文件下载:详情可查看APi https://min.io/docs/minio/linux/developers/java/API.html
*/
public class FileDownloader {
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
// Create a minioClient with the MinIO server playground, its access key and secret key.
MinioClient minioClient =
MinioClient.builder()
//.endpoint("https://play.min.io")
//.credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
//minio服务器地址
.endpoint("http://localhost:9000")
//1.accessKey与secretKey配置方式1:直接使用minio的登录用户名与密码,如下面使用默认的minioadmin/minioadmin
//.credentials("minioadmin", "minioadmin")
//2.accessKey与secretKey配置方式2(推荐):在minio控制台中的`User-->Access Key`中创建一个即可
.credentials("Da2FVnOqZ41k15Ha", "TGN6lY7ZB2Ft3b5u7i94zlCiRpt357dO")
.build();
// 文件下载
// 'yuantest'.
minioClient.downloadObject(
DownloadObjectArgs
.builder()
//bucketName
.bucket("yuantest")
//objectName
.object("Java开发手册(黄山版).pdf")
//outputpath,直接指定文件的输出位置+文件名即可
.filename("D:\\aa\\files\\Java开发手册(黄山版)_mynew11.pdf")
.build());
System.out.println("bucket 'yuantest' 中的'Java开发手册(黄山版).pdf'文件已下载到'D:\\aa\\files\\Java开发手册(黄山版)_mynew11.pdf.");
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
System.out.println("HTTP trace: " + e.httpTrace());
}
}
}