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:
parent
f4d0dfcbbe
commit
5bae0396c0
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.s3;
|
package com.baeldung.s3;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.amazonaws.services.s3.AmazonS3;
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
@ -49,6 +50,11 @@ public class AWSS3Service {
|
|||||||
public PutObjectResult putObject(String bucketName, String key, File file) {
|
public PutObjectResult putObject(String bucketName, String key, File file) {
|
||||||
return s3client.putObject(bucketName, key, 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
|
//listing objects
|
||||||
public ObjectListing listObjects(String bucketName) {
|
public ObjectListing listObjects(String bucketName) {
|
||||||
|
@ -66,6 +66,9 @@ public class S3Application {
|
|||||||
new File("/Users/user/Document/hello.txt")
|
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
|
//listing objects
|
||||||
ObjectListing objectListing = awsService.listObjects(bucketName);
|
ObjectListing objectListing = awsService.listObjects(bucketName);
|
||||||
for(S3ObjectSummary os : objectListing.getObjectSummaries()) {
|
for(S3ObjectSummary os : objectListing.getObjectSummaries()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user