I have an amazon s3 bucket which contains one file and I am looking for a way to download that one file using the file extension. currently, I have the code to download multiple files from s3 bucket using key and then filter based on the extension. Something like this:
s3.listObjects(operaBucketName, keyName)
.getObjectSummaries()
.forEach(s -> keys.add(s.getKey()));
List<String> filteredKeys =
keys.stream().filter(s -> s.contains(extension)).collect(Collectors.toList());
//add to file list
List<File> files = new ArrayList<>();
for (String key : filteredKeys) {
File file = new File(FilenameUtils.getFullPath(localDirectory) + FilenameUtils.getName(key));
downloadFileFromS3(operaBucketName, key, file);
files.add(file);
}
I want to do this but targeting a single file. I tried s3.getObject(operaBucketName, keyName);
, but the com.amazonaws.services.s3.model.S3Object doesn't have a way to check the file extension. I think using this How to write an S3 object to a file? I can write S3object contents to file.
Also, just in case, there are multiple files in the given s3 folder(key), will getObject throw an exception?
Sdk version used:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.792</version>
</dependency>
Thanks!