Wednesday, July 1, 2015

Java code to read and write objects in AWS S3


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import org.apache.log4j.Logger;
import amazon.odin.awsauth.OdinAWSCredentialsProvider;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.S3Object;

public class S3Utils {
 public static final String MATERIAL_SET = "<material_set_name>";
 public static final String BUCKET = "<bucket_name>";
 public static final Integer S3_URL_VALIDITY_PERIOD_MILLISEC = 1000*60*60; // S3 url should be valid for 1 hour.
 private static final Logger logger = Logger.getLogger(S3Utils.class);
 private static final String folder = "<folder_name>";
 
 public static String writeToS3(File file, String folder) {
  com.amazonaws.services.s3.AmazonS3 s3Client = new com.amazonaws.services.s3.AmazonS3Client(
    getAWSCredentials());
  String key = file.getName();
  System.out.println("Going to write to key " + folder + key + " in S3");
  s3Client.putObject(BUCKET, folder + key, file);
  
  //This generates a URL that can be used to download the uploaded file.
  Date expiration = new Date();
  long msec = expiration.getTime();
  msec += S3_URL_VALIDITY_PERIOD_MILLISEC;
  expiration.setTime(msec);
  GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(BUCKET, folder + key);
  generatePresignedUrlRequest.setMethod(HttpMethod.GET);
  generatePresignedUrlRequest.setExpiration(expiration);
  URL s = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
  if(s == null) {
   System.out.println("Url returned was null");
   return null;
  }
  System.out.println("S3 Url: " + s.toString());
  return s.toString();
 }
 
 public static String readFromS3(String folder, String key) {
  StringBuilder sb = new StringBuilder();
  com.amazonaws.services.s3.AmazonS3 s3Client = new com.amazonaws.services.s3.AmazonS3Client(
    getAWSCredentials());
  
  S3Object s3object = s3Client.getObject(BUCKET, folder + key);
  InputStream is = s3object.getObjectContent();
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  
  String line;
  try {
   while((line = br.readLine()) != null) {
    System.out.println(line);
    sb.append(line);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return sb.toString();
 }
 
 private static AWSCredentials getAWSCredentials() {
  AWSCredentialsProvider credentialsProvider = new OdinAWSCredentialsProvider(MATERIAL_SET);
     AWSCredentials credentials = credentialsProvider.getCredentials();
        return credentials;
    }
}

1 comment: