From f7c937f71fa923c3ff88ea19614e67f2d85b8423 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Fri, 5 Jun 2009 08:24:02 +0000 Subject: [PATCH] Issue 40: simple example that uses s3 in a commandline environment. contributed by Cae Fernandez git-svn-id: http://jclouds.googlecode.com/svn/trunk@892 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../samples/createandlistbuckets/README.txt | 29 +++++ aws/s3/samples/createandlistbuckets/pom.xml | 79 ++++++++++++++ .../aws/s3/CreateListOwnedBuckets.java | 56 ++++++++++ .../org/jclouds/aws/s3/samples/MainApp.java | 98 +++++++++++++++++ ...CreateListOwnedBucketsIntegrationTest.java | 103 ++++++++++++++++++ .../test/CreateListOwnedBucketsLiveTest.java | 96 ++++++++++++++++ aws/s3/samples/pom.xml | 5 +- 7 files changed, 464 insertions(+), 2 deletions(-) create mode 100644 aws/s3/samples/createandlistbuckets/README.txt create mode 100644 aws/s3/samples/createandlistbuckets/pom.xml create mode 100644 aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/CreateListOwnedBuckets.java create mode 100644 aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/samples/MainApp.java create mode 100644 aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsIntegrationTest.java create mode 100644 aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsLiveTest.java diff --git a/aws/s3/samples/createandlistbuckets/README.txt b/aws/s3/samples/createandlistbuckets/README.txt new file mode 100644 index 0000000000..04f6257ec7 --- /dev/null +++ b/aws/s3/samples/createandlistbuckets/README.txt @@ -0,0 +1,29 @@ +==== + + Copyright (C) 2009 Global Cloud Specialists, Inc. + + ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + ==================================================================== +==== +# +# this is a simple example command line client that creates a bucket, then displays all buckets you own +# 1. execute 'mvn install' to build the sample +# 2. invoke the jar, passing your aws credentials and the bucket you wish to create +# ex. +# java -jar target/jclouds-s3-sample-createandlistbuckets-jar-with-dependencies.jar accesskey secretkey testbucketName diff --git a/aws/s3/samples/createandlistbuckets/pom.xml b/aws/s3/samples/createandlistbuckets/pom.xml new file mode 100644 index 0000000000..87d5618652 --- /dev/null +++ b/aws/s3/samples/createandlistbuckets/pom.xml @@ -0,0 +1,79 @@ + + + + + + jclouds-s3-samples-project + org.jclouds + 1.0-SNAPSHOT + ../pom.xml + + 4.0.0 + jclouds-s3-sample-createandlistbuckets + jclouds s3 sample that creates a bucket then lists all owned buckets + jclouds s3 sample that creates a bucket then lists all owned buckets + + + ${project.artifactId} + + + + org.apache.maven.plugins + maven-jar-plugin + + + + org.jclouds.aws.s3.samples.MainApp + + + + + + + maven-assembly-plugin + + + jar-with-dependencies + + + + org.jclouds.aws.s3.samples.MainApp + + + + + + make-assembly + package + + single + + + + + + + + + diff --git a/aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/CreateListOwnedBuckets.java b/aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/CreateListOwnedBuckets.java new file mode 100644 index 0000000000..97a51bd547 --- /dev/null +++ b/aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/CreateListOwnedBuckets.java @@ -0,0 +1,56 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.aws.s3; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.jclouds.aws.s3.domain.S3Bucket; + +/** + * CreateListOwnedBuckets is a class contaning operations to creates a bucket if it doesn't exist + * and lists all buckets owned by the user. + * + * @author Carlos Fernandes + */ +public class CreateListOwnedBuckets { + + S3Context s3Context; + + public CreateListOwnedBuckets(S3Context context) { + this.s3Context = context; + } + + public List list() throws InterruptedException, ExecutionException, + TimeoutException { + return s3Context.getConnection().listOwnedBuckets().get(10, TimeUnit.SECONDS); + } + + public Boolean createBucket(String bucketName) throws InterruptedException, ExecutionException, + TimeoutException { + return s3Context.getConnection().putBucketIfNotExists(bucketName).get(10, TimeUnit.SECONDS); + } +} diff --git a/aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/samples/MainApp.java b/aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/samples/MainApp.java new file mode 100644 index 0000000000..e1453e4d46 --- /dev/null +++ b/aws/s3/samples/createandlistbuckets/src/main/java/org/jclouds/aws/s3/samples/MainApp.java @@ -0,0 +1,98 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.aws.s3.samples; + +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +import javax.annotation.Resource; + +import org.jclouds.aws.s3.CreateListOwnedBuckets; +import org.jclouds.aws.s3.S3Context; +import org.jclouds.aws.s3.S3ContextFactory; +import org.jclouds.aws.s3.config.LiveS3ConnectionModule; +import org.jclouds.aws.s3.domain.S3Bucket; +import org.jclouds.logging.Logger; + +/** + * This the Main class of an Application that demonstrates the use of the CreateListOwnedBuckets + * class. + * + * Usage is: java MainApp \"accesskeyid\" \"secretekey\" \"bucketName\" + * + * @author Carlos Fernandes + */ +public class MainApp { + + @Resource + protected static Logger logger = Logger.NULL; + + public static int PARAMETERS = 3; + public static String INVALID_SYNTAX = "Invalid number of parameters. Syntax is: \"accesskeyid\" \"secretekey\" \"bucketName\" "; + + public static void main(String[] args) throws InterruptedException, ExecutionException, + TimeoutException { + + if (args.length < PARAMETERS) + throw new IllegalArgumentException(INVALID_SYNTAX); + + // Variables + S3Context context = null; + CreateListOwnedBuckets listMyOwnBuckets = null; + List myBuckets = null; + + // Args + String accesskeyid = args[0]; + String secretkey = args[1]; + String bucketName = args[2]; + + // Init + context = S3ContextFactory.createS3Context(accesskeyid, secretkey, + new LiveS3ConnectionModule()); + listMyOwnBuckets = new CreateListOwnedBuckets(context); + + try { + + // Create Bucket + listMyOwnBuckets.createBucket(bucketName); + + // List bucket + myBuckets = listMyOwnBuckets.list(); + + for (S3Bucket.Metadata bucketObj : myBuckets) { + System.out.println(String.format(" %1$s", bucketObj)); + System.out.println(String.format(": %1$s entries%n", context.createInputStreamMap( + bucketObj.getName()).size())); + } + + } finally { + // Close connecton + context.close(); + context = null; + } + + } + +} diff --git a/aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsIntegrationTest.java b/aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsIntegrationTest.java new file mode 100644 index 0000000000..829cabf1ec --- /dev/null +++ b/aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsIntegrationTest.java @@ -0,0 +1,103 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.aws.s3.samples.test; + +import java.util.concurrent.TimeUnit; + +import org.jclouds.aws.s3.CreateListOwnedBuckets; +import org.jclouds.aws.s3.S3Context; +import org.jclouds.aws.s3.S3ContextFactory; +import org.jclouds.aws.s3.config.LiveS3ConnectionModule; +import org.jclouds.aws.s3.config.StubS3ConnectionModule; +import org.jclouds.aws.s3.reference.S3Constants; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +/** + * Integration Unit test for simple CreateListOwnedBuckets. + * + * @author Carlos Fernandes + */ +@Test(testName = "s3.createListOwnedBucketsIntegrationTest") +public class CreateListOwnedBucketsIntegrationTest { + + private S3Context context; + private final String sysAWSAccessKeyId = System + .getProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID); + private final String sysAWSSecretAccessKey = System + .getProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY); + private String bucketPrefix = (System.getProperty("user.name") + "." + this.getClass() + .getSimpleName()).toLowerCase(); + + @BeforeClass(inheritGroups = false, groups = { "integration", "live" }) + @Parameters( { S3Constants.PROPERTY_AWS_ACCESSKEYID, S3Constants.PROPERTY_AWS_SECRETACCESSKEY }) + public void setUpTest(@Optional String AWSAccessKeyId, @Optional String AWSSecretAccessKey) { + + AWSAccessKeyId = AWSAccessKeyId != null ? AWSAccessKeyId : sysAWSAccessKeyId; + AWSSecretAccessKey = AWSSecretAccessKey != null ? AWSSecretAccessKey : sysAWSSecretAccessKey; + + if ((AWSAccessKeyId != null) && (AWSSecretAccessKey != null)) + context = S3ContextFactory.createS3Context(AWSAccessKeyId, AWSSecretAccessKey, + new LiveS3ConnectionModule()); + else + context = S3ContextFactory.createContext("stub", "stub").withHttpAddress("stub") + .withModule(new StubS3ConnectionModule()).build(); + } + + @Test(groups = { "integration", "live" }) + public void s3Test() throws Exception { + + // Init + CreateListOwnedBuckets listMyOwnBuckets = new CreateListOwnedBuckets(context); + String bucketName = bucketPrefix + "needstoexist"; + + // Create Bucket + assert listMyOwnBuckets.createBucket(bucketName); // Creates a + // random bucket + // first to + // make sure list() will return + // something. + + // List bucket + String string = listMyOwnBuckets.list().toString(); + assert string.length() > 0; // This test will validate if the list() operation will return any + // string + + } + + @AfterClass + public void tearDownClient() throws Exception { + + // Removes the bucket created for test purposes only + assert context.getConnection().deleteBucketIfEmpty(bucketPrefix + "needstoexist").get(10, + TimeUnit.SECONDS); + + context.close(); + context = null; + } + +} diff --git a/aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsLiveTest.java b/aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsLiveTest.java new file mode 100644 index 0000000000..b2206a1e9e --- /dev/null +++ b/aws/s3/samples/createandlistbuckets/src/test/java/org/jclouds/aws/s3/samples/test/CreateListOwnedBucketsLiveTest.java @@ -0,0 +1,96 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.aws.s3.samples.test; + +import java.util.concurrent.TimeUnit; + +import org.jclouds.aws.s3.CreateListOwnedBuckets; +import org.jclouds.aws.s3.S3Context; +import org.jclouds.aws.s3.S3ContextFactory; +import org.jclouds.aws.s3.config.LiveS3ConnectionModule; +import org.jclouds.aws.s3.reference.S3Constants; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Optional; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +/** + * Live Unit test for simple CreateListOwnedBuckets. + * + * @author Carlos Fernandes + */ +@Test(testName = "s3.createListOwnedBucketsLiveTest") +public class CreateListOwnedBucketsLiveTest { + + private S3Context context; + private final String sysAWSAccessKeyId = System + .getProperty(S3Constants.PROPERTY_AWS_ACCESSKEYID); + private final String sysAWSSecretAccessKey = System + .getProperty(S3Constants.PROPERTY_AWS_SECRETACCESSKEY); + private String bucketPrefix = (System.getProperty("user.name") + "." + this.getClass() + .getSimpleName()).toLowerCase(); + + @BeforeClass(inheritGroups = false, groups = { "live" }) + @Parameters( { S3Constants.PROPERTY_AWS_ACCESSKEYID, S3Constants.PROPERTY_AWS_SECRETACCESSKEY }) + public void setUpTest(@Optional String AWSAccessKeyId, @Optional String AWSSecretAccessKey) { + + AWSAccessKeyId = AWSAccessKeyId != null ? AWSAccessKeyId : sysAWSAccessKeyId; + AWSSecretAccessKey = AWSSecretAccessKey != null ? AWSSecretAccessKey : sysAWSSecretAccessKey; + + context = S3ContextFactory.createS3Context(AWSAccessKeyId, AWSSecretAccessKey, + new LiveS3ConnectionModule()); + + } + + @Test(groups = { "live" }) + public void s3Test() throws Exception { + + // Init + CreateListOwnedBuckets listMyOwnBuckets = new CreateListOwnedBuckets(context); + String bucketName = bucketPrefix + "needstoexist"; + + // Create Bucket + assert listMyOwnBuckets.createBucket(bucketName); // Creates a random bucket first to make + // sure list() will return something. + + // List bucket + String string = listMyOwnBuckets.list().toString(); + assert string.length() > 0; // This test will validate if the list() operation will return any + // string + + } + + @AfterClass + public void tearDownClient() throws Exception { + + // Removes the bucket created for test purposes only + context.getConnection().deleteBucketIfEmpty(bucketPrefix + "needstoexist").get(10, + TimeUnit.SECONDS); + + context.close(); + context = null; + } + +} diff --git a/aws/s3/samples/pom.xml b/aws/s3/samples/pom.xml index fe981f1bf3..fa65eaa05d 100644 --- a/aws/s3/samples/pom.xml +++ b/aws/s3/samples/pom.xml @@ -39,7 +39,8 @@ - + createandlistbuckets + ${project.groupId} @@ -60,4 +61,4 @@ test - \ No newline at end of file +