package net.codejava.aws; import java.io.File; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRequest; public class UploadFileExample1 { public static void main(String[] args) { String bucketName = "codejava-bucket"; String fileName = "Java Logo.png"; String filePath = "D:/Images/" + fileName; S3Client client = S3Client.builder().build(); PutObjectRequest request = PutObjectRequest.builder() .bucket(bucketName).key(fileName).build(); client.putObject(request, RequestBody.fromFile(new File(filePath))); } }The code is self-explanatory - quite simple, right? The file is stored as an object in the given bucket, with object key is the file name. If you want to put the file in a “folder”, specify the key something like this:
.bucket(bucketName).key("programming/java/" + fileName).build();Also note that by default, the uploaded file is not accessible by public users. And the program terminates quickly as the operation is asynchronous.
PutObjectRequest request = PutObjectRequest.builder() .bucket(bucketName) .key(fileName) .acl("public-read").build();Then you can use web browser to access the file using the following URL pattern:
https://bucket-name.s3.region-name.amazonaws.com/object-key
PutObjectRequest request = PutObjectRequest.builder() .bucket(bucketName) .key(key) .acl("public-read") .contentType("image/png") .build();The other methods are contentDisposition(), contentEncoding(), contentLanguage(), contentLength()…
package net.codejava.aws; import java.io.File; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; import software.amazon.awssdk.services.s3.model.HeadObjectResponse; import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.waiters.S3Waiter; public class UploadFileExample3 { public static void main(String[] args) { String bucketName = "codejava-bucket"; String folderName = "photos"; String fileName = "Java Logo.png"; String filePath = "D:/Images/" + fileName; String key = folderName + "/" + fileName; S3Client client = S3Client.builder().build(); PutObjectRequest request = PutObjectRequest.builder() .bucket(bucketName) .key(key) .acl("public-read") .build(); client.putObject(request, RequestBody.fromFile(new File(filePath))); S3Waiter waiter = client.waiter(); HeadObjectRequest requestWait = HeadObjectRequest.builder().bucket(bucketName).key(key).build(); WaiterResponse<HeadObjectResponse> waiterResponse = waiter.waitUntilObjectExists(requestWait); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("File " + fileName + " was uploaded."); } }You see, the method call waitUntilObjectExists() cause the program to wait until the file actually uploaded to S3. Then you can run your logic afterward.Those are some code examples about uploading files directly from local computer to a bucket on Amazon S3 server, using AWS SDK for Java. To see the coding in action, I recommend you watch the following video: