From f9bdf4ca048d1db63fead0855347be6b98828c5b Mon Sep 17 00:00:00 2001 From: Jeremy Daggett Date: Sun, 30 Mar 2014 23:06:19 -0700 Subject: [PATCH] JCLOUDS-346: Update Rackspace Cloud Files Examples --- rackspace/README.md | 10 +-- rackspace/pom.xml | 8 +- .../cloudfiles/CloudFilesPublish.java | 56 ++++++++---- .../rackspace/cloudfiles/Constants.java | 6 +- .../rackspace/cloudfiles/CreateContainer.java | 19 ++-- .../CrossOriginResourceSharingContainer.java | 41 +++++---- .../cloudfiles/DeleteObjectsAndContainer.java | 40 +++++---- .../rackspace/cloudfiles/GenerateTempURL.java | 15 +++- .../rackspace/cloudfiles/GetObject.java | 89 +++++++++++++++++++ .../rackspace/cloudfiles/ListContainers.java | 23 +++-- .../rackspace/cloudfiles/ListObjects.java | 38 ++++---- .../cloudfiles/UploadDirectoryToCDN.java | 32 ++++--- .../cloudfiles/UploadLargeObject.java | 46 ++++++---- .../rackspace/cloudfiles/UploadObjects.java | 44 +++++---- 14 files changed, 311 insertions(+), 156 deletions(-) create mode 100644 rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GetObject.java diff --git a/rackspace/README.md b/rackspace/README.md index ad2e876..efbeee4 100644 --- a/rackspace/README.md +++ b/rackspace/README.md @@ -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 diff --git a/rackspace/pom.xml b/rackspace/pom.xml index 7efe241..c8eb7a8 100644 --- a/rackspace/pom.xml +++ b/rackspace/pom.xml @@ -52,8 +52,8 @@ ${jclouds.version} - org.apache.jclouds.provider - cloudfiles-us + org.apache.jclouds.labs + rackspace-cloudfiles-us ${jclouds.version} @@ -88,8 +88,8 @@ ${jclouds.version} - org.apache.jclouds.provider - cloudfiles-uk + org.apache.jclouds.labs + rackspace-cloudfiles-uk ${jclouds.version} diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CloudFilesPublish.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CloudFilesPublish.java index dcc5bd6..f788968 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CloudFilesPublish.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CloudFilesPublish.java @@ -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.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); } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/Constants.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/Constants.java index 0d58332..77b1434 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/Constants.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/Constants.java @@ -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"; diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CreateContainer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CreateContainer.java index b280df1..0b5766d 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CreateContainer.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CreateContainer.java @@ -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); } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CrossOriginResourceSharingContainer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CrossOriginResourceSharingContainer.java index 57ad605..08280a5 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CrossOriginResourceSharingContainer.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/CrossOriginResourceSharingContainer.java @@ -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); } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/DeleteObjectsAndContainer.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/DeleteObjectsAndContainer.java index 82682d2..ed12560 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/DeleteObjectsAndContainer.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/DeleteObjectsAndContainer.java @@ -20,17 +20,19 @@ 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.Set; +import java.util.List; 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.domain.ObjectInfo; -import org.jclouds.openstack.swift.options.ListContainerOptions; +import org.jclouds.openstack.swift.v1.domain.Container; +import org.jclouds.openstack.swift.v1.domain.ObjectList; +import org.jclouds.openstack.swift.v1.domain.SwiftObject; +import org.jclouds.openstack.swift.v1.features.ObjectApi; +import org.jclouds.openstack.swift.v1.options.ListContainerOptions; +import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; import com.google.common.io.Closeables; @@ -42,10 +44,10 @@ * @author Jeremy Daggett */ public class DeleteObjectsAndContainer 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 @@ -65,9 +67,9 @@ public static void main(String[] args) throws IOException { } public DeleteObjectsAndContainer(String username, String apiKey) { - swift = ContextBuilder.newBuilder(PROVIDER) + cloudFiles = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) - .buildApi(CloudFilesClient.class); + .buildApi(CloudFilesApi.class); } /** @@ -76,21 +78,21 @@ public DeleteObjectsAndContainer(String username, String apiKey) { private void deleteObjectsAndContainer() { System.out.format("Delete Container%n"); - Set containers = swift - .listContainers(ListContainerOptions.Builder.withPrefix(CONTAINER)); + List containers = cloudFiles.containerApiInRegion(REGION) + .list(ListContainerOptions.Builder.prefix(CONTAINER)).toList(); - for (ContainerMetadata container: containers) { + for (Container container: containers) { System.out.format(" %s%n", container.getName()); - Set objects = swift.listObjects(container.getName()); + ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, container.getName()); + ObjectList objects = objectApi.list(ListContainerOptions.NONE); - for (ObjectInfo object: objects) { + for (SwiftObject object: objects) { System.out.format(" %s%n", object.getName()); - - swift.removeObject(container.getName(), object.getName()); + objectApi.delete(object.getName()); } - swift.deleteContainerIfEmpty(container.getName()); + cloudFiles.containerApiInRegion(REGION).deleteIfEmpty(container.getName()); } } @@ -98,6 +100,6 @@ private void deleteObjectsAndContainer() { * Always close your service when you're done with it. */ public void close() throws IOException { - Closeables.close(swift, true); + Closeables.close(cloudFiles, true); } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GenerateTempURL.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GenerateTempURL.java index d1c6e78..07c8bae 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GenerateTempURL.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GenerateTempURL.java @@ -19,8 +19,10 @@ package org.jclouds.examples.rackspace.cloudfiles; import com.google.common.base.Charsets; +import com.google.common.io.ByteSource; import com.google.common.io.Closeables; import com.google.common.io.Files; + import org.jclouds.ContextBuilder; import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStoreContext; @@ -28,6 +30,8 @@ import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponseException; +import org.jclouds.io.Payload; +import org.jclouds.io.Payloads; import org.jclouds.util.Strings2; import java.io.Closeable; @@ -62,7 +66,7 @@ public class GenerateTempURL implements Closeable { private final BlobStoreContext blobStoreContext; /** - * 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 @@ -92,8 +96,13 @@ public GenerateTempURL(String username, String apiKey) { private void generatePutTempURL() throws IOException { System.out.format("Generate PUT Temp URL%n"); - - String payload = "This object will be public for 10 minutes."; + + // Create the Payload + String data = "This object will be public for 10 minutes."; + ByteSource source = ByteSource.wrap(data.getBytes()); + Payload payload = Payloads.newByteSourcePayload(source); + + // Create the Blob Blob blob = blobStore.blobBuilder(FILENAME).payload(payload).contentType("text/plain").build(); HttpRequest request = blobStoreContext.getSigner().signPutBlob(CONTAINER, blob, TEN_MINUTES); diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GetObject.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GetObject.java new file mode 100644 index 0000000..a5a2a79 --- /dev/null +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/GetObject.java @@ -0,0 +1,89 @@ +/* + * 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.examples.rackspace.cloudfiles; + +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.http.options.GetOptions; +import org.jclouds.openstack.swift.v1.domain.SwiftObject; +import org.jclouds.openstack.swift.v1.features.ObjectApi; +import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; + +import com.google.common.io.Closeables; + +/** + * Gets an object from a container and displays the results. + * + * NOTE: Run the {@link UploadObjects} example prior to running this example. + * + * @author Everett Toews + * @author Jeremy Daggett + */ +public class GetObject implements Closeable { + private final CloudFilesApi cloudFiles; + + /** + * 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 + */ + public static void main(String[] args) throws IOException { + GetObject getObject = new GetObject(args[0], args[1]); + + try { + getObject.getObject(); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + getObject.close(); + } + } + + public GetObject(String username, String apiKey) { + cloudFiles = ContextBuilder.newBuilder(PROVIDER) + .credentials(username, apiKey) + .buildApi(CloudFilesApi.class); + + } + + private void getObject() { + System.out.format("Get Object%n"); + + ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER); + SwiftObject object = objectApi.get("uploadObjectFromFile.txt", GetOptions.NONE); + + System.out.format(" %s%n", object); + } + + /** + * Always close your service when you're done with it. + */ + public void close() throws IOException { + Closeables.close(cloudFiles, true); + } +} diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListContainers.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListContainers.java index c30a37b..d844d38 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListContainers.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListContainers.java @@ -19,15 +19,15 @@ package org.jclouds.examples.rackspace.cloudfiles; 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.Set; +import java.util.List; 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.v1.domain.Container; +import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; import com.google.common.io.Closeables; @@ -38,10 +38,10 @@ * @author Jeremy Daggett */ public class ListContainers 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,17 +61,16 @@ public static void main(String[] args) throws IOException { } public ListContainers(String username, String apiKey) { - swift = ContextBuilder.newBuilder(PROVIDER) + cloudFiles = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) - .buildApi(CloudFilesClient.class); + .buildApi(CloudFilesApi.class); } private void listContainers() { System.out.format("List Containers%n"); - Set containers = swift.listContainers(); - - for (ContainerMetadata container: containers) { + List containers = cloudFiles.containerApiInRegion(REGION).list().toList(); + for (Container container: containers) { System.out.format(" %s%n", container); } } @@ -80,6 +79,6 @@ private void listContainers() { * Always close your service when you're done with it. */ public void close() throws IOException { - Closeables.close(swift, true); + Closeables.close(cloudFiles, true); } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListObjects.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListObjects.java index 5a4ca8c..4d7e115 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListObjects.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/ListObjects.java @@ -20,16 +20,17 @@ 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.Set; import org.jclouds.ContextBuilder; -import org.jclouds.cloudfiles.CloudFilesClient; -import org.jclouds.openstack.swift.CommonSwiftClient; -import org.jclouds.openstack.swift.domain.ObjectInfo; -import org.jclouds.openstack.swift.options.ListContainerOptions; +import org.jclouds.openstack.swift.v1.domain.ObjectList; +import org.jclouds.openstack.swift.v1.domain.SwiftObject; +import org.jclouds.openstack.swift.v1.features.ObjectApi; +import org.jclouds.openstack.swift.v1.options.ListContainerOptions; +import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; import com.google.common.io.Closeables; @@ -40,10 +41,10 @@ * @author Jeremy Daggett */ public class ListObjects 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 @@ -64,29 +65,32 @@ public static void main(String[] args) throws IOException { } public ListObjects(String username, String apiKey) { - swift = ContextBuilder.newBuilder(PROVIDER) + cloudFiles = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) - .buildApi(CloudFilesClient.class); + .buildApi(CloudFilesApi.class); } private void listObjects() { System.out.format("List Objects%n"); - Set objects = swift.listObjects(CONTAINER); + ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER); + ObjectList objects = objectApi.list(ListContainerOptions.NONE); - for (ObjectInfo objectInfo: objects) { - System.out.format(" %s%n", objectInfo); + for (SwiftObject object: objects) { + System.out.format(" %s%n", object); } } private void listObjectsWithFiltering() { System.out.format("List Objects With Filtering%n"); - ListContainerOptions filter = ListContainerOptions.Builder.withPrefix("createObjectFromString"); - Set objects = swift.listObjects(CONTAINER, filter); + ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER); - for (ObjectInfo objectInfo: objects) { - System.out.format(" %s%n", objectInfo); + ListContainerOptions filter = ListContainerOptions.Builder.prefix("createObjectFromString"); + ObjectList objects = objectApi.list(filter); + + for (SwiftObject object: objects) { + System.out.format(" %s%n", object); } } @@ -94,6 +98,6 @@ private void listObjectsWithFiltering() { * Always close your service when you're done with it. */ public void close() throws IOException { - Closeables.close(swift, true); + Closeables.close(cloudFiles, true); } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java index 96cccad..1a803bb 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadDirectoryToCDN.java @@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static java.util.concurrent.Executors.newFixedThreadPool; 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.File; @@ -34,10 +35,15 @@ import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.domain.Blob; -import org.jclouds.cloudfiles.CloudFilesClient; +import org.jclouds.io.Payloads; +import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; +import org.jclouds.rackspace.cloudfiles.v1.features.CDNApi; +import org.jclouds.rackspace.cloudfiles.v1.options.UpdateCDNContainerOptions; import com.google.common.collect.Lists; +import com.google.common.io.ByteSource; import com.google.common.io.Closeables; +import com.google.common.io.Files; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -50,15 +56,16 @@ * way to upload content for a static website (http://j.mp/rax-static). * * @author Everett Toews + * @author Jeremy Daggett */ public class UploadDirectoryToCDN implements Closeable { private static final int THREADS = Integer.getInteger("upload.threadpool.size", 10); private final BlobStore blobStore; - 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 @@ -84,7 +91,7 @@ public UploadDirectoryToCDN(String username, String apiKey) { ContextBuilder builder = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey); blobStore = builder.buildView(BlobStoreContext.class).getBlobStore(); - cloudFilesClient = builder.buildApi(CloudFilesClient.class); + cloudFiles = blobStore.getContext().unwrapApi(CloudFilesApi.class); } /** @@ -140,7 +147,7 @@ private void uploadFiles(String container, List blobDetails) ListenableFuture> future = Futures.successfulAsList(blobUploaderFutures); List uploadedBlobDetails = future.get(); // begin the upload - + System.out.format("%n"); for (int i = 0; i < uploadedBlobDetails.size(); i++) { @@ -165,8 +172,10 @@ private void uploadFiles(String container, List blobDetails) private void enableCdnContainer(String container) { System.out.format("Enable CDN%n"); - URI cdnURI = cloudFilesClient.enableCDN(container); - cloudFilesClient.setCDNStaticWebsiteIndex(container, "index.html"); + CDNApi cdnApi = cloudFiles.cdnApiInRegion(REGION); + URI cdnURI = cdnApi.enable(container); + + cdnApi.update(container, UpdateCDNContainerOptions.Builder.staticWebsiteIndexPage("index.html")); System.out.format(" Go to %s/%n", cdnURI); } @@ -176,7 +185,6 @@ private void enableCdnContainer(String container) { */ public void close() throws IOException { Closeables.close(blobStore.getContext(), true); - Closeables.close(cloudFilesClient, true); } /** @@ -186,7 +194,7 @@ public void close() throws IOException { private class BlobUploader implements Callable { private final String container; private final BlobDetail toBeUploadedBlobDetail; - + protected BlobUploader(String container, BlobDetail toBeUploadedBlobDetail) { this.container = container; this.toBeUploadedBlobDetail = toBeUploadedBlobDetail; @@ -194,14 +202,16 @@ protected BlobUploader(String container, BlobDetail toBeUploadedBlobDetail) { @Override public BlobDetail call() throws Exception { + ByteSource byteSource = Files.asByteSource(toBeUploadedBlobDetail.getLocalFile()); + Blob blob = blobStore.blobBuilder(toBeUploadedBlobDetail.getRemoteBlobName()) - .payload(toBeUploadedBlobDetail.getLocalFile()) + .payload(Payloads.newByteSourcePayload(byteSource)) .contentType("") // allows Cloud Files to determine the content type .build(); String eTag = blobStore.putBlob(container, blob); BlobDetail uploadedBlobDetail = new BlobDetail( toBeUploadedBlobDetail.getRemoteBlobName(), toBeUploadedBlobDetail.getLocalFile(), eTag); - + return uploadedBlobDetail; } } diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadLargeObject.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadLargeObject.java index bf0f68a..2ecb4ea 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadLargeObject.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadLargeObject.java @@ -18,11 +18,10 @@ */ package org.jclouds.examples.rackspace.cloudfiles; -import com.google.common.io.Closeables; -import org.jclouds.ContextBuilder; -import org.jclouds.blobstore.BlobStore; -import org.jclouds.blobstore.BlobStoreContext; -import org.jclouds.blobstore.domain.Blob; +import static org.jclouds.blobstore.options.PutOptions.Builder.multipart; +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.File; @@ -30,20 +29,28 @@ import java.util.Properties; import java.util.concurrent.ExecutionException; -import static org.jclouds.blobstore.options.PutOptions.Builder.multipart; -import static org.jclouds.examples.rackspace.cloudfiles.Constants.CONTAINER; -import static org.jclouds.examples.rackspace.cloudfiles.Constants.PROVIDER; +import org.jclouds.ContextBuilder; +import org.jclouds.blobstore.BlobStore; +import org.jclouds.blobstore.domain.Blob; +import org.jclouds.io.Payload; +import org.jclouds.io.Payloads; +import org.jclouds.openstack.swift.v1.blobstore.RegionScopedBlobStoreContext; + +import com.google.common.io.ByteSource; +import com.google.common.io.Closeables; +import com.google.common.io.Files; /** * Upload a large object in the Cloud Files container from the CreateContainer example. - * + * * @author Everett Toews + * @author Jeremy Daggett */ public class UploadLargeObject implements Closeable { private BlobStore blobStore; /** - * 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 @@ -70,25 +77,32 @@ public UploadLargeObject(String username, String apiKey) { // This property controls the size (in bytes) of parts being uploaded in parallel, the default is 33554432 bytes = 32 MB overrides.setProperty("jclouds.mpu.parts.size", "67108864"); // 64 MB - BlobStoreContext context = ContextBuilder.newBuilder(PROVIDER) + RegionScopedBlobStoreContext context = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey) .overrides(overrides) - .buildView(BlobStoreContext.class); - blobStore = context.getBlobStore(); + .buildView(RegionScopedBlobStoreContext.class); + blobStore = context.blobStoreInRegion(REGION); } /** - * Upload a large object from a File using the Swift API. + * Upload a large object from a File using the BlobStore API. + * * @throws ExecutionException * @throws InterruptedException */ private void uploadLargeObjectFromFile(File largeFile) throws InterruptedException, ExecutionException { System.out.format("Upload Large Object From File%n"); + ByteSource source = Files.asByteSource(largeFile); + // create the payload and set the content length + Payload payload = Payloads.newByteSourcePayload(source); + payload.getContentMetadata().setContentLength(largeFile.length()); + Blob blob = blobStore.blobBuilder(largeFile.getName()) - .payload(largeFile) + .payload(payload) .build(); - + + // configure the blobstore to use multipart uploading of the file String eTag = blobStore.putBlob(CONTAINER, blob, multipart()); System.out.format(" Uploaded %s eTag=%s", largeFile.getName(), eTag); diff --git a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadObjects.java b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadObjects.java index f67d7fd..d7ed51c 100644 --- a/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadObjects.java +++ b/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudfiles/UploadObjects.java @@ -20,6 +20,7 @@ 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.BufferedWriter; import java.io.Closeable; @@ -31,25 +32,29 @@ import org.jclouds.ContextBuilder; import org.jclouds.blobstore.BlobStore; -import org.jclouds.blobstore.BlobStoreContext; import org.jclouds.blobstore.domain.Blob; -import org.jclouds.cloudfiles.CloudFilesClient; -import org.jclouds.openstack.swift.CommonSwiftClient; -import org.jclouds.openstack.swift.domain.SwiftObject; +import org.jclouds.io.Payload; +import org.jclouds.io.Payloads; +import org.jclouds.openstack.swift.v1.blobstore.RegionScopedBlobStoreContext; +import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.ByteSource; import com.google.common.io.Closeables; +import com.google.common.io.Files; /** * Upload objects in the Cloud Files container from the CreateContainer example. * * @author Everett Toews + * @author Jeremy Daggett */ public class UploadObjects implements Closeable { private final BlobStore blobStore; - 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 @@ -73,8 +78,8 @@ public static void main(String[] args) throws IOException { public UploadObjects(String username, String apiKey) { ContextBuilder builder = ContextBuilder.newBuilder(PROVIDER) .credentials(username, apiKey); - blobStore = builder.buildView(BlobStoreContext.class).getBlobStore(); - swift = builder.buildApi(CloudFilesClient.class); + blobStore = builder.buildView(RegionScopedBlobStoreContext.class).blobStoreInRegion(REGION); + cloudFiles = blobStore.getContext().unwrapApi(CloudFilesApi.class); } /** @@ -93,11 +98,11 @@ private void uploadObjectFromFile() throws IOException { out.write("uploadObjectFromFile"); out.close(); - SwiftObject object = swift.newSwiftObject(); - object.getInfo().setName(filename + suffix); - object.setPayload(tempFile); - - swift.putObject(CONTAINER, object); + ByteSource byteSource = Files.asByteSource(tempFile); + Payload payload = Payloads.newByteSourcePayload(byteSource); + + cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER) + .replace(filename + suffix, payload, ImmutableMap. of()); System.out.format(" %s%s%n", filename, suffix); } @@ -110,11 +115,11 @@ private void uploadObjectFromString() { String filename = "uploadObjectFromString.txt"; - SwiftObject object = swift.newSwiftObject(); - object.getInfo().setName(filename); - object.setPayload("uploadObjectFromString"); + ByteSource source = ByteSource.wrap("uploadObjectFromString".getBytes()); + Payload payload = Payloads.newByteSourcePayload(source); - swift.putObject(CONTAINER, object); + cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER) + .replace(filename, payload, ImmutableMap.of()); System.out.format(" %s%n", filename); } @@ -130,8 +135,10 @@ private void uploadObjectFromStringWithMetadata() { Map userMetadata = new HashMap(); userMetadata.put("key1", "value1"); + ByteSource source = ByteSource.wrap("uploadObjectFromString".getBytes()); + Blob blob = blobStore.blobBuilder(filename) - .payload("uploadObjectFromStringWithMetadata") + .payload(Payloads.newByteSourcePayload(source)) .userMetadata(userMetadata) .build(); @@ -145,6 +152,5 @@ private void uploadObjectFromStringWithMetadata() { */ public void close() throws IOException { Closeables.close(blobStore.getContext(), true); - Closeables.close(swift, true); } }