private File retrieveS3File(String sqsdMessageBody) throws UnsupportedEncodingException {
    File localFile = null;

    if (!sqsdMessageBody.isEmpty()) {

      AmazonS3 s3 = new AmazonS3Client();

      List<S3EventNotificationRecord> records =
          S3EventNotification.parseJson(sqsdMessageBody).getRecords();

      S3EventNotificationRecord firstRecord = records.get(0);

      String bucketName = firstRecord.getS3().getBucket().getName();

      String objectRegion = firstRecord.getAwsRegion();
      Region s3Region = Region.getRegion(Regions.fromName(objectRegion));
      s3.setRegion(s3Region);

      // Object key may have spaces or unicode non-ASCII characters.
      String keyName = firstRecord.getS3().getObject().getKey().replace('+', ' ');
      keyName = URLDecoder.decode(keyName, "UTF-8");

      localFile = new File(keyName);

      System.out.println("Downloading file: " + objectRegion + "/" + bucketName + "/" + keyName);
      s3.getObject(new GetObjectRequest(bucketName, keyName), localFile);

      if (!localFile.canRead()) {
        localFile = null;
      }
    }
    return localFile;
  }
Ejemplo n.º 2
0
  private AmazonS3 getAmzS3Client() {

    BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyId, secretAccessKey);
    AmazonS3 s3Client = new AmazonS3Client(awsCreds);
    Region region = Region.getRegion(Regions.EU_WEST_1);
    s3Client.setRegion(region);

    return s3Client;
  }
 public S3Service() {
   // s3client = new AmazonS3Client(new
   // ProfileCredentialsProvider("enterprise-computing-ws16").getCredentials());
   s3client = new AmazonS3Client();
   s3client.setRegion(Region.getRegion(Regions.EU_WEST_1));
 }