In this AWS Java SDK tutorial, I’d like to share with you some code examples for uploading files programmatically from local computer to a bucket on Amazon S3 server, with a Java console program, using AWS SDK for Java. In details, you will learn:

  • 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)

To follow this tutorial, you must have AWS SDK for Java installed for your Maven project.

Note: In the following code examples, the files are transferred directly from local computer to S3 server over HTTP.

 

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

Replace bucket-name, region-name and object-key by their actual values.

 

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:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.