java代码中调用minio上传文件接口
时间: 2023-08-17 21:38:54 浏览: 120
要在Java代码中使用MinIO SDK上传文件,您需要完成以下步骤:
1. 添加MinIO SDK的依赖项。您可以使用Maven或Gradle将其添加到您的项目中,例如:
使用Maven:
```xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.1.0</version>
</dependency>
```
使用Gradle:
```
implementation 'io.minio:minio:8.1.0'
```
2. 在代码中创建MinioClient对象并初始化,例如:
```java
import io.minio.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
public class MinioUploader {
private static final String ENDPOINT = "http://localhost:9000";
private static final String ACCESS_KEY = "ACCESS_KEY";
private static final String SECRET_KEY = "SECRET_KEY";
public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException {
MinioClient minioClient = MinioClient.builder()
.endpoint(ENDPOINT)
.credentials(ACCESS_KEY, SECRET_KEY)
.build();
// your upload code here
}
}
```
请注意,您需要将`ENDPOINT`,`ACCESS_KEY`和`SECRET_KEY`更改为您的MinIO服务器的正确值。
3. 上传文件。您可以使用`putObject`方法上传文件,例如:
```java
import io.minio.*;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
public class MinioUploader {
private static final String ENDPOINT = "http://localhost:9000";
private static final String ACCESS_KEY = "ACCESS_KEY";
private static final String SECRET_KEY = "SECRET_KEY";
public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException {
MinioClient minioClient = MinioClient.builder()
.endpoint(ENDPOINT)
.credentials(ACCESS_KEY, SECRET_KEY)
.build();
String bucketName = "my-bucket";
String objectName = "my-object";
String fileName = "/path/to/file";
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.filename(fileName)
.build();
minioClient.putObject(putObjectArgs);
System.out.println("File uploaded successfully");
}
}
```
请注意,您需要将`bucketName`,`objectName`和`fileName`更改为您自己的值。
这就是上传文件到MinIO的基本步骤。您可以根据需要调整代码和参数。
阅读全文