我需要创建S3存储桶并将.jpg文件上传到创建的存储桶。
public void saveTest(){
try {
MinioClient minioClient = MinioClient.builder()
.endpoint("http://localhost:9099")
.credentials("minioadmin", "minioadmin")
.build();
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket("test").build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket("test").build());
} else {
System.out.println("Bucket 'test' already exists.");
}
FileInputStream stream = new FileInputStream("C:\\path\\picture.jpg");
minioClient.putObject("test", "Resume.jpg", stream, (long) stream.available(), null, null, null);
minioClient.putObject(PutObjectArgs
.builder()
.bucket("test")
.object("Resume.jpg")
.stream(new FileInputStream("C:\\path\\picture.jpg"), 1L, 5L * 1024 * 1024).build());
} catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
System.out.println("Error occurred: " + e.getMessage());
e.printStackTrace();
}
}
刚刚偶然发现了同样的问题,解决方法如下:
您必须在标头中传递 contentType,或者通过以下代码更轻松地传递:
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).contentType(contentType).object(objectName).stream(inputStream, inputStream.available(), 5L * 1024 * 1024).build());