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.
TransferManager and all Amazon Web Services client objects are thread safe. By default, the thread pool will shutdown when the transfer manager instance is garbage collected.
To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.
In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With