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
This commit is contained in:
adrian.f.cole 2009-06-05 08:24:02 +00:00
parent 98fe4e9955
commit f7c937f71f
7 changed files with 464 additions and 2 deletions

View File

@ -0,0 +1,29 @@
====
Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
====================================================================
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

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
$HeadURL$ $Revision$ $Date$ Copyright (C) 2009 Adrian Cole
<adrian@jclouds.org>
====================================================================
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.html 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.
====================================================================
-->
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<artifactId>jclouds-s3-samples-project</artifactId>
<groupId>org.jclouds</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jclouds-s3-sample-createandlistbuckets</artifactId>
<name>jclouds s3 sample that creates a bucket then lists all owned buckets</name>
<description>jclouds s3 sample that creates a bucket then lists all owned buckets</description>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.jclouds.aws.s3.samples.MainApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.jclouds.aws.s3.samples.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,56 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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<S3Bucket.Metadata> 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);
}
}

View File

@ -0,0 +1,98 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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<S3Bucket.Metadata> 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;
}
}
}

View File

@ -0,0 +1,103 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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;
}
}

View File

@ -0,0 +1,96 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* 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;
}
}

View File

@ -39,7 +39,8 @@
<!-- as this has a dependency on something you can't get from maven, excluding
<module>googleappengine</module>
-->
</modules>
<module>createandlistbuckets</module>
</modules>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
@ -60,4 +61,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>