2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Upload image to amazon s3 using java

Overview

  • AWS Simple Storage Service(S3), which allows you to process your entire storage infrastructure and give a better scalability, reliability, and speed than just storing files on the file system.
  • This article will give the basic flow and understanding of Image upload using S3 in a simpler way with required code snippet. Hope it will helpful to upload an image into S3 in an easiest way. 
Prerequisite 

Before proceeding with this please make sure the Access Key, Secret Key, Bucket Name and the Amazan S3 HTTP path to upload an image in the specified bucket are available with you. And you will get all these things once you purchase your S3 service license from Amazon.


Dependencies

    aws-java-sdk-1.0.002

    aws-java-sdk-1.7.13

    aws-java-sdk-core-1.9.0

    commons-codec-1.6

    commons-httpclient-3.1


Code snippet to upload an image into Amazon S3 server

Spring Controller

@RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
@ResponseBody
public String uploadImage(@RequestParam("file") MultipartFile file,
    HttpServletRequest request){
        if (!file.isEmpty()) {
            filePath = ImageS3Util.uploadImageToAWSS3(file);
        }
    return "view page";
}


Java Utility Class

import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
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.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class ImageS3Util {
    private static final String ACCESS_KEY = "xxx";
    private static final String SECRET_KEY = "xxx";
    private static final String END_POINT_URL = "xxx";// e.g http://s3.amazonaws.com
    private static final String BUCKET = "xxx";
    private static final String IMAGE_LOCATION = "xxx";
    private static final String S3_CACHE = "xxx"; // e.g 60
    private static AmazonS3 s3;
    public static void uploadImageToAWSS3(MultipartFile multipartFile) throws IllegalStateException,
               IOException {	String fileName = null;
        try {
            AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
            java.security.Security.setProperty("networkaddress.cache.ttl", S3_CACHE);
            s3 = new AmazonS3Client(credentials);	s3.setEndpoint(END_POINT_URL);
            InputStream stream = multipartFile.getInputStream();
            fileName = System.currentTimeMillis() + "_" + fileName;
            ObjectMetadata objectMetadata = new ObjectMetadata();
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET, IMAGE_LOCATION + "/" + fileName, stream,objectMetadata);
            //skip if do not want to access the image directly from S3
            putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead); 
            //skip if do not want to access the image directly from S3
            s3.putObject(putObjectRequest);
        	} catch (AmazonServiceException e) {
                e.printStackTrace()
        }
    }
}


The uploaded image you can view on your given aws S3 http path or else can able to directly access the stored image like, http://[bucket].s3.amazonaws.com/[imagelocation]/[imagename]


For more info please visit aws official site  AWS S3 Image Upload  to enhance the utility class as per your requirement. And also feel free to add your queries on comment box if any.


Thank You for reading this post