5/31/15

Image Upload to amazon S3 in Spring mvc

What is amazon s3:-




Amazon s3 is object storage service provided by Amazon under AWS. This service is cloud hosted so you don't need to worry about storage complexity and other hardware related things. 

Uploading Images to S3 using AWS Management Console
You can login to your aws management console and then in S3 section, you need to create a bucket. Then you can directly add your images to S3 bucket.


Uploading Images to S3 using automated script
Here i will describe how to upload image/any object to S3 while working with Spring MVC.

Step 1:- You must have a bucket where you will upload your s3 objects.


Step 2:-  Add AWS SDK dependency in your pom.xml file.

         <!-- For AWS -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.9.30</version>
</dependency>

Step 3:- In Spring MVC write a AWS Utility as given below


import java.io.File;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class AWSUtil {

  
 public String s3Upload(String imageName,String type)
 {
  String imageKey = "your key to access that object";
  String uploadPath = "give your local path to image "+imageName;
  String s3bucketName     = "your bucket name";
  
  AmazonS3 s3client = new AmazonS3Client(new BasicAWSCredentials("your aws key", "your aws secret"));
  try 
  {
            System.out.println("Uploading a new object to S3 from a file\n");
            File file = new File(uploadPath);
            s3client.putObject(new PutObjectRequest(
                               s3bucketName, imageKey, file).withCannedAcl(CannedAccessControlList.PublicRead));
            

         } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, which " +
              "means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which " +
              "means the client encountered " +
                    "an internal error while trying to " +
                    "communicate with S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
  
  return "https://s3-ap-southeast-1.amazonaws.com/" + s3bucketName + "/" + imageKey;
 }

}

Step 4:- Make object of this Utility class and call the s3Upload method with parameters you have.

You will get url to access your image/object in return. I have made import in this utility as public. You can also set your custom ACL for uploaded object.