Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / files / FileService.java
blobf48ffd8e8001d4e740365bdf0d73e23375277879
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.files;
5 import com.google.appengine.api.blobstore.BlobKey;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
10 /**
11 * This is the interface for interacting with the Google App Engine File
12 * Service. A {@code FileService} instance is obtained via
13 * {@link FileServiceFactory#getFileService()}.
15 * Currently there are two file systems supported:
16 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
17 * BLOBSTORE}
18 * and
19 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS
20 * GS}
22 * When writing files in GS (Google Storage), you have to first create a
23 * writable file handle by using {@link createNewGSFile}, append to it and
24 * when all done writing, you must finalize the file for it to persist.
25 * Typical usage to create a new file in Google Storage is as follows:
27 * <pre> {@code
28 * FileService fileService = FileServiceFactory.getFileService();
29 * GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
30 * .setBucket("mybucket")
31 * .setKey("myfile")
32 * .setMimeType("text/html")
33 * .setAcl("public_read")
34 * .addUserMetadata("myfield1", "my field value");
35 * AppEngineFile writableFile =
36 * fileService.createNewGSFile(optionsBuilder.build());
37 * // Open a channel to write to it
38 * boolean lock = false;
39 * FileWriteChannel writeChannel =
40 * fileService.openWriteChannel(writableFile, lock);
41 * // Different standard Java ways of writing to the channel
42 * // are possible. Here we use a PrintWriter:
43 * PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
44 * out.println("The woods are lovely dark and deep.");
45 * out.println("But I have promises to keep.");
47 * // Close without finalizing and save the file path for writing later
48 * out.close();
49 * String path = writableFile.getFullPath();
51 * // Write more to the file in a separate request:
52 * writableFile = new AppEngineFile(path);
54 * // This time lock because we intend to finalize
55 * lock = true;
56 * writeChannel = fileService.openWriteChannel(writableFile, lock);
58 * // This time we write to the channel directly.
59 * writeChannel.write(ByteBuffer.wrap(
60 * "And miles to go before I sleep.".getBytes()));
62 * // Now finalize
63 * writeChannel.closeFinally();
64 * // At this point the file is visible in App Engine as:
65 * // "/gs/mybucket/myfile"
66 * // and to anybody on the Internet through Google Storage as:
67 * // (http://storage.googleapis.com/mybucket/myfile)
68 * // So reading it through Files API:
69 * String filename = "/gs/mybucket/myfile";
70 * AppEngineFile readableFile = new AppEngineFile(filename);
71 * FileReadChannel readChannel =
72 * fileService.openReadChannel(readableFile, false);
73 * // Again, different standard Java ways of reading from the channel.
74 * BufferedReader reader =
75 * new BufferedReader(Channels.newReader(readChannel, "UTF8"));
76 * String line = reader.readLine();
77 * // line = "The woods are lovely dark and deep."
78 * readChannel.close();}</pre>
81 * @deprecated This api has been deprecated in favor of the App Engine GCS client.
82 * @see <a href="https://developers.google.com/appengine/docs/java/googlecloudstorageclient/">
83 * App Engine GCS client documentation</a>
85 @Deprecated
86 public interface FileService {
88 /**
89 * Creates a new empty file in the BlobStore of the specified mime-type and
90 * returns an {@code AppEngineFile} representing the file. The returned
91 * instance will have a {@link AppEngineFile#getFileSystem() file system} of
92 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
93 * BLOBSTORE}.
95 * @param mimeType the mime-type of the file to be created. This parameter may
96 * be used to inform the BlobStore of the mime-type for the file. The
97 * mime-type will be returned by the BlobStore in an HTTP response if
98 * the file is requested directly from the BlobStore using the
99 * blob-key.
100 * @return A {@code AppEngineFile} representing the newly created file.
101 * @throws IOException If there is any problem communicating with the backend
102 * system
104 AppEngineFile createNewBlobFile(String mimeType) throws IOException;
107 * Creates a new empty file in the BlobStore of the specified mime-type and
108 * returns an {@code AppEngineFile} representing the file. The returned
109 * instance will have a {@link AppEngineFile#getFileSystem() file system} of
110 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
111 * BLOBSTORE}.
113 * @param mimeType the mime-type of the file to be created. This parameter may
114 * be used to inform the BlobStore of the mime-type for the file. The
115 * mime-type will be returned by the BlobStore in an HTTP response if
116 * the file is requested directly from the BlobStore using the
117 * blob-key.
118 * @param blobInfoUploadedFileName BlobStore will store this name in the
119 * BlobInfo's fileName field. This string will <em>not</em> be
120 * the {@link AppEngineFile#getNamePart() name} of the returned
121 * {@code AppEngineFile}. It will be returned by the BlobStore in an HTTP
122 * response if the file is requested directly from the BlobStore using
123 * the blob-key.
124 * @return A {@code AppEngineFile} representing the newly created file.
125 * @throws IOException If there is any problem communicating with the backend
126 * system
128 AppEngineFile createNewBlobFile(String mimeType, String blobInfoUploadedFileName)
129 throws IOException;
132 * Creates a new writable file in Google Storage of the specified mime-type
133 * and returns an {@code AppEngineFile} representing the file. The returned
134 * instance can only be used for writing and must be
135 * {@link RecordWriteChannel#closeFinally() finalized} before reading it.
136 * The returned file will have a
137 * {@link AppEngineFile#getFileSystem() file system} of
138 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS GS}.
139 * For complete write/read lifecycle, please refer to the comments at the
140 * top of this file.
142 * @param fileOptions {@code GSFileOptions} for creating a Google Storage
143 * file.
144 * @return A writable {@code AppEngineFile}.
145 * @throws IOException If there is any problem communicating with the backend
146 * system
148 AppEngineFile createNewGSFile(final GSFileOptions fileOptions) throws IOException;
151 * Given an {@code AppEngineFile}, returns a {@code FileWriteChannel} that may
152 * be used for appending bytes to the file.
154 * @param file the file to which to append bytes. The file must exist and it
155 * must not yet have been finalized. Furthermore, if the file is a
156 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS GS}
157 * file then it must be {@link AppEngineFile#isWritable() writable}.
158 * @param lock should the file be locked for exclusive access?
159 * @throws FileNotFoundException if the file does not exist in the backend
160 * repository. The file may have been deleted by another request, or
161 * the file may have been lost due to system failure or a scheduled
162 * relocation. Each backend repository offers different guarantees
163 * regarding when this is possible.
164 * @throws FinalizationException if the file has already been finalized. The
165 * file may have been finalized by another request.
166 * @throws LockException if the file is locked in a different App Engine
167 * request, or if {@code lock = true} and the file is opened in a
168 * different App Engine request
169 * @throws IOException if any other unexpected problem occurs
171 FileWriteChannel openWriteChannel(AppEngineFile file, boolean lock)
172 throws FileNotFoundException, FinalizationException, LockException, IOException;
175 * Given an {@code AppEngineFile}, returns a {@code FileReadChannel} that may
176 * be used for reading bytes from the file.
178 * @param file The file from which to read bytes. The file must exist and it
179 * must have been finalized. Furthermore, if the file is a
180 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS GS}
181 * file then it must be {@link AppEngineFile#isReadable() readable}.
182 * @param lock Should the file be locked for exclusive access?
183 * @throws FileNotFoundException if the file does not exist in the backend
184 * repository. The file may have been deleted by another request, or
185 * the file may have been lost due to system failure or a scheduled
186 * relocation. Each backend repository offers different guarantees
187 * regarding when this is possible.
188 * @throws FinalizationException if the file has not yet been finalized
189 * @throws LockException if the file is locked in a different App Engine
190 * request, or if {@code lock = true} and the file is opened in a
191 * different App Engine request
192 * @throws IOException if any other problem occurs contacting the backend
193 * system
195 FileReadChannel openReadChannel(AppEngineFile file, boolean lock)
196 throws FileNotFoundException, LockException, IOException;
199 * Given a
200 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
201 * BLOBSTORE} file that has been finalized, returns the {@code BlobKey} for
202 * the corresponding blob.
204 * @param file A {@link
205 * com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
206 * BLOBSTORE} file that has been finalized
207 * @return The corresponding {@code BlobKey}, or {@code null} if none can be
208 * found. This can occur if the file has not been finalized, or if it
209 * does not exist.
210 * @throws IllegalArgumentException if {@code file} is not a {@code BLOBSTORE}
211 * file.
213 BlobKey getBlobKey(AppEngineFile file);
216 * Given a {@code BlobKey}, returns an instance of {@code AppEngineFile} with
217 * the given key. This method does not check whether the file actually exists
218 * although the file corresponding to the key should be a finalized one.
220 * @param blobKey A blobkey
221 * @return an instance of {@code AppEngineFile} with the given key.
223 AppEngineFile getBlobFile(BlobKey blobKey);
226 * Given an {@link AppEngineFile} returns a {@link RecordWriteChannel} that may be used for
227 * writing to the file using the leveldb log format.
229 * @param file the file to which to write records. The file must exist, be closed, and it
230 * must not yet have been finalized.
231 * @param lock should the file be locked for exclusive access?
232 * @throws FileNotFoundException if the file does not exist in the backend
233 * repository. The file may have been deleted by another request, or
234 * the file may have been lost due to system failure or a scheduled
235 * relocation. Each backend repository offers different guarantees
236 * regarding when this is possible.
237 * @throws FinalizationException if the file has already been finalized. The
238 * file may have been finalized by another request.
239 * @throws LockException if the file is locked in a different App Engine
240 * request, or if {@code lock = true} and the file is opened in a
241 * different App Engine request
242 * @throws IOException if any other unexpected problem occurs
244 RecordWriteChannel openRecordWriteChannel(AppEngineFile file, boolean lock)
245 throws FileNotFoundException, FinalizationException, LockException, IOException;
248 * Given an {@link AppEngineFile}, returns a {@link RecordReadChannel} that may
249 * be used for reading records from a file written using the leveldb log format.
251 * @param file The file from which to read records. The file must exist, be closed, and it
252 * must have been finalized.
253 * @param lock Should the file be locked for exclusive access?
254 * @throws FileNotFoundException if the file does not exist in the backend
255 * repository. The file may have been deleted by another request, or
256 * the file may have been lost due to system failure or a scheduled
257 * relocation. Each backend repository offers different guarantees
258 * regarding when this is possible.
259 * @throws FinalizationException if the file has not yet been finalized
260 * @throws LockException if the file is locked in a different App Engine
261 * request, or if {@code lock = true} and the file is opened in a
262 * different App Engine request
263 * @throws IOException if any other problem occurs contacting the backend
264 * system
266 RecordReadChannel openRecordReadChannel(AppEngineFile file, boolean lock)
267 throws FileNotFoundException, LockException, IOException;
270 * Returns a string that represents the default Google Storage bucket name
271 * for the application.
273 * @throws IOException if any other problem occurs contacting the backend
274 * system
276 String getDefaultGsBucketName() throws IOException;
279 * Given a finalized {@link AppEngineFile}, return a {@link FileStat} object
280 * that contains information about it.
282 * Limitations in current implementation:
283 * <ol>
284 * <li>File must be finalized before stat can be called.</li>
285 * <li>Only {@code filename}, {@code finalized}, and {@code length} are filled
286 * in the {@link FileStat} returned.</li>
287 * </ol>
289 * @param file the appEngfile to fetch file stat.
290 * @return {@link FileStat} object that has metadata about the appEngineFile.
291 * @throws FileNotFoundException if the file does not exist in the backend
292 * repository. The file may have been deleted by another request, or
293 * the file may have been lost due to system failure or a scheduled
294 * relocation. Each backend repository offers different guarantees
295 * regarding when this is possible.
296 * @throws FinalizationException if the file has not yet been finalized.
297 * @throws LockException if the file is locked in a different App Engine
298 * request.
299 * @throws IOException if any other problem occurs contacting the backend
300 * system.
302 FileStat stat(AppEngineFile file) throws IOException;
305 * Given {@link AppEngineFile}s with finalized filename,
306 * permanently delete them in bulk.
308 * Delete on non-existent/non-finalized files is a no-op. Thus a file is
309 * guaranteed to be non-existent if no exception is thrown after delete.
311 * After validating file type, this method tries to delete as many files as
312 * possible and will throw an exception in the end if any single deletion failed.
314 * @param files to delete.
315 * @throws NullPointerException if files is {@code null} or any file is {@code null}.
316 * @throws UnsupportedOperationException if a file's type is not supported by delete
317 * or file does not have a finalized name.
318 * @throws IOException if any other problem occurs contacting the backend system.
320 void delete(AppEngineFile... files) throws IOException;