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.
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.