From 5bae0396c0537903372a615ac7d0929ce8939f50 Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Sat, 1 Jul 2023 13:23:45 +0000 Subject: [PATCH] Upload file and get its URL from Amazon S3 (#14317) * Upload file and get its URL from Amazon S3 * Upload file and get its URL from Amazon S3 * Upload file and get its URL from Amazon S3 --- .../aws-s3/src/main/java/com/baeldung/s3/AWSS3Service.java | 6 ++++++ .../aws-s3/src/main/java/com/baeldung/s3/S3Application.java | 3 +++ 2 files changed, 9 insertions(+) diff --git a/aws-modules/aws-s3/src/main/java/com/baeldung/s3/AWSS3Service.java b/aws-modules/aws-s3/src/main/java/com/baeldung/s3/AWSS3Service.java index 792e41a188..64ed043f98 100644 --- a/aws-modules/aws-s3/src/main/java/com/baeldung/s3/AWSS3Service.java +++ b/aws-modules/aws-s3/src/main/java/com/baeldung/s3/AWSS3Service.java @@ -1,6 +1,7 @@ package com.baeldung.s3; import java.io.File; +import java.net.URL; import java.util.List; import com.amazonaws.services.s3.AmazonS3; @@ -49,6 +50,11 @@ public class AWSS3Service { public PutObjectResult putObject(String bucketName, String key, File file) { return s3client.putObject(bucketName, key, file); } + //uploading object and getting url + public URL getObjectURL(String bucketName, String key, File file) { + s3client.putObject(bucketName, key, file); + return s3client.getUrl(bucketName, key); + } //listing objects public ObjectListing listObjects(String bucketName) { diff --git a/aws-modules/aws-s3/src/main/java/com/baeldung/s3/S3Application.java b/aws-modules/aws-s3/src/main/java/com/baeldung/s3/S3Application.java index fdfb909f73..39a9b0050f 100644 --- a/aws-modules/aws-s3/src/main/java/com/baeldung/s3/S3Application.java +++ b/aws-modules/aws-s3/src/main/java/com/baeldung/s3/S3Application.java @@ -66,6 +66,9 @@ public class S3Application { new File("/Users/user/Document/hello.txt") ); + //uploading object and getting url + awsService.getObjectURL(bucketName, "Document/hello.txt", new File("/Users/user/Document/hello.txt")); + //listing objects ObjectListing objectListing = awsService.listObjects(bucketName); for(S3ObjectSummary os : objectListing.getObjectSummaries()) {