Minio S3上传.jpg文件到存储桶

问题描述 投票:0回答:1

我需要创建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();
    }
  }

我没有错误,文件出现在存储桶中,但我无法打开它 enter image description here

amazon-s3 bucket minio
1个回答
0
投票

刚刚偶然发现了同样的问题,解决方法如下:

您必须在标头中传递 contentType,或者通过以下代码更轻松地传递:

minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).contentType(contentType).object(objectName).stream(inputStream, inputStream.available(), 5L * 1024 * 1024).build());
© www.soinside.com 2019 - 2024. All rights reserved.