import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import io.minio.http.Method;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
/**
* @author Jast
* @description
* @date 2023-08-18
*/
public class MinIOClient {
private static final String MINIO_API = "http://192.158.1251"; // MinIO服务器的访问地址
private static final String ACCESS_KEY = "minio"; // MinIO服务器的访问凭证,访问密钥
private static final String SECRET_KEY = "minioMMMMmmmm"; // MinIO服务器的访问凭证,访问密钥
private static final String BUCKET_NAME = "pic"; // 存储桶的名称
public static void main(String[] args) throws Exception {
String targetName = UUID.randomUUID()+".png"; // 生成一个随机的对象名称作为上传后的文件名
uploadImg(BUCKET_NAME, targetName, "/Users/mac/Public/临时目录/multimodal_image_13714985U025515.jpeg"); // 上传图片
String link = getLink(BUCKET_NAME, targetName, 60); // 获取图片链接,有效期为60秒
System.out.println("链接:"+link); // 输出图片链接
}
/**
* 上传图片到MinIO服务器
* @param bucketName 存储桶名称
* @param targetName 上传后的对象名称
* @param uploadFilePath 上传文件的路径
* @return 上传是否成功
* @throws IOException IO异常
* @throws ServerException MinIO服务器异常
* @throws InsufficientDataException 数据不足异常
* @throws ErrorResponseException 错误响应异常
* @throws NoSuchAlgorithmException 无效的算法异常
* @throws InvalidKeyException 无效的密钥异常
* @throws InvalidResponseException 无效的响应异常
* @throws XmlParserException XML解析异常
* @throws InternalException 内部异常
*/
public static boolean uploadImg(String bucketName, String targetName, String uploadFilePath) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
// 创建一个MinioClient对象,用于与MinIO服务器进行交互
MinioClient minioClient = MinioClient.builder()
.endpoint(MINIO_API) // 设置MinIO服务器的访问地址
.credentials(ACCESS_KEY, SECRET_KEY) // 设置MinIO服务器的访问凭证
.build();
// 设置分块大小为5GB
long partSize = 5L * 1024 * 1024 * 1024;
// 将文件上传到MinIO服务器
ObjectWriteResponse objectWriteResponse = minioClient.putObject(
PutObjectArgs.builder() // 构建PutObjectArgs对象,用于指定上传参数
.bucket(bucketName) // 设置要上传到的存储桶名称
.object(targetName) // 设置上传后的对象名称
.contentType("image/jpeg")//需要通过浏览器直接访问图片时,需要设置该参数,否则获取的链接访问时会直接当做文件下载
.stream(new FileInputStream(uploadFilePath), -1, partSize) // 设置上传的文件流和分块大小
.build() // 构建PutObjectArgs对象
);
System.out.println(targetName + "上传成功");
return true;
}
/**
* 获取MinIO服务器上图片的链接
* @param bucketName 存储桶名称
* @param targetName 对象名称
* @param expires 链接的有效期,单位为秒
* @return 图片链接
* @throws ServerException MinIO服务器异常
* @throws InsufficientDataException 数据不足异常
* @throws ErrorResponseException 错误响应异常
* @throws IOException IO异常
* @throws NoSuchAlgorithmException 无效的算法异常
* @throws InvalidKeyException 无效的密钥异常
* @throws InvalidResponseException 无效的响应异常
* @throws XmlParserException XML解析异常
* @throws InternalException 内部异常
*/
public static String getLink(String bucketName, String targetName, Integer expires) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
// 创建一个MinioClient对象,用于与MinIO服务器进行交互
MinioClient minioClient = MinioClient.builder()
.endpoint(MINIO_API) // 设置MinIO服务器的访问地址
.credentials(ACCESS_KEY, SECRET_KEY) // 设置MinIO服务器的访问凭证
.build();
// 构建GetPresignedObjectUrlArgs对象,用于指定获取预签名URL的参数
GetPresignedObjectUrlArgs arg = GetPresignedObjectUrlArgs.builder()
.method(Method.GET) // 设置请求方法为GET
.bucket(bucketName) // 设置存储桶名称
.object(targetName) // 设置对象名称
.expiry(expires) // 设置URL的有效期
.build();
// 调用MinioClient的getPresignedObjectUrl方法获取预签名URL
return minioClient.getPresignedObjectUrl(arg);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.