Skip to content
Permalink
Browse files
Updated Cloud Files examples for 1.7.3
  • Loading branch information
Jeremy Daggett authored and Everett Toews committed Jun 10, 2014
1 parent fb328b5 commit 0347a90e26f2829ef2574eff63638f044b3134c9
Showing 12 changed files with 135 additions and 41 deletions.
@@ -22,11 +22,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.jclouds.examples</groupId>
<artifactId>rackspace-examples</artifactId>
<version>1.7.3-SNAPSHOT</version>
<version>1.7.4-SNAPSHOT</version>
<name>rackspace-examples</name>

<properties>
<jclouds.version>1.7.2</jclouds.version>
<jclouds.version>1.7.3</jclouds.version>
</properties>

<dependencies>
@@ -34,11 +34,13 @@
import org.jclouds.io.Payloads;
import org.jclouds.openstack.swift.v1.features.ObjectApi;
import org.jclouds.openstack.swift.v1.options.CreateContainerOptions;
import org.jclouds.openstack.swift.v1.reference.SwiftHeaders;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;
import org.jclouds.rackspace.cloudfiles.v1.features.CDNApi;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
@@ -83,8 +85,13 @@ public CloudFilesPublish(String username, String apiKey) {
*/
private void createContainer() {
System.out.format("Create Container%n");

cloudFiles.containerApiInRegion(REGION).createIfAbsent(CONTAINER_PUBLISH, CreateContainerOptions.NONE);
// 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");

CreateContainerOptions opts = new CreateContainerOptions().headers(enableStaticWebHeaders);
cloudFiles.getContainerApiForRegion(REGION).create(CONTAINER_PUBLISH, opts);

System.out.format(" %s%n", CONTAINER_PUBLISH);
}
@@ -100,14 +107,12 @@ private void createObjectFromFile() throws IOException {
try {
Files.write("Hello Cloud Files", tempFile, Charsets.UTF_8);

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

ByteSource byteSource = Files.asByteSource(tempFile);
Payload payload = Payloads.newByteSourcePayload(byteSource);

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

System.out.format(" %s%s%n", FILENAME, SUFFIX);
objectApi.put(FILENAME + SUFFIX, payload);
} finally {
tempFile.delete();
}
@@ -119,8 +124,8 @@ private void createObjectFromFile() throws IOException {
*/
private void enableCdnContainer() {
System.out.format("Enable CDN Container%n");
CDNApi cdnApi = cloudFiles.cdnApiInRegion(REGION);

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

System.out.format(" Go to %s/%s%s%n", cdnURI, FILENAME, SUFFIX);
@@ -71,7 +71,7 @@ private void createContainer() {
CreateContainerOptions options = CreateContainerOptions.Builder
.metadata(ImmutableMap.of("key1", "value1", "key2", "value2"));

cloudFiles.containerApiInRegion(REGION).createIfAbsent(CONTAINER, options);
cloudFiles.getContainerApiForRegion(REGION).create(CONTAINER, options);

System.out.format(" %s%n", CONTAINER);
}
@@ -33,6 +33,8 @@
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

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

/**
@@ -71,7 +73,7 @@ public CrossOriginResourceSharingContainer(String username, String apiKey) {
cloudFiles = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.buildApi(CloudFilesApi.class);
containerApi = cloudFiles.containerApiInRegion(REGION);
containerApi = cloudFiles.getContainerApiForRegion(REGION);
}

/**
@@ -85,13 +87,13 @@ public CrossOriginResourceSharingContainer(String username, String apiKey) {
private void createCorsContainer() {
System.out.format("Create Cross Origin Resource Sharing Container%n");

Map<String, String> corsMetadata = ImmutableMap.of(
Multimap<String, String> headers = ImmutableMultimap.of(
"Access-Control-Allow-Origin", "*",
"Access-Control-Max-Age", "600",
"Access-Control-Allow-Headers", "X-My-Header");
CreateContainerOptions options = CreateContainerOptions.Builder.metadata(corsMetadata);
CreateContainerOptions options = CreateContainerOptions.Builder.headers(headers);

containerApi.createIfAbsent(CONTAINER, options);
containerApi.create(CONTAINER, options);
System.out.format(" %s%n", CONTAINER);

Container container = containerApi.get(CONTAINER);
@@ -76,21 +76,21 @@ public DeleteObjectsAndContainer(String username, String apiKey) {
private void deleteObjectsAndContainer() {
System.out.format("Delete Container%n");

List<Container> containers = cloudFiles.containerApiInRegion(REGION)
List<Container> containers = cloudFiles.getContainerApiForRegion(REGION)
.list(ListContainerOptions.Builder.prefix(CONTAINER)).toList();

for (Container container: containers) {
System.out.format(" %s%n", container.getName());

ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, container.getName());
ObjectApi objectApi = cloudFiles.getObjectApiForRegionAndContainer(REGION, container.getName());
ObjectList objects = objectApi.list(ListContainerOptions.NONE);

for (SwiftObject object: objects) {
System.out.format(" %s%n", object.getName());
objectApi.delete(object.getName());
}

cloudFiles.containerApiInRegion(REGION).deleteIfEmpty(container.getName());
cloudFiles.getContainerApiForRegion(REGION).deleteIfEmpty(container.getName());
}
}

@@ -128,10 +128,14 @@ private void generateGetTempURL() throws IOException {

// GET the file using jclouds
File file = File.createTempFile(FILENAME, ".tmp");
String content = Strings2.toString(blobStoreContext.utils().http().invoke(request).getPayload());
Files.write(content, file, Charsets.UTF_8);

System.out.format(" GET Success (%s)%n", file.getAbsolutePath());
try {
String content = Strings2.toString(blobStoreContext.utils().http().invoke(request).getPayload());
Files.write(content, file, Charsets.UTF_8);

System.out.format(" GET Success (%s)%n", file.getAbsolutePath());
} finally {
file.delete();
}
}

private void generateDeleteTempURL() throws IOException {
@@ -0,0 +1,82 @@
/*
* 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.openstack.swift.v1.domain.Container;
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

import com.google.common.io.Closeables;

/**
* 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;

/**
* 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 {
GetContainer getContainer = new GetContainer(args[0], args[1]);

try {
getContainer.getContainer();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
getContainer.close();
}
}

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

private void getContainer() {
System.out.format("Get Container%n");

Container container = cloudFiles.getContainerApiForRegion(REGION).get(CONTAINER);
System.out.format(" %s%n", container);
}

/**
* Always close your service when you're done with it.
*/
public void close() throws IOException {
Closeables.close(cloudFiles, true);
}
}
@@ -72,7 +72,7 @@ public GetObject(String username, String apiKey) {
private void getObject() {
System.out.format("Get Object%n");

ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER);
ObjectApi objectApi = cloudFiles.getObjectApiForRegionAndContainer(REGION, CONTAINER);
SwiftObject object = objectApi.get("uploadObjectFromFile.txt", GetOptions.NONE);

System.out.format(" %s%n", object);
@@ -67,7 +67,7 @@ public ListContainers(String username, String apiKey) {
private void listContainers() {
System.out.format("List Containers%n");

List<Container> containers = cloudFiles.containerApiInRegion(REGION).list().toList();
List<Container> containers = cloudFiles.getContainerApiForRegion(REGION).list().toList();
for (Container container: containers) {
System.out.format(" %s%n", container);
}
@@ -71,7 +71,7 @@ public ListObjects(String username, String apiKey) {
private void listObjects() {
System.out.format("List Objects%n");

ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER);
ObjectApi objectApi = cloudFiles.getObjectApiForRegionAndContainer(REGION, CONTAINER);
ObjectList objects = objectApi.list(ListContainerOptions.NONE);

for (SwiftObject object: objects) {
@@ -82,7 +82,7 @@ private void listObjects() {
private void listObjectsWithFiltering() {
System.out.format("List Objects With Filtering%n");

ObjectApi objectApi = cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER);
ObjectApi objectApi = cloudFiles.getObjectApiForRegionAndContainer(REGION, CONTAINER);

ListContainerOptions filter = ListContainerOptions.Builder.prefix("createObjectFromString");
ObjectList objects = objectApi.list(filter);
@@ -36,11 +36,13 @@
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 org.jclouds.rackspace.cloudfiles.v1.features.CDNApi;
import org.jclouds.rackspace.cloudfiles.v1.options.UpdateCDNContainerOptions;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
@@ -168,12 +170,15 @@ 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");

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

cdnApi.update(container, UpdateCDNContainerOptions.Builder.staticWebsiteIndexPage("index.html"));
UpdateContainerOptions opts = new UpdateContainerOptions().headers(enableStaticWebHeaders);
cloudFiles.getContainerApiForRegion(REGION).update(container, opts);

// enable the CDN container
URI cdnURI = cloudFiles.getCDNApiForRegion(REGION).enable(container);
System.out.format(" Go to %s/%n", cdnURI);
}

@@ -197,7 +202,6 @@ protected BlobUploader(String container, BlobDetail toBeUploadedBlobDetail) {
this.toBeUploadedBlobDetail = toBeUploadedBlobDetail;
}

@Override
public BlobDetail call() throws Exception {
ByteSource byteSource = Files.asByteSource(toBeUploadedBlobDetail.getLocalFile());

@@ -218,12 +222,11 @@ public BlobDetail call() throws Exception {
* the user of upload progress.
*/
private class BlobUploaderCallback implements FutureCallback<BlobDetail> {
@Override

public void onSuccess(BlobDetail result) {
System.out.format(".");
}

@Override
public void onFailure(Throwable t) {
System.out.format("X %s", t);
}
@@ -37,7 +37,6 @@
import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteSource;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
@@ -96,8 +95,8 @@ private void uploadObjectFromFile() throws IOException {
ByteSource byteSource = Files.asByteSource(tempFile);
Payload payload = Payloads.newByteSourcePayload(byteSource);

cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER)
.replace(filename + suffix, payload, ImmutableMap.<String, String> of());
cloudFiles.getObjectApiForRegionAndContainer(REGION, CONTAINER)
.put(filename + suffix, payload);

System.out.format(" %s%s%n", filename, suffix);
} finally {
@@ -116,8 +115,7 @@ private void uploadObjectFromString() {
ByteSource source = ByteSource.wrap("uploadObjectFromString".getBytes());
Payload payload = Payloads.newByteSourcePayload(source);

cloudFiles.objectApiInRegionForContainer(REGION, CONTAINER)
.replace(filename, payload, ImmutableMap.<String, String>of());
cloudFiles.getObjectApiForRegionAndContainer(REGION, CONTAINER).put(filename, payload);

System.out.format(" %s%n", filename);
}

0 comments on commit 0347a90

Please sign in to comment.