Oct 24, 2013

AWS SDK for Java - Tutorial 4 : S3

Introduction
Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, secure, fast, inexpensive infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.

Useful Classe
AmazonS3Client

Source Code
Step 1: Variables requires to create S3 bucket
 // S3 
 private String BucketName = "aws-tutorials";
 private String BucketPolicy ="{"Version": "2008-10-17","Statement": [{"Sid": "AddPerm","Effect": "Allow","Principal": {"AWS": "*"},"Action": "s3:GetObject","Resource": "arn:aws:s3:::aws-tutorials/*"}]}";



Step 2: Create s3 bucket with policy
public void createS3(){

  try {
   AmazonS3Client s3 = new AmazonS3Client(credentials);
   s3.setEndpoint(endPoint);
   s3.setRegion(region);
   log.Info("Creating Bucket :" + BucketName);
   
   s3.createBucket(BucketName);
   log.Info("Policy :" + BucketPolicy);
   s3.setBucketPolicy(BucketName, BucketPolicy);
   
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(0);
  }
 }


Screenshots


Click here to download full source