summaryrefslogtreecommitdiffstats
path: root/src/ceph/doc/radosgw/swift/java.rst
blob: 8977a3b16b1950eae1c428c0a6a6392ec637b450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
.. _java_swift:

=====================
 Java Swift Examples
=====================

Setup
=====

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

.. code-block:: java

       import org.javaswift.joss.client.factory.AccountConfig;
       import org.javaswift.joss.client.factory.AccountFactory;
       import org.javaswift.joss.client.factory.AuthenticationMethod;
       import org.javaswift.joss.model.Account;
       import org.javaswift.joss.model.Container;
       import org.javaswift.joss.model.StoredObject;
       import java.io.File;
       import java.io.IOException;
       import java.util.*;


Create a Connection
===================

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

.. code-block:: java

       String username = "USERNAME";
       String password = "PASSWORD";
       String authUrl  = "https://radosgw.endpoint/auth/1.0";

       AccountConfig config = new AccountConfig();
       config.setUsername(username);
       config.setPassword(password);
       config.setAuthUrl(authUrl);
       config.setAuthenticationMethod(AuthenticationMethod.BASIC);
       Account account = new AccountFactory(config).createAccount();


Create a Container
==================

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

.. code-block:: java

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


Create an Object
================

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

.. code-block:: java

       Container container = account.getContainer("my-new-container");
       StoredObject object = container.getObject("foo.txt");
       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``:

.. code-block:: java

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


List Owned Containers
=====================

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

.. code-block:: java

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

The output will look something like this::

	mahbuckat1
	mahbuckat2
	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:

.. code-block:: java

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

The output will look something like this::

   myphoto1.jpg
   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``:

.. code-block:: java

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


Retrieve an Object
==================

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

.. code-block:: java

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


Delete an Object
================

This deletes the object ``goodbye.txt`` in the container "my-new-container":

.. code-block:: java

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


Delete a Container
==================

This deletes a container named "my-new-container": 

.. code-block:: java

       Container container = account.getContainer("my-new-container");
       container.delete();
	
.. note:: The container must be empty! Otherwise it won't work!