Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / doc / radosgw / swift / java.rst
1 .. _java_swift:
2
3 =====================
4  Java Swift Examples
5 =====================
6
7 Setup
8 =====
9
10 The following examples may require some or all of the following Java
11 classes to be imported:
12
13 .. code-block:: java
14
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;
21        import java.io.File;
22        import java.io.IOException;
23        import java.util.*;
24
25
26 Create a Connection
27 ===================
28
29 This creates a connection so that you can interact with the server:
30
31 .. code-block:: java
32
33        String username = "USERNAME";
34        String password = "PASSWORD";
35        String authUrl  = "https://radosgw.endpoint/auth/1.0";
36
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();
43
44
45 Create a Container
46 ==================
47
48 This creates a new container called ``my-new-container``:
49
50 .. code-block:: java
51
52        Container container = account.getContainer("my-new-container");
53        container.create();
54
55
56 Create an Object
57 ================
58
59 This creates an object ``foo.txt`` from the file named ``foo.txt`` in 
60 the container ``my-new-container``:
61
62 .. code-block:: java
63
64        Container container = account.getContainer("my-new-container");
65        StoredObject object = container.getObject("foo.txt");
66        object.uploadObject(new File("foo.txt"));
67
68
69 Add/Update Object Metadata
70 ==========================
71
72 This adds the metadata key-value pair ``key``:``value`` to the object named
73 ``foo.txt`` in the container ``my-new-container``:
74
75 .. code-block:: java
76
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);
82
83
84 List Owned Containers
85 =====================
86
87 This gets a list of Containers that you own.
88 This also prints out the container name.
89
90 .. code-block:: java
91
92        Collection<Container> containers = account.list();
93        for (Container currentContainer : containers) {
94            System.out.println(currentContainer.getName());
95        }
96
97 The output will look something like this::
98
99         mahbuckat1
100         mahbuckat2
101         mahbuckat3
102
103
104 List a Container's Content
105 ==========================
106
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:
109
110 .. code-block:: java
111
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());
116        }
117
118 The output will look something like this::
119
120    myphoto1.jpg
121    myphoto2.jpg
122
123
124 Retrieve an Object's Metadata
125 =============================
126
127 This retrieves metadata and gets the MIME type for an object named ``foo.txt``
128 in a container named ``my-new-container``:
129
130 .. code-block:: java
131
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));
137        }
138
139
140 Retrieve an Object
141 ==================
142
143 This downloads the object ``foo.txt`` in the container ``my-new-container`` 
144 and saves it in ``./outfile.txt``:
145
146 .. code-block:: java
147
148        Container container = account.getContainer("my-new-container");
149        StoredObject object = container.getObject("foo.txt");
150        object.downloadObject(new File("outfile.txt"));
151
152
153 Delete an Object
154 ================
155
156 This deletes the object ``goodbye.txt`` in the container "my-new-container":
157
158 .. code-block:: java
159
160        Container container = account.getContainer("my-new-container");
161        StoredObject object = container.getObject("foo.txt");
162        object.delete();
163
164
165 Delete a Container
166 ==================
167
168 This deletes a container named "my-new-container": 
169
170 .. code-block:: java
171
172        Container container = account.getContainer("my-new-container");
173        container.delete();
174         
175 .. note:: The container must be empty! Otherwise it won't work!