启动脚本
#!/bin/bash
export MINIO_ROOT_USER=minio-user
export MINIO_ROOT_PASSWORD=minio-pwd
# nohup启动服务 指定文件存放路径 /opt/minio/data 还有设置日志文件路径 /opt/minio/logs
# 目前最新版本已经区分了Console和API两个服务的端口,需要指定端口号,否则每次Minio会随即生成端口号。
nohup ./minio server --address :9002 --console-address :9001 /opt/minio/data > /opt/minio/logs/minio.log 2>&1 &
依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.3</version>
</dependency>
demo
package demo.minio;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class MinioTest {
private static final String ENDPOINT = "http://xxxxx:9002";
private static final String ACCESS_KEY = "minio-user";
private static final String SECRET_KEY = "minio-pwd";
private static final String BUCKET_NAME = "test";
public static void main(String[] args) {
try {
// 创建 MinIO 客户端
MinioClient minioClient = MinioClient.builder()
.endpoint(ENDPOINT)
.credentials(ACCESS_KEY, SECRET_KEY)
.build();
// 1. 检查存储桶是否存在,不存在则创建
checkAndCreateBucket(minioClient);
// 2. 列出所有存储桶
listBuckets(minioClient);
// 3. 上传文件
uploadObject(minioClient);
// 4. 获取文件外链
String url = getObjectUrl(minioClient, "test-object.txt", 7);
System.out.println("文件外链: " + url);
// // 5. 列出存储桶中的对象
// listObjects(minioClient);
//
// // 6. 下载文件
// downloadObject(minioClient);
//
// // 7. 删除文件
// deleteObject(minioClient);
//
// // 8. 删除存储桶
// deleteBucket(minioClient);
System.out.println("所有操作成功完成!");
} catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
System.err.println("操作失败: " + e.getMessage());
e.printStackTrace();
}
}
private static void checkAndCreateBucket(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
boolean exists = client.bucketExists(BucketExistsArgs.builder().bucket(BUCKET_NAME).build());
if (!exists) {
client.makeBucket(MakeBucketArgs.builder().bucket(BUCKET_NAME).build());
System.out.println("创建存储桶: " + BUCKET_NAME);
} else {
System.out.println("存储桶已存在: " + BUCKET_NAME);
}
}
private static void listBuckets(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
List<Bucket> buckets = client.listBuckets();
System.out.println("所有存储桶:");
for (Bucket bucket : buckets) {
System.out.println(" - " + bucket.name() + " (创建时间: " + bucket.creationDate() + ")");
}
}
private static void uploadObject(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
String objectName = "test-object.txt";
String content = "Hello, MinIO from Java!";
ByteArrayInputStream stream = new ByteArrayInputStream(content.getBytes());
client.putObject(PutObjectArgs.builder()
.bucket(BUCKET_NAME)
.object(objectName)
.stream(stream, stream.available(), -1)
.contentType("text/plain")
.build());
System.out.println("上传对象: " + objectName + " 到存储桶: " + BUCKET_NAME);
stream.close();
}
// 新增:获取文件外链方法
private static String getObjectUrl(MinioClient client, String objectName, int expires)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(BUCKET_NAME)
.object(objectName)
.expiry(expires, TimeUnit.DAYS)
.build();
return client.getPresignedObjectUrl(args);
}
private static void listObjects(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
System.out.println("存储桶 " + BUCKET_NAME + " 中的对象:");
Iterable<Result<Item>> results = client.listObjects(
ListObjectsArgs.builder().bucket(BUCKET_NAME).build());
for (Result<Item> result : results) {
Item item = result.get();
System.out.println(" - " + item.objectName() + " (大小: " + item.size() + "B)");
}
}
private static void downloadObject(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
String objectName = "test-object.txt";
client.getObject(GetObjectArgs.builder()
.bucket(BUCKET_NAME)
.object(objectName)
.build());
System.out.println("下载对象: " + objectName);
// 文件将下载到当前目录,实际应用中可以指定输出路径
}
private static void deleteObject(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
String objectName = "test-object.txt";
client.removeObject(RemoveObjectArgs.builder()
.bucket(BUCKET_NAME)
.object(objectName)
.build());
System.out.println("删除对象: " + objectName);
}
private static void deleteBucket(MinioClient client)
throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
InternalException {
client.removeBucket(RemoveBucketArgs.builder()
.bucket(BUCKET_NAME)
.build());
System.out.println("删除存储桶: " + BUCKET_NAME);
}
}