Skip to content

Commit

Permalink
Added file clean up for uploaded files
Browse files Browse the repository at this point in the history
  • Loading branch information
This-Is-Ko committed Oct 8, 2023
1 parent 928353d commit 6cf4a15
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ public void uploadtoS3(InstagramPost post) {
// Overwrites any file with the same name
for (String imageFileName : post.getImagesFileNames()) {
String filePath = imageGeneratorProperies.getOutputPath() + imageFileName;
PutObjectRequest request = new PutObjectRequest(amazonS3Properties.getBucketName(),imageFileName, new File(filePath))
File file = new File(filePath);
PutObjectRequest request = new PutObjectRequest(amazonS3Properties.getBucketName(),imageFileName, file)
.withCannedAcl(CannedAccessControlList.PublicRead);
s3Client.putObject(request);
String imageUrl = s3Client.getUrl(amazonS3Properties.getBucketName(), imageFileName).toString();
log.atInfo().setMessage(post.getPlayer().getName() + " - Successfully uploaded image " + imageFileName + " to S3 @ " + imageUrl).log();
post.getImagesS3Urls().add(imageUrl);

cleanUpFile(file);
}
} catch (AmazonServiceException ex) {
// The call was transmitted successfully, but Amazon S3 couldn't process
Expand All @@ -57,4 +60,13 @@ public void uploadtoS3(InstagramPost post) {
log.atInfo().setMessage(post.getPlayer().getName() + " - No images to upload").log();
}
}

// Delete files uploaded to S3
private void cleanUpFile(File file) {
if (file.delete()) {
log.info("Deleted file: " + file.getAbsolutePath());
} else {
log.warn("Unable to delete file: " + file.getAbsolutePath());
}
}
}

0 comments on commit 6cf4a15

Please sign in to comment.