Upload File to S3 using AWS Java SDK - Java Console Program
- Details
- Written by Nam Ha Minh
- Last Updated on 03 January 2022   |   Print Email
- Upload a file to S3 bucket with default permission
- Upload a file to S3 bucket with public read permission
- Wait until the file exists (uploaded)
1. Upload File to S3 Bucket with Default Permission
The following code example uploads an image file to a S3 bucket in your AWS account’s default region, with default permission (owner has read-write permission; public users do not have permission):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.
2. Upload File to S3 Bucket with Public Read Permission
In case you want to give read permission for public users, i.e. the file is accessible by visitors using web browser, you can specify the public-read permission when creating a new request like this: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
3. Set additional information for upload file
You can use the contentXXX() methods of the PutObjectRequest class to specify additional information for the file stored on S3. For example, the following code set content type of the file to be “image/png” for the file:PutObjectRequest request = PutObjectRequest.builder() .bucket(bucketName) .key(key) .acl("public-read") .contentType("image/png") .build();The other methods are contentDisposition(), contentEncoding(), contentLanguage(), contentLength()…
4. Wait Until the File Exists (Uploaded)
By default, the file upload operation is asynchronous. If you want to wait until the file exists (uploaded) in order to run some custom logics that depend on the existence of the file, use a S3Waiter as shown in the following code example: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:
Related AWS Java SDK Tutorials:
- How to Generate AWS Access Key ID and Secret Access Key
- How to setup AWS SDK for Java for Amazon S3 Development
- AWS Java SDK S3 List Buckets Example
- AWS Java SDK S3 List Objects Examples
- AWS Java SDK S3 Create Bucket Examples
- AWS Java SDK S3 Create Folder Examples
- Upload File to S3 using AWS Java SDK - Java Servlet JSP Web App
- Spring Boot File Upload to Amazon S3 Example
- AWS Java SDK Download File from S3 Example
- AWS Java SDK S3 Delete Objects Examples
- AWS Java SDK S3 Delete Buckets Examples