10 The following examples may require some or all of the following Java
11 classes to be imported:
15 import org.javaswift.joss.client.factory.AccountConfig;
16 import org.javaswift.joss.client.factory.AccountFactory;
17 import org.javaswift.joss.client.factory.AuthenticationMethod;
18 import org.javaswift.joss.model.Account;
19 import org.javaswift.joss.model.Container;
20 import org.javaswift.joss.model.StoredObject;
22 import java.io.IOException;
29 This creates a connection so that you can interact with the server:
33 String username = "USERNAME";
34 String password = "PASSWORD";
35 String authUrl = "https://radosgw.endpoint/auth/1.0";
37 AccountConfig config = new AccountConfig();
38 config.setUsername(username);
39 config.setPassword(password);
40 config.setAuthUrl(authUrl);
41 config.setAuthenticationMethod(AuthenticationMethod.BASIC);
42 Account account = new AccountFactory(config).createAccount();
48 This creates a new container called ``my-new-container``:
52 Container container = account.getContainer("my-new-container");
59 This creates an object ``foo.txt`` from the file named ``foo.txt`` in
60 the container ``my-new-container``:
64 Container container = account.getContainer("my-new-container");
65 StoredObject object = container.getObject("foo.txt");
66 object.uploadObject(new File("foo.txt"));
69 Add/Update Object Metadata
70 ==========================
72 This adds the metadata key-value pair ``key``:``value`` to the object named
73 ``foo.txt`` in the container ``my-new-container``:
77 Container container = account.getContainer("my-new-container");
78 StoredObject object = container.getObject("foo.txt");
79 Map<String, Object> metadata = new TreeMap<String, Object>();
80 metadata.put("key", "value");
81 object.setMetadata(metadata);
87 This gets a list of Containers that you own.
88 This also prints out the container name.
92 Collection<Container> containers = account.list();
93 for (Container currentContainer : containers) {
94 System.out.println(currentContainer.getName());
97 The output will look something like this::
104 List a Container's Content
105 ==========================
107 This gets a list of objects in the container ``my-new-container``; and, it also
108 prints out each object's name, the file size, and last modified date:
112 Container container = account.getContainer("my-new-container");
113 Collection<StoredObject> objects = container.list();
114 for (StoredObject currentObject : objects) {
115 System.out.println(currentObject.getName());
118 The output will look something like this::
124 Retrieve an Object's Metadata
125 =============================
127 This retrieves metadata and gets the MIME type for an object named ``foo.txt``
128 in a container named ``my-new-container``:
132 Container container = account.getContainer("my-new-container");
133 StoredObject object = container.getObject("foo.txt");
134 Map<String, Object> returnedMetadata = object.getMetadata();
135 for (String name : returnedMetadata.keySet()) {
136 System.out.println("META / "+name+": "+returnedMetadata.get(name));
143 This downloads the object ``foo.txt`` in the container ``my-new-container``
144 and saves it in ``./outfile.txt``:
148 Container container = account.getContainer("my-new-container");
149 StoredObject object = container.getObject("foo.txt");
150 object.downloadObject(new File("outfile.txt"));
156 This deletes the object ``goodbye.txt`` in the container "my-new-container":
160 Container container = account.getContainer("my-new-container");
161 StoredObject object = container.getObject("foo.txt");
168 This deletes a container named "my-new-container":
172 Container container = account.getContainer("my-new-container");
175 .. note:: The container must be empty! Otherwise it won't work!