Skip to content
Permalink
Browse files
JCLOUDS-346: Update Rackspace Cloud Files Examples
  • Loading branch information
Jeremy Daggett authored and Everett Toews committed Apr 21, 2014
1 parent 300d460 commit f9bdf4ca048d1db63fead0855347be6b98828c5b
Showing 14 changed files with 311 additions and 156 deletions.
@@ -133,15 +133,15 @@ Click Run and watch the Console for the output!

## Next Steps

Some suggestions.
Here are some suggestions on how to continue working with jclouds:

1. Change the examples to do different things that you want to do.
1. After running some examples, compare the output with what you see in the [Cloud Control Panel](https://mycloud.rackspace.com/).
1. Browse the [documentation](http://www.jclouds.org/documentation/) and have a look at the [Javadoc](http://demobox.github.com/jclouds-maven-site/latest/apidocs).
1. Return to the [Installation Guide](http://www.jclouds.org/documentation/userguide/installation-guide/) and have a look at the different ways to integrate jclouds with your project.
1. Join the [jclouds mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds) or maybe even the [jclouds developer mailing list](https://groups.google.com/forum/?fromgroups#!forum/jclouds-dev).
1. Browse the [JavaDocs](http://javadocs.jclouds.cloudbees.net/).
1. Return to the [Installation Guide](http://jclouds.apache.org/start/install/) and have a look at the different ways to integrate jclouds with your project.
1. Join the [user mailing list](http://www.mail-archive.com/user@jclouds.apache.org/) or [developer mailing list](http://www.mail-archive.com/dev@jclouds.apache.org/).

Welcome to the jclouds [community](http://www.jclouds.org/documentation/community/)!
Welcome to the jclouds [community](http://jclouds.apache.org/community/)!

## Support and Feedback

@@ -52,8 +52,8 @@
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>cloudfiles-us</artifactId>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>rackspace-cloudfiles-us</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
@@ -88,8 +88,8 @@
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>cloudfiles-uk</artifactId>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>rackspace-cloudfiles-uk</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
@@ -18,24 +18,40 @@
*/
package org.jclouds.examples.rackspace.cloudfiles;

import com.google.common.io.Closeables;
import org.jclouds.ContextBuilder;
import org.jclouds.cloudfiles.CloudFilesClient;
import org.jclouds.openstack.swift.domain.SwiftObject;

import java.io.*;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.CONTAINER_PUBLISH;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.FILENAME;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.PROVIDER;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.REGION;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.SUFFIX;

import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;

import static org.jclouds.examples.rackspace.cloudfiles.Constants.*;
import org.jclouds.ContextBuilder;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.openstack.swift.v1.features.ObjectApi;
import org.jclouds.openstack.swift.v1.options.CreateContainerOptions;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;
import org.jclouds.rackspace.cloudfiles.v1.features.CDNApi;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteSource;
import com.google.common.io.Closeables;
import com.google.common.io.Files;

/**
* This example will create a container, put a file in it, and publish it on the internet!
*/
public class CloudFilesPublish implements Closeable {
private final CloudFilesClient cloudFilesClient;
private final CloudFilesApi cloudFiles;

/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
* To get a username and API key see http://jclouds.apache.org/guides/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
@@ -57,9 +73,9 @@ public static void main(String[] args) throws IOException {
}

public CloudFilesPublish(String username, String apiKey) {
cloudFilesClient = ContextBuilder.newBuilder(PROVIDER)
cloudFiles = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(CloudFilesClient.class);
.buildApi(CloudFilesApi.class);
}

/**
@@ -69,7 +85,7 @@ public CloudFilesPublish(String username, String apiKey) {
private void createContainer() {
System.out.format("Create Container%n");

cloudFilesClient.createContainer(CONTAINER_PUBLISH);
cloudFiles.containerApiInRegion(REGION).createIfAbsent(CONTAINER_PUBLISH, CreateContainerOptions.NONE);

System.out.format(" %s%n", CONTAINER_PUBLISH);
}
@@ -86,12 +102,13 @@ private void createObjectFromFile() throws IOException {
BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
out.write("Hello Cloud Files");
out.close();

ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER_PUBLISH);

SwiftObject object = cloudFilesClient.newSwiftObject();
object.getInfo().setName(FILENAME + SUFFIX);
object.setPayload(tempFile);
ByteSource byteSource = Files.asByteSource(tempFile);
Payload payload = Payloads.newByteSourcePayload(byteSource);

cloudFilesClient.putObject(CONTAINER_PUBLISH, object);
objectApi.replace(FILENAME + SUFFIX, payload, ImmutableMap.<String, String>of());

System.out.format(" %s%s%n", FILENAME, SUFFIX);
}
@@ -102,8 +119,9 @@ private void createObjectFromFile() throws IOException {
*/
private void enableCdnContainer() {
System.out.format("Enable CDN Container%n");

URI cdnURI = cloudFilesClient.enableCDN(CONTAINER_PUBLISH);

CDNApi cdnApi = cloudFiles.cdnApiInRegion(REGION);
URI cdnURI = cdnApi.enable(CONTAINER_PUBLISH);

System.out.format(" Go to %s/%s%s%n", cdnURI, FILENAME, SUFFIX);
}
@@ -112,6 +130,6 @@ private void enableCdnContainer() {
* Always close your service when you're done with it.
*/
public void close() throws IOException {
Closeables.close(cloudFilesClient, true);
Closeables.close(cloudFiles, true);
}
}
@@ -22,12 +22,12 @@
* Constants used by the Rackspace Examples.
*
* @author Everett Toews
* @authot Jeremy Daggett
* @author Jeremy Daggett
*/
public interface Constants {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the system property or default value to "cloudfiles-uk"
public static final String PROVIDER = System.getProperty("provider.cf", "cloudfiles-us");
// To use the Rackspace Cloud (UK) set the system property or default value to "rackspace-cloudfiles-uk"
public static final String PROVIDER = System.getProperty("provider.cf", "rackspace-cloudfiles-us");
public static final String REGION = System.getProperty("region", "IAD");

public static final String CONTAINER_PUBLISH = "jclouds-example-publish";
@@ -20,13 +20,14 @@

import static org.jclouds.examples.rackspace.cloudfiles.Constants.CONTAINER;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.PROVIDER;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.REGION;

import java.io.Closeable;
import java.io.IOException;

import org.jclouds.ContextBuilder;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.options.CreateContainerOptions;
import org.jclouds.openstack.swift.v1.options.CreateContainerOptions;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.Closeables;
@@ -38,10 +39,10 @@
* @author Jeremy Daggett
*/
public class CreateContainer implements Closeable {
private final CommonSwiftClient swift;
private final CloudFilesApi cloudFiles;

/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
* To get a username and API key see http://jclouds.apache.org/guides/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
@@ -61,18 +62,18 @@ public static void main(String[] args) throws IOException {
}

public CreateContainer(String username, String apiKey) {
swift = ContextBuilder.newBuilder(PROVIDER)
cloudFiles = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(CommonSwiftClient.class);
.buildApi(CloudFilesApi.class);
}

private void createContainer() {
System.out.format("Create Container%n");

CreateContainerOptions options = CreateContainerOptions.Builder
.withMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"));
.metadata(ImmutableMap.of("key1", "value1", "key2", "value2"));

swift.createContainer(CONTAINER, options);
cloudFiles.containerApiInRegion(REGION).createIfAbsent(CONTAINER, options);

System.out.format(" %s%n", CONTAINER);
}
@@ -81,6 +82,6 @@ private void createContainer() {
* Always close your service when you're done with it.
*/
public void close() throws IOException {
Closeables.close(swift, true);
Closeables.close(cloudFiles, true);
}
}
@@ -20,22 +20,23 @@

import static org.jclouds.examples.rackspace.cloudfiles.Constants.CONTAINER;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.PROVIDER;
import static org.jclouds.examples.rackspace.cloudfiles.Constants.REGION;

import java.io.Closeable;
import java.io.IOException;
import java.util.Map;

import org.jclouds.ContextBuilder;
import org.jclouds.cloudfiles.CloudFilesClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.domain.ContainerMetadata;
import org.jclouds.openstack.swift.options.CreateContainerOptions;
import org.jclouds.openstack.swift.v1.domain.Container;
import org.jclouds.openstack.swift.v1.features.ContainerApi;
import org.jclouds.openstack.swift.v1.options.CreateContainerOptions;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.Closeables;

/**
* Create an Cloud Files container with Cross Origin Resource Sharing (CORS) allowed. CORS container headers allow
* Create a Cloud Files container with Cross Origin Resource Sharing (CORS) allowed. CORS container headers allow
* users to upload files from one website--or origin--to your Cloud Files account. When you set the CORS headers on
* your container, you tell Cloud Files which sites may post to your account, how often your container checks its
* allowed sites list, and whether or not metadata headers can be passed with the objects.
@@ -44,10 +45,11 @@
* @author Jeremy Daggett
*/
public class CrossOriginResourceSharingContainer implements Closeable {
private final CommonSwiftClient swift;
private final CloudFilesApi cloudFiles;
private final ContainerApi containerApi;

/**
* To get a username and API key see http://www.jclouds.org/documentation/quickstart/rackspace/
* To get a username and API key see http://jclouds.apache.org/guides/rackspace/
*
* The first argument (args[0]) must be your username
* The second argument (args[1]) must be your API key
@@ -68,9 +70,10 @@ public static void main(String[] args) throws IOException {
}

public CrossOriginResourceSharingContainer(String username, String apiKey) {
swift = ContextBuilder.newBuilder(PROVIDER)
cloudFiles = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(CloudFilesClient.class);
.buildApi(CloudFilesApi.class);
containerApi = cloudFiles.containerApiInRegion(REGION);
}

/**
@@ -88,13 +91,13 @@ private void createCorsContainer() {
"Access-Control-Allow-Origin", "*",
"Access-Control-Max-Age", "600",
"Access-Control-Allow-Headers", "X-My-Header");
CreateContainerOptions options = CreateContainerOptions.Builder.withMetadata(corsMetadata);
CreateContainerOptions options = CreateContainerOptions.Builder.metadata(corsMetadata);

swift.createContainer(CONTAINER, options);
containerApi.createIfAbsent(CONTAINER, options);
System.out.format(" %s%n", CONTAINER);
ContainerMetadata containerMetadata = swift.getContainerMetadata(CONTAINER);
System.out.format(" %s%n", containerMetadata.getMetadata());

Container container = containerApi.get(CONTAINER);
System.out.format(" %s%n", container.getMetadata());
}

/**
@@ -108,17 +111,17 @@ private void updateCorsContainer() {
"Access-Control-Max-Age", "60",
"Access-Control-Allow-Headers", "X-My-Other-Header");

swift.setContainerMetadata(CONTAINER, corsMetadata);
containerApi.updateMetadata(CONTAINER, corsMetadata);
System.out.format(" %s%n", CONTAINER);
ContainerMetadata containerMetadata = swift.getContainerMetadata(CONTAINER);
System.out.format(" %s%n", containerMetadata.getMetadata());

Container container = containerApi.get(CONTAINER);
System.out.format(" %s%n", container.getMetadata());
}

/**
* Always close your service when you're done with it.
*/
public void close() throws IOException {
Closeables.close(swift, true);
Closeables.close(cloudFiles, true);
}
}

0 comments on commit f9bdf4c

Please sign in to comment.