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
This commit is contained in:
Michael Olayemi 2023-07-01 13:23:45 +00:00 committed by GitHub
parent f4d0dfcbbe
commit 5bae0396c0
2 changed files with 9 additions and 0 deletions

View File

@ -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) {

View File

@ -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()) {