Skip to content
Browse files
Example to show how to enable CORS on a Container.
  • Loading branch information
Everett Toews committed Jun 4, 2013
1 parent 48b0c16 commit a5dab7061dec4a370984277e9db0e15e70865835
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
@@ -38,6 +38,7 @@
import org.jclouds.examples.rackspace.clouddns.UpdateRecords;
import org.jclouds.examples.rackspace.cloudfiles.CloudFilesPublish;
import org.jclouds.examples.rackspace.cloudfiles.CreateContainer;
import org.jclouds.examples.rackspace.cloudfiles.CrossOriginResourceSharingContainer;
import org.jclouds.examples.rackspace.cloudfiles.DeleteObjectsAndContainer;
import org.jclouds.examples.rackspace.cloudfiles.ListContainers;
import org.jclouds.examples.rackspace.cloudfiles.ListObjects;
@@ -90,6 +91,7 @@ private void smokeTest(String[] args) {

@@ -0,0 +1,133 @@
* 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
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* 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 java.util.Map;

import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
import org.jclouds.openstack.swift.CommonSwiftClient;
import org.jclouds.openstack.swift.domain.ContainerMetadata;
import org.jclouds.openstack.swift.options.CreateContainerOptions;


* Create an object storage 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.
* @author Everett Toews
public class CrossOriginResourceSharingContainer {
private BlobStore storage;
private RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift;

* To get a username and API key see
* 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) {
CrossOriginResourceSharingContainer corsContainer = new CrossOriginResourceSharingContainer();

try {
catch (Exception e) {
finally {

private void init(String[] args) {
// The provider configures jclouds To use the Rackspace Cloud (US)
// To use the Rackspace Cloud (UK) set the provider to "cloudfiles-uk"
String provider = "cloudfiles-us";

String username = args[0];
String apiKey = args[1];

BlobStoreContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
storage = context.getBlobStore();
swift = context.unwrap();

* Create a Cross Origin Resource Sharing container.
* Access-Control-Allow-Origin: Which URLs can make Cross Origin Requests. Format is
* Separate URLs with a space. An asterisk (*) allows all.
* Access-Control-Max-Age: The maximum age for the browser to cache the options request, in seconds.
* Access-Control-Allow-Headers: Which custom metadata headers you allow to be assigned to objects in this container.
private void createCorsContainer() {
System.out.println("Create Cross Origin Resource Sharing Container");

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

swift.getApi().createContainer(Constants.CONTAINER, options);
System.out.println(" " + Constants.CONTAINER);

ContainerMetadata containerMetadata = swift.getApi().getContainerMetadata(Constants.CONTAINER);
System.out.println(" " + containerMetadata.getMetadata());

* Update a Cross Origin Resource Sharing container.
private void updateCorsContainer() {
System.out.println("Update Cross Origin Resource Sharing Container");

Map<String, String> corsMetadata = ImmutableMap.of(
"Access-Control-Allow-Origin", "",
"Access-Control-Max-Age", "60",
"Access-Control-Allow-Headers", "X-My-Other-Header");

swift.getApi().setContainerMetadata(Constants.CONTAINER, corsMetadata);
System.out.println(" " + Constants.CONTAINER);

ContainerMetadata containerMetadata = swift.getApi().getContainerMetadata(Constants.CONTAINER);
System.out.println(" " + containerMetadata.getMetadata());

* Always close your service when you're done with it.
public void close() {
if (storage != null) {

0 comments on commit a5dab70

Please sign in to comment.