Wednesday, September 3, 2014

Uploading and deleting contents to Amazon S3 using REST Api


Following Jar files are required to use the REST Services:

  1. jets3t-0.8.1a.jar
  2. commons-httpclient-3.1.jar
I have created following utility class to upload and delete the contents to/from Amazon S3 cloud.

_________________________________________________________________________________

package com.abhinav.s3.restutil;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

/**
 * The Class S3RESTService.<br/>
 * This class can be used to manually store and delete contents from the s3 bucket.
 *
 * @author Abhinav kumar mishra
 */
public class S3RESTService {

/** The bucket name. */
private final String bucketName;

/** The s3Service. */
private S3Service s3Service;

/** The bucket. */
private S3Bucket bucket;

/**
* Instantiates a new rEST service.
*/
public S3RESTService(final String accessKey, final String secretKey, final String bucketName) {

//Amazon Web Services BucketName
this.bucketName = bucketName;

System.out.println("S3RESTService Initializing: accessKey="+accessKey
+ "secretKey="+secretKey+" bucketName="+bucketName);

// Instantiate S3 Service and create necessary bucket.
try {
s3Service = new RestS3Service(new AWSCredentials(accessKey, secretKey));
bucket = s3Service.getOrCreateBucket(bucketName);
System.out.println("S3RESTService Initialization Complete");
} catch (S3ServiceException s3ServExcp) {
System.err.println("S3RESTService Initialization Error: "
+ s3ServExcp);
}
}

/**
* Put object.
*
* @param fileName the file name
* @return true, if successful
* @throws S3ServiceException the s3 service exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public boolean putObject(final String fileName) throws S3ServiceException,
NoSuchAlgorithmException, IOException {
final S3Object obj = s3Service.putObject(bucket, new S3Object(new File(fileName)));
return obj.getDataInputFile().exists();
}

/**
* Put object.
*
* @param fileName the file name
* @return true, if successful
* @throws S3ServiceException the s3 service exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public boolean putObject(final File fileName) throws S3ServiceException,
NoSuchAlgorithmException, IOException {
final S3Object obj = s3Service.putObject(bucket, new S3Object(fileName));
return obj.getDataInputFile().exists();
}

/**
* Delete object.
*
* @param fileName the file name
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws ServiceException the service exception
*/
public void deleteObject(final String fileName)
throws NoSuchAlgorithmException, IOException, ServiceException {
s3Service.deleteObject(bucketName,new S3Object(new File(fileName)).getKey());
}

/**
* Delete object.
*
* @param fileName the file name
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws ServiceException the service exception
*/
public void deleteObject(final File fileName)
throws NoSuchAlgorithmException, IOException, ServiceException {
s3Service.deleteObject(bucketName, new S3Object(fileName).getKey());
}
}

-------------------------------------------------------------------------------------------------------------

//Test

public class TestS3RESTService{

public static void main(String[] args) throws IOException {
try {
// Create the instance of the S3RESTService by providing
// the accessKey,secretKey and bucketName.
S3RESTService restServ = new S3RESTService("xxxx", "xxx","myBucket");
// CALL TO UPLOAD DATA
restServ.putObject("C:\\data\\test.txt");
// CALL TO DELETE DATA
restServ.deleteObject("C:\\data\\test.txt");
} catch (ServiceException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

}

No comments:

Post a Comment

Thanks for your comments/Suggestions.