Upload file to S3 with Spring

This article I’ll show you how to create program with spring and upload file to amazon S3.

Access Key ID & Secret Access Key

Before we start, we need 2 pieces of information from your aws console. You can obtain this from your amazon dashboard.

From the top right, click your name, select security and credentials and click continue to security credentials. Expand theĀ Access Keys (Access Key ID and Secret Access Key). Click create new access key and will be download a csv file called rootkey.csv.

Create a bucket

You can create a bucket from the coding, but in this demo I’ll focus to upload file. Therefore I’ll create bucket name it as yourfoldername.

Screen Shot 2015-08-24 at 2.25.28 PM


In order to upload file into S3, you’ll need to add the amazon sdk into your dependency.

<!-- AWS -->


ApplicationContext (JavaBased-Config)

Let’s spring manage AWS Object and it’s lifecycle. In this application context I’ve define 2 beans, which is AWSCredentials and AmazonS3 object.

@ComponentScan(basePackageClasses = Application.class, excludeFilters = @Filter({Controller.class, Configuration.class}))
public class ApplicationConfig {

   private String awsId;
   private String awsKey;
   public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
	PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	ppc.setLocations(new Resource[] {
		new ClassPathResource("/amazon.properties")
	return ppc;
   public AWSCredentials credential() {
   	return new BasicAWSCredentials(awsId, awsKey);
   public AmazonS3 s3client() {
 	return new AmazonS3Client(credential()); 


Properties file

Create a amazon.properties file and put it under your classpath. Copy the previous download content from your rootkey.csv into this properties file.



Create a AswS3Service

Create a service interface with the following methods

public interface AwsS3Service {

    public void uploadFile(String uploadFile, String fileName);


Create Service Implementation

public class AwsS3ServiceImpl implements AwsS3Service {

   private AmazonS3 s3client;
   private String nameCardBucket;
    * upload file to folder and set it to public
    public void uploadFile(String uploadFile, String filename) {
	String fileNameInS3 = filename;
		new PutObjectRequest(nameCardBucket, 
			fileNameInS3, new File(uploadFile))


Test Your Code

public class S3Test {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    AwsS3Service service = ctx.getBean(AwsS3Service.class);
    String uploadFile = "/Users/mingch/Documents/test2.txt";
    String fileName = "test2.txt";
    service.uploadFile(uploadFile, fileName);



Screen Shot 2015-08-24 at 2.48.30 PM


The code show you a simple way to upload file to S3, but you can enhance it like upload into a folder within the bucket or set security level and etc.


Source Code

Download source code

Upload file to S3 with Spring

3 thoughts on “Upload file to S3 with Spring

  • January 17, 2017 at 8:19 pm


    Thank you very much for you post.

    Would you please provide the source code of the project.

    Thank you.

    • January 18, 2017 at 1:05 am

      Source code added.

  • January 26, 2018 at 8:21 am

    Thanks lot you mad my day.. Your post amazing and well structured


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.