Java Swift Examples


The following examples may require some or all of the following Java classes to be imported:

import java.util.List;
import java.util.Map;
import com.rackspacecloud.client.cloudfiles.FilesClient;
import com.rackspacecloud.client.cloudfiles.FilesConstants;
import com.rackspacecloud.client.cloudfiles.FilesContainer;
import com.rackspacecloud.client.cloudfiles.FilesContainerExistsException;
import com.rackspacecloud.client.cloudfiles.FilesObject;
import com.rackspacecloud.client.cloudfiles.FilesObjectMetaData;

Create a Connection

This creates a connection so that you can interact with the server:

String username = "USERNAME";
String password = "PASSWORD";
String authUrl  = "";

FilesClient client = new FilesClient(username, password, authUrl);
if (!client.login()) {
        throw new RuntimeException("Failed to log in");

Create a Container

This creates a new container called my-new-container:


Create an Object

This creates an object foo.txt from the file named foo.txt in the container my-new-container:

File file = new File("foo.txt");
String mimeType = FilesConstants.getMimetype("txt");
client.storeObject("my-new-container", file, mimeType);

Add/Update Object Metadata

This adds the metadata key-value pair key:value to the object named foo.txt in the container my-new-container:

FilesObjectMetaData metaData = client.getObjectMetaData("my-new-container", "foo.txt");
metaData.addMetaData("key", "value");

Map<String, String> metamap = metaData.getMetaData();
client.updateObjectMetadata("my-new-container", "foo.txt", metamap);

List Owned Containers

This gets a list of Containers that you own. This also prints out the container name.

List<FilesContainer> containers = client.listContainers();
for (FilesContainer container : containers) {
        System.out.println("  " + container.getName());

The output will look something like this:


List a Container’s Content

This gets a list of objects in the container my-new-container; and, it also prints out each object’s name, the file size, and last modified date:

List<FilesObject> objects = client.listObjects("my-new-container");
for (FilesObject object : objects) {
        System.out.println("  " + object.getName());

The output will look something like this:


Retrieve an Object’s Metadata

This retrieves metadata and gets the MIME type for an object named foo.txt in a container named my-new-container:

FilesObjectMetaData metaData =  client.getObjectMetaData("my-new-container", "foo.txt");
String mimeType = metaData.getMimeType();

Retrieve an Object

This downloads the object foo.txt in the container my-new-container and saves it in ./outfile.txt:

FilesObject obj;
File outfile = new File("outfile.txt");

List<FilesObject> objects = client.listObjects("my-new-container");
for (FilesObject object : objects) {
        String name = object.getName();
        if (name.equals("foo.txt")) {
                obj = object;

Delete an Object

This deletes the object goodbye.txt in the container “my-new-container”:

client.deleteObject("my-new-container", "goodbye.txt");

Delete a Container

This deletes a container named “my-new-container”:



The container must be empty! Otherwise it won’t work!