java 操作minio
时间: 2023-10-16 12:11:37 浏览: 173
基于Java的minio相关操作示例
Minio是一个开源的对象存储服务器,它可以存储大量的数据,支持多种数据类型的存储,包括文本、图片、视频、音频等。在Java中,我们可以使用Minio Java SDK来操作Minio。
首先,需要引入Minio Java SDK依赖:
```xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.6</version>
</dependency>
```
然后,创建一个MinioClient对象来连接到Minio服务器:
```java
String accessKey = "YOUR_ACCESS_KEY";
String secretKey = "YOUR_SECRET_KEY";
String endpoint = "http://YOUR_MINIO_SERVER:9000";
MinioClient minioClient = new MinioClient.Builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
```
接下来,可以使用MinioClient对象来执行各种操作。例如:
- 创建一个存储桶:
```java
String bucketName = "test-bucket";
boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!bucketExists) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
```
- 上传一个文件:
```java
String bucketName = "test-bucket";
String objectName = "test-file.txt";
String fileName = "path/to/local/file.txt";
minioClient.uploadObject(UploadObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.filename(fileName)
.build());
```
- 下载一个文件:
```java
String bucketName = "test-bucket";
String objectName = "test-file.txt";
String fileName = "path/to/local/file.txt";
minioClient.downloadObject(DownloadObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.filename(fileName)
.build());
```
- 列出所有存储桶:
```java
List<Bucket> buckets = minioClient.listBuckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.name());
}
```
- 列出存储桶中的所有对象:
```java
String bucketName = "test-bucket";
Iterable<Result<Item>> objects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
for (Result<Item> result : objects) {
Item item = result.get();
System.out.println(item.objectName());
}
```
以上是一些基本的操作示例,更多的操作可以查看Minio Java SDK的官方文档。
阅读全文