Java Swift Examples

Setup

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

  1. import org.javaswift.joss.client.factory.AccountConfig;
  2. import org.javaswift.joss.client.factory.AccountFactory;
  3. import org.javaswift.joss.client.factory.AuthenticationMethod;
  4. import org.javaswift.joss.model.Account;
  5. import org.javaswift.joss.model.Container;
  6. import org.javaswift.joss.model.StoredObject;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.*;

Create a Connection

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

  1. String username = "USERNAME";
  2. String password = "PASSWORD";
  3. String authUrl = "https://radosgw.endpoint/auth/1.0";
  4. AccountConfig config = new AccountConfig();
  5. config.setUsername(username);
  6. config.setPassword(password);
  7. config.setAuthUrl(authUrl);
  8. config.setAuthenticationMethod(AuthenticationMethod.BASIC);
  9. Account account = new AccountFactory(config).createAccount();

Create a Container

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

  1. Container container = account.getContainer("my-new-container");
  2. container.create();

Create an Object

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

  1. Container container = account.getContainer("my-new-container");
  2. StoredObject object = container.getObject("foo.txt");
  3. object.uploadObject(new File("foo.txt"));

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:

  1. Container container = account.getContainer("my-new-container");
  2. StoredObject object = container.getObject("foo.txt");
  3. Map<String, Object> metadata = new TreeMap<String, Object>();
  4. metadata.put("key", "value");
  5. object.setMetadata(metadata);

List Owned Containers

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

  1. Collection<Container> containers = account.list();
  2. for (Container currentContainer : containers) {
  3. System.out.println(currentContainer.getName());
  4. }

The output will look something like this:

  1. mahbuckat1
  2. mahbuckat2
  3. mahbuckat3

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:

  1. Container container = account.getContainer("my-new-container");
  2. Collection<StoredObject> objects = container.list();
  3. for (StoredObject currentObject : objects) {
  4. System.out.println(currentObject.getName());
  5. }

The output will look something like this:

  1. myphoto1.jpg
  2. myphoto2.jpg

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:

  1. Container container = account.getContainer("my-new-container");
  2. StoredObject object = container.getObject("foo.txt");
  3. Map<String, Object> returnedMetadata = object.getMetadata();
  4. for (String name : returnedMetadata.keySet()) {
  5. System.out.println("META / "+name+": "+returnedMetadata.get(name));
  6. }

Retrieve an Object

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

  1. Container container = account.getContainer("my-new-container");
  2. StoredObject object = container.getObject("foo.txt");
  3. object.downloadObject(new File("outfile.txt"));

Delete an Object

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

  1. Container container = account.getContainer("my-new-container");
  2. StoredObject object = container.getObject("foo.txt");
  3. object.delete();

Delete a Container

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

  1. Container container = account.getContainer("my-new-container");
  2. container.delete();

Note

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