14

I am trying to batch upload a couple of files in S3 using TranferManager. Below is my code:

@GetMapping("s3/batch/upload/base64")
public void uploadBase64ToWebp() {

     List<File> fileList = new ArrayList<>();
    String rawData = "1";
    String base64Data = Base64.encodeBase64String(rawData.getBytes(StandardCharsets.UTF_8));
     byte[] data = getBinaryImageData(base64Data);
     File file = new File("1234.webp");
     try {

         FileUtils.writeByteArrayToFile(file, data);

     } catch (IOException e) {

         System.out.println(e);
     }
     fileList.add(file);
     ObjectMetadataProvider metadataProvider = new ObjectMetadataProvider() {
            public void provideObjectMetadata(File file, ObjectMetadata metadata) {

                metadata.setContentType("image/webp");
                metadata.getUserMetadata().put("filename", file.getPath());
                metadata.getUserMetadata().put("createDateTime", new Date().toString());
            }
        };
        TransferManager transferManager = TransferManagerBuilder.standard().withS3Client(amazonS3).build();
     transferManager.uploadFileList(bucketName, "school/transactions", new File("."), fileList, metadataProvider);
}

private byte[] getBinaryImageData(String image) {

        return Base64.decodeBase64(
            image
                .replace("data:image/webp;base64,", "")
                .getBytes(StandardCharsets.UTF_8)
        );
    }

Here,as you can see, I am giving the file name as '1234.webp', but the file name that is getting saved in S3 is '34.webp'. I tried a bigger name like '1234567.webp' and again the first two digits get truncated and the file name is '34567.webp'. What i am doing wrong?

Please note, that in the example that i have pasted here, i am just uploading one file but in my actual code, I do upload multiple files, but in both the cases, the names get truncated anyhow.

T Anna
  • 874
  • 5
  • 21
  • 52
  • 3
    metadata.getUserMetadata().put("filename", file.getPath()); If you want to use the file name, shouldn't you be using file.getName() instead of file.getPath() ? – srikanth Nutigattu Aug 06 '18 at 08:36
  • 3
    When I see the metadata for the file in S3, it has the correct name '1234.webp' for the filename attribute. Its the name with which the file is acutally saved is the issue. When i go to the bucket folder, i see '34.webp' as the file and if I try to see its metadata, the filename is fine – T Anna Aug 06 '18 at 08:43

1 Answers1

9

Ok, so it was a Java IO issue. I updated the below to show the path and it worked.

Old:

File file = new File("1234.webp");

New:

File file = new File("./1234.webp");

Still trying to figure out why the first two letters got dropped.

T Anna
  • 874
  • 5
  • 21
  • 52
  • 4
    The first two characters are dropped because the provided `pathname` string is converted to an abstract `pathname` - see https://docs.oracle.com/javase/7/docs/api/java/io/File.html, https://stackoverflow.com/a/24611323/5787099, and https://stackoverflow.com/q/38731172/5787099 – KiteCoder Aug 12 '18 at 23:27