Skip to content
Permalink
Browse files
Fixes to Cloud Files examples from PR #50
  • Loading branch information
Everett Toews committed Jun 10, 2014
1 parent 0347a90 commit ce3365b82415a7df0297293cc4f122f4c8109ceb
Showing 5 changed files with 54 additions and 47 deletions.
@@ -62,6 +62,7 @@ private void smokeTest(String[] args) throws IOException {
CloudFilesPublish.main(args);
CreateContainer.main(args);
ListContainers.main(args);
GenerateTempURL.main(args);
UploadObjects.main(args);
ListObjects.main(args);
CrossOriginResourceSharingContainer.main(args);
@@ -23,6 +23,8 @@
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 static org.jclouds.openstack.swift.v1.reference.SwiftHeaders.STATIC_WEB_ERROR;
import static org.jclouds.openstack.swift.v1.reference.SwiftHeaders.STATIC_WEB_INDEX;

import java.io.Closeable;
import java.io.File;
@@ -85,10 +87,10 @@ public CloudFilesPublish(String username, String apiKey) {
*/
private void createContainer() {
System.out.format("Create Container%n");
// create a Multimap for the static web headers

Multimap<String, String> enableStaticWebHeaders =
ImmutableMultimap.of(SwiftHeaders.STATIC_WEB_INDEX, FILENAME + SUFFIX,
SwiftHeaders.STATIC_WEB_ERROR, "error.html");
ImmutableMultimap.of(STATIC_WEB_INDEX, FILENAME + SUFFIX,
STATIC_WEB_ERROR, "error.html");

CreateContainerOptions opts = new CreateContainerOptions().headers(enableStaticWebHeaders);
cloudFiles.getContainerApiForRegion(REGION).create(CONTAINER_PUBLISH, opts);
@@ -18,11 +18,9 @@
*/
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;
@@ -32,14 +30,15 @@
import org.jclouds.http.HttpResponseException;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.util.Strings2;
import org.jclouds.openstack.swift.v1.blobstore.RegionScopedBlobStoreContext;

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

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;

/**
* The Temporary URL feature (TempURL) allows you to create limited-time Internet addresses which allow you to grant
@@ -62,7 +61,7 @@ public class GenerateTempURL implements Closeable {
private static final int TEN_MINUTES = 10 * 60;

private final BlobStore blobStore;
private final BlobStoreContext blobStoreContext;
private final RegionScopedBlobStoreContext blobStoreContext;

/**
* To get a username and API key see http://jclouds.apache.org/guides/rackspace/
@@ -89,8 +88,8 @@ public static void main(String[] args) throws IOException {
public GenerateTempURL(String username, String apiKey) {
blobStoreContext = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildView(BlobStoreContext.class);
blobStore = blobStoreContext.getBlobStore();
.buildView(RegionScopedBlobStoreContext.class);
blobStore = blobStoreContext.blobStoreInRegion(REGION);
}

private void generatePutTempURL() throws IOException {
@@ -105,7 +104,7 @@ private void generatePutTempURL() throws IOException {
Blob blob = blobStore.blobBuilder(FILENAME).payload(payload).contentType("text/plain").build();
HttpRequest request = blobStoreContext.getSigner().signPutBlob(CONTAINER, blob, TEN_MINUTES);

System.out.format(" %s %s", request.getMethod(), request.getEndpoint());
System.out.format(" %s %s%n", request.getMethod(), request.getEndpoint());

// PUT the file using jclouds
HttpResponse response = blobStoreContext.utils().http().invoke(request);
@@ -124,16 +123,18 @@ private void generateGetTempURL() throws IOException {

HttpRequest request = blobStoreContext.getSigner().signGetBlob(CONTAINER, FILENAME, TEN_MINUTES);

System.out.format(" %s %s", request.getMethod(), request.getEndpoint());
System.out.format(" %s %s%n", request.getMethod(), request.getEndpoint());

// GET the file using jclouds
File file = File.createTempFile(FILENAME, ".tmp");
Payload payload = blobStoreContext.utils().http().invoke(request).getPayload();

try {
String content = Strings2.toString(blobStoreContext.utils().http().invoke(request).getPayload());
Files.write(content, file, Charsets.UTF_8);

Files.asByteSink(file).writeFrom(payload.openStream());

System.out.format(" GET Success (%s)%n", file.getAbsolutePath());
} finally {
payload.release();
file.delete();
}
}
@@ -143,7 +144,7 @@ private void generateDeleteTempURL() throws IOException {

HttpRequest request = blobStoreContext.getSigner().signRemoveBlob(CONTAINER, FILENAME);

System.out.format(" %s %s", request.getMethod(), request.getEndpoint());
System.out.format(" %s %s%n", request.getMethod(), request.getEndpoint());

// DELETE the file using jclouds
HttpResponse response = blobStoreContext.utils().http().invoke(request);
@@ -33,9 +33,6 @@

/**
* Create a Cloud Files container with some metadata associated with it.
*
* @author Everett Toews
* @author Jeremy Daggett
*/
public class GetContainer implements Closeable {
private final CloudFilesApi cloudFiles;
@@ -18,29 +18,8 @@
*/
package org.jclouds.examples.rackspace.cloudfiles;

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;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.io.Payloads;
import org.jclouds.openstack.swift.v1.options.UpdateContainerOptions;
import org.jclouds.openstack.swift.v1.reference.SwiftHeaders;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
@@ -51,6 +30,30 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.domain.Location;
import org.jclouds.io.Payloads;
import org.jclouds.openstack.swift.v1.blobstore.RegionScopedBlobStoreContext;
import org.jclouds.openstack.swift.v1.options.UpdateContainerOptions;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
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 static org.jclouds.openstack.swift.v1.reference.SwiftHeaders.STATIC_WEB_ERROR;
import static org.jclouds.openstack.swift.v1.reference.SwiftHeaders.STATIC_WEB_INDEX;

/**
* Upload an entire directory and all of its sub-directories to a Cloud Files container. The local directory hierarchy
@@ -87,9 +90,10 @@ public static void main(String[] args) throws IOException {
}

public UploadDirectoryToCDN(String username, String apiKey) {
ContextBuilder builder = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey);
blobStore = builder.buildView(BlobStoreContext.class).getBlobStore();
RegionScopedBlobStoreContext context = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildView(RegionScopedBlobStoreContext.class);
blobStore = context.blobStoreInRegion(REGION);
cloudFiles = blobStore.getContext().unwrapApi(CloudFilesApi.class);
}

@@ -99,10 +103,12 @@ public UploadDirectoryToCDN(String username, String apiKey) {
private void uploadDirectory(String dirPath, String container) throws InterruptedException, ExecutionException {
File dir = new File(dirPath);
checkArgument(dir.isDirectory(), "%s is not a directory", dirPath);

System.out.format("Uploading %s to %s", dirPath, container);

blobStore.createContainerInLocation(null, container);
// There is only one assignable location because we are using the RegionScopedBlobStoreContext
Location location = getOnlyElement(blobStore.listAssignableLocations());
blobStore.createContainerInLocation(location, container);

List<BlobDetail> blobDetails = Lists.newArrayList();
generateFileList(dir, "", blobDetails);
@@ -171,8 +177,8 @@ private void uploadFiles(String container, List<BlobDetail> blobDetails)
private void enableCdnContainer(String container) {
System.out.format("Enable CDN%n");
Multimap<String, String> enableStaticWebHeaders =
ImmutableMultimap.of(SwiftHeaders.STATIC_WEB_INDEX, "index.html",
SwiftHeaders.STATIC_WEB_ERROR, "error.html");
ImmutableMultimap.of(STATIC_WEB_INDEX, "index.html",
STATIC_WEB_ERROR, "error.html");

UpdateContainerOptions opts = new UpdateContainerOptions().headers(enableStaticWebHeaders);
cloudFiles.getContainerApiForRegion(REGION).update(container, opts);

0 comments on commit ce3365b

Please sign in to comment.