@@ -127,40 +127,102 @@ Note: `setStdout(true)` is ignored if you do not have access to the algorithm so
127127
128128## Working with Data
129129
130- Manage your data stored within Algorithmia :
130+ The Algorithmia Java client also provides a way to manage both Algorithmia hosted data
131+ and data from Dropbox or S3 accounts that you' ve connected to you Algorithmia account.
132+
133+ This client provides a `DataFile` type (generally created by `client.file(uri)`)
134+ and a `DataDir` type (generally created by `client.dir(uri)`) that provide
135+ methods for managing your data.
136+
137+ ### Create directories
138+
139+ Create directories by instantiating a `DataDirectory` object and calling `create()`:
131140
132141```java
133- // Create a directory "foo"
134- DataDirectory foo = client. dir(" data://.my/foo" );
135- foo. create();
142+ DataDirectory robots = client.dir("data://.my/robots");
143+ robots.create();
136144
137- // Create a directory with specific ACL
138- DataDirectory fooLimited = client. dir(" data://.my/fooLimited" );
139- fooLimited. create(DataAcl . PRIVATE );
145+ DataDirectory dbxRobots = client.dir("dropbox://robots");
146+ dbxRobots.create();
147+ ```
148+
149+ ### Upload files to a directory
150+
151+ Upload files by calling `put` on a `DataFile` object, or by calling `putFile` on a `DataDirectory` object.
152+
153+ ```java
154+ DataDirectory robots = client.dir("data://.my/robots");
155+
156+ // Upload local file
157+ robots.putFile(new File("/path/to/Optimus_Prime.png"));
158+ // Write a text file
159+ robots.file("Optimus_Prime.txt").put("Leader of the Autobots");
160+ // Write a binary file
161+ robots.file("Optimus_Prime.key").put(new byte[] { (byte)0xe0, 0x4f, (byte)0xd0, 0x20 });
162+ ```
163+
164+ ### Download contents of file
140165
141- // Or, update the directory's ACL after creation
142- fooLimited. updatePermissions(DataAcl . PRIVATE );
166+ Download files by calling `getString`, `getBytes`, or `getFile` on a DataFile object:
167+
168+ ```java
169+ DataDirectory robots = client.dir("data://.my/robots");
170+
171+ // Download file and get the file handle
172+ File t800File = robots.file("T-800.png").getFile();
173+
174+ // Get the file' s contents as a string
175+ String t800Text = robots. file(" T-800.txt" ). getString();
176+
177+ // Get the file's contents as a byte array
178+ byte [] t800Bytes = robots. file(" T-800.png" ). getBytes();
179+ ```
143180
144- // View the directory's permissions
145- fooLimited. getPermissions(). getReadPermissions() == DataAclType . PRIVATE
181+ ### Delete files and directories
146182
147- // Upload files to "foo" directory
148- foo. file(" sample.txt" ). put(" sample text contents" );
149- foo. file(" binary_file" ). put(new byte [] { (byte )0xe0 , 0x4f , (byte )0xd0 , 0x20 });
150- foo. putFile(new File (" /path/to/myfile" ));
183+ Delete files and directories by calling `delete` on their respective `DataFile ` or `DataDirectory ` object.
184+ `DataDirectories ` take an optional `force` parameter that indicates whether the directory should be deleted
185+ if it contains files or other directories.
151186
152- // List files in "foo"
153- for (DataFile file : foo. getFileIter()) {
154- System . out. println(file. toString() " at URL: " + file. url());
187+ ```java
188+ client. file(" data://.my/robots/C-3PO.txt" ). delete();
189+ client. dir(" data://.my/robots" ). delete(false );
190+ ```
191+
192+ ### List directory contents
193+
194+ Iterate over the contents of a directory using the iterated returned by calling `files`, or `dirs` on a `DataDirectory ` object:
195+
196+ ```java
197+ // List top level directories
198+ DataDirectory myRoot = client. dir(" data://.my" );
199+ for (DataDirectory dir : myRoot. dirs()) {
200+ System . out. println(" Directory " + dir. toString() + " at URL " + dir. url());
201+ }
202+
203+ // List files in the 'robots' directory
204+ DataDirectory robots = client. dir(" data://.my/robots" );
205+ for (DataFile file : robots. files()) {
206+ System . out. println(" File " + file. toString() + " at URL: " + file. url());
155207}
208+ ```
156209
157- // Get contents of files
158- String sampleText = foo. file(" sample.txt" ). getString();
159- byte [] binaryContent = foo. file(" binary_file" ). getBytes();
160- File tempFile = foo. file(" myfile" ). getFile();
210+ ### Manage directory permissions
161211
162- // Delete files and directories
163- foo. file(" sample.txt" ). delete();
164- foo. delete(true ); // true implies force deleting the directory and its contents
212+ Directory permissions may be set when creating a directory, or may be updated on already existing directories.
213+
214+ ```java
215+ DataDirectory fooLimited = client. dir(" data://.my/fooLimited" );
216+
217+ // Create the directory as private
218+ fooLimited. create(DataAcl . PRIVATE );
219+
220+ // Update a directory to be public
221+ fooLimited. updatePermissions(DataAcl . PUBLIC );
222+
223+ // Check a directory's permissions
224+ if (fooLimited. getPermissions(). getReadPermissions() == DataAclType . PRIVATE ) {
225+ System . out. println(" fooLimited is private" );
226+ }
165227```
166228
0 commit comments