此博文内容为:
- 使用SDK创建bucket;
- 使用SDK上传文件;
- 使用SDK下载文件。
maven添加:
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.5.2</version>
</dependency>
首先使用Java SDK创建一个客户端实例:
MinioClient minioClient = MinioClient.builder()
.endpoint("http://192.168.36.128:9000")
.credentials("minioadmin", "minioadmin")
.build();
随后创建一个bucket
minioClient.makeBucket(MakeBucketArgs
.builder()
.bucket("user1")
.build());
上传文件:
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\cff\\Desktop\\test.pdf");
ObjectWriteResponse writeResponse = minioClient.putObject(PutObjectArgs
.builder()
.bucket("user1")
.object("test.pdf")
.stream(fileInputStream, fileInputStream.available(), -1).build());
System.out.println("writeResponse : " + writeResponse.etag() + " " + writeResponse.versionId(
这里有一个要注意的地方:
public PutObjectArgs.Builder stream(InputStream stream, long objectSize, long partSize) {
this.validateNotNull(stream, "stream");
long[] partinfo = this.getPartInfo(objectSize, partSize);
long pSize = partinfo[0];
int pCount = (int)partinfo[1];
BufferedInputStream bis = stream instanceof BufferedInputStream ? (BufferedInputStream)stream : new BufferedInputStream(stream);
return this.setStream(bis, objectSize, pSize, pCount);
}
这个PutObjectArgs是上传接口partSize是分片,这里我填写的是-1。
下载文件:
try (InputStream stream =
minioClient.getObject(GetObjectArgs
.builder()
.bucket("user1")
.object("test.pdf")
.build())) {
// Read the stream
saveFile(stream, "C:\\Users\\cff\\Desktop\\test2.pdf");
}catch (Exception e){
e.printStackTrace();
}
完整代码如下:
package cn.minio.test;
import io.minio.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.*;
@SpringBootApplication
public class Main implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
void saveFile(InputStream inputStream, String saveFile){
try (
OutputStream outputStream = new FileOutputStream(saveFile)) {
// 读取源文件数据并写入目标文件
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("文件保存成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(String... args) throws Exception {
MinioClient minioClient = MinioClient.builder()
.endpoint("http://192.168.36.128:9000")
.credentials("minioadmin", "minioadmin")
.build();
minioClient.makeBucket(MakeBucketArgs
.builder()
.bucket("user1")
.build());
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\cff\\Desktop\\test.pdf");
ObjectWriteResponse writeResponse = minioClient.putObject(PutObjectArgs
.builder()
.bucket("user1")
.object("test.pdf")
.stream(fileInputStream, fileInputStream.available(), -1).build());
System.out.println("writeResponse : " + writeResponse.etag() + " " + writeResponse.versionId());
try (InputStream stream =
minioClient.getObject(GetObjectArgs
.builder()
.bucket("user1")
.object("test.pdf")
.build())) {
// Read the stream
saveFile(stream, "C:\\Users\\cff\\Desktop\\test2.pdf");
}catch (Exception e){
e.printStackTrace();
}
}
}
程序运行截图如下:
源码打包下载地址:
Java/MinIOTest at master · fengfanchen/Java · GitHub