This page helps you get started with jclouds
API with Amazon Web Services
org.apache.jclouds.provider/aws-ec2
and org.apache.jclouds.provider/aws-s3
using jclouds Installation.org.apache.jclouds.driver/jclouds-sshj
and org.apache.jclouds.driver/jclouds-sl4j
.{% highlight java %} import static org.jclouds.aws.s3.options.PutObjectOptions.Builder.withAcl;
// get a context with amazon that offers the portable BlobStore API BlobStoreContext context = ContextBuilder.newBuilder(“aws-s3”) .credentials(accesskeyid, secretkey) .buildView(BlobStoreContext.class);
// create a container in the default location BlobStore blobStore = context.getBlobStore(); blobStore.createContainerInLocation(null, bucket);
// add blob Blob blob = blobStore.newBlob(“test”); blob.setPayload(“test data”); blobStore.putBlob(bucket, blob);
// when you need access to s3-specific features, // use the provider-specific context AWSS3Client s3Client = AWSS3Client.class.cast(context.getProviderSpecificContext().getApi());
// make the object world readable String publicReadWriteObjectKey = “public-read-write-acl”; S3Object object = s3Client.newS3Object();
object.getMetadata().setKey(publicReadWriteObjectKey); object.setPayload(“hello world”); s3Client.putObject(bucket, object, withAcl(CannedAccessPolicy.PUBLIC_READ));
context.close(); {% endhighlight %}
{% highlight java %} // get a context with ec2 that offers the portable ComputeService API ComputeServiceContext context = ContextBuilder.newBuilder(“aws-ec2”) .credentials(accesskeyid, secretkey) .modules(ImmutableSet. of(new Log4JLoggingModule(), new SshjSshClientModule())) .buildView(ComputeServiceContext.class);
// here's an example of the portable api Set<? extends Location> locations = context.getComputeService().listAssignableLocations();
Set<? extends Image> images = context.getComputeService().listImages();
// pick the highest version of the RightScale CentOS template Template template = context.getComputeService().templateBuilder().osFamily(OsFamily.CENTOS).build();
// specify your own groups which already have the correct rules applied template.getOptions().as(AWSEC2TemplateOptions.class).securityGroups(group1);
// specify your own keypair for use in creating nodes template.getOptions().as(AWSEC2TemplateOptions.class).keyPair(keyPair);
// run a couple nodes accessible via group Set<? extends NodeMetadata> nodes = context.getComputeService().createNodesInGroup(“webserver”, 2, template);
// when you need access to very ec2-specific features, use the provider-specific context AWSEC2Client ec2Client = AWSEC2Client.class.cast(context.getProviderSpecificContext().getApi());
// ex. to get an ip and associate it with a node NodeMetadata node = Iterables.get(nodes, 0); String ip = ec2Client.getElasticIPAddressServices().allocateAddressInRegion(node.getLocation().getId()); ec2Client.getElasticIPAddressServices().associateAddressInRegion(node.getLocation().getId(),ip, node.getProviderId());
context.close(); {% endhighlight %}