Created
April 10, 2013 04:09
-
-
Save jeevanullas/5351730 to your computer and use it in GitHub Desktop.
Walrus Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.io.File; | |
import com.amazonaws.AmazonClientException; | |
import com.amazonaws.AmazonServiceException; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.PropertiesCredentials; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3Client; | |
import com.amazonaws.services.s3.model.Bucket; | |
import com.amazonaws.services.s3.S3ClientOptions; | |
import com.amazonaws.services.s3.model.S3Object; | |
import com.amazonaws.services.s3.model.ListObjectsRequest; | |
import com.amazonaws.services.s3.model.GetObjectRequest; | |
import com.amazonaws.services.s3.model.ObjectListing; | |
import com.amazonaws.services.s3.model.S3ObjectSummary; | |
import com.amazonaws.services.s3.model.ObjectMetadata; | |
public class WalrusTest { | |
/* The static variables, that would drive the show */ | |
static AmazonS3 s3; | |
static File fileToPut ; | |
static File fileToGet ; | |
static String bucketName ; | |
static S3ClientOptions s3ClientOptions; | |
private static void init() throws Exception { | |
AWSCredentials credentials = new PropertiesCredentials( | |
WalrusTest.class.getResourceAsStream("Credentials.properties")); | |
s3 = new AmazonS3Client(credentials); | |
s3ClientOptions = new S3ClientOptions(); | |
// s3.setEndpoint("s3.amazonaws.com"); | |
s3.setEndpoint("http://10.104.1.150:8773/services/Walrus"); | |
/* This part is required for Walrus , trying to use path-style bucket access */ | |
s3ClientOptions.setPathStyleAccess(true); | |
s3.setS3ClientOptions(s3ClientOptions); | |
fileToPut = new File("/Users/jeevanullas/WalrusTest/helloworld.txt"); | |
fileToGet = new File("/Users/jeevanullas/WalrusTest/helloworld-download.txt"); | |
bucketName = new String("mybucket-new"); | |
} | |
public static void main(String[] args) throws Exception { | |
/* A welcome message does not hurt your eyes */ | |
System.out.println("==========================================="); | |
System.out.println("Welcome to the AWS Java SDK!"); | |
System.out.println("==========================================="); | |
/* Initialize stuff before proceeding with the actual work */ | |
init(); | |
/* Create bucket */ | |
try { | |
Bucket mybucket = s3.createBucket(bucketName); | |
} catch (AmazonServiceException ase) { | |
System.out.println("Error creating bucket: "+bucketName); | |
ase.printStackTrace(); | |
System.out.println("Caught Exception: " + ase.getMessage()); | |
System.out.println("Reponse Status Code: " + ase.getStatusCode()); | |
System.out.println("Error Code: " + ase.getErrorCode()); | |
System.out.println("Request ID: " + ase.getRequestId()); | |
} | |
/* Upload a file to the bucket */ | |
try { | |
s3.putObject(bucketName, "helloworld.txt", fileToPut); | |
} catch (AmazonServiceException ase) { | |
System.out.println("Error uploading file to the bucket: "+bucketName); | |
ase.printStackTrace(); | |
System.out.println("Caught Exception: " + ase.getMessage()); | |
System.out.println("Reponse Status Code: " + ase.getStatusCode()); | |
System.out.println("Error Code: " + ase.getErrorCode()); | |
System.out.println("Request ID: " + ase.getRequestId()); | |
} | |
/* Upload a file using delimiters in the key */ | |
try { | |
s3.putObject(bucketName, "abc/def/helloworld.txt", fileToPut); | |
} catch (AmazonServiceException ase) { | |
System.out.println("Error uploading a file using key with delimiters\n"); | |
ase.printStackTrace(); | |
System.out.println("Caught Exception: " + ase.getMessage()); | |
System.out.println("Reponse Status Code: " + ase.getStatusCode()); | |
System.out.println("Error Code: " + ase.getErrorCode()); | |
System.out.println("Request ID: " + ase.getRequestId()); | |
} | |
/* Download a file from the bucket to local filesystem */ | |
try { | |
ObjectMetadata objectMetadata = s3.getObject(new GetObjectRequest(bucketName, "helloworld.txt"), fileToGet); | |
} catch (AmazonServiceException ase) { | |
System.out.println("Error download file from the bucket\n"); | |
ase.printStackTrace(); | |
System.out.println("Caught Exception: " + ase.getMessage()); | |
System.out.println("Reponse Status Code: " + ase.getStatusCode()); | |
System.out.println("Error Code: " + ase.getErrorCode()); | |
System.out.println("Request ID: " + ase.getRequestId()); | |
} | |
/* Listing keys */ | |
ObjectListing objectListing; | |
ListObjectsRequest listObjectsRequest = new ListObjectsRequest(); | |
listObjectsRequest.withBucketName(bucketName); | |
listObjectsRequest.withPrefix("abc/"); | |
do { | |
objectListing = s3.listObjects(listObjectsRequest); | |
for (S3ObjectSummary objectSummary : | |
objectListing.getObjectSummaries()) { | |
System.out.println( " - " + objectSummary.getKey() + " " + | |
"(size = " + objectSummary.getSize() + | |
")"); | |
} | |
listObjectsRequest.setMarker(objectListing.getNextMarker()); | |
} while (objectListing.isTruncated()); | |
/* List all buckets in the cloud */ | |
try { | |
List<Bucket> buckets = s3.listBuckets(); | |
System.out.println("List of all buckets in your cloud:\n"); | |
for (Bucket bucket : buckets) { | |
System.out.println(bucket.getName()+"\n"); | |
} | |
} catch (AmazonServiceException ase) { | |
ase.printStackTrace(); | |
System.out.println("Error Message: " + ase.getMessage()); | |
System.out.println("HTTP Status Code: " + ase.getStatusCode()); | |
System.out.println("AWS Error Code: " + ase.getErrorCode()); | |
System.out.println("Error Type: " + ase.getErrorType()); | |
System.out.println("Request ID: " + ase.getRequestId()); | |
} catch (AmazonClientException ace) { | |
System.out.println("Error Message: " + ace.getMessage()); | |
} | |
/* TODO: Delete all the objects created in this code */ | |
// s3.deleteObject(bucketName,"helloworld.txt"); | |
/* TODO: Delete the bucket created in this code */ | |
// s3.deleteBucket(bucketName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment