Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / files / FileService.java
blob0c2d991e336af3fb4d402cd959ff6e26fa5eb453
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 public interface FileService {
83 /**
84 * Creates a new empty file in the BlobStore of the specified mime-type and
85 * returns an {@code AppEngineFile} representing the file. The returned
86 * instance will have a {@link AppEngineFile#getFileSystem() file system} of
87 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
88 * BLOBSTORE}.
90 * @param mimeType the mime-type of the file to be created. This parameter may
91 * be used to inform the BlobStore of the mime-type for the file. The
92 * mime-type will be returned by the BlobStore in an HTTP response if
93 * the file is requested directly from the BlobStore using the
94 * blob-key.
95 * @return A {@code AppEngineFile} representing the newly created file.
96 * @throws IOException If there is any problem communicating with the backend
97 * system
99 AppEngineFile createNewBlobFile(String mimeType) throws IOException;
102 * Creates a new empty file in the BlobStore of the specified mime-type and
103 * returns an {@code AppEngineFile} representing the file. The returned
104 * instance will have a {@link AppEngineFile#getFileSystem() file system} of
105 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
106 * BLOBSTORE}.
108 * @param mimeType the mime-type of the file to be created. This parameter may
109 * be used to inform the BlobStore of the mime-type for the file. The
110 * mime-type will be returned by the BlobStore in an HTTP response if
111 * the file is requested directly from the BlobStore using the
112 * blob-key.
113 * @param blobInfoUploadedFileName BlobStore will store this name in the
114 * BlobInfo's fileName field. This string will <em>not</em> be
115 * the {@link AppEngineFile#getNamePart() name} of the returned
116 * {@code AppEngineFile}. It will be returned by the BlobStore in an HTTP
117 * response if the file is requested directly from the BlobStore using
118 * the blob-key.
119 * @return A {@code AppEngineFile} representing the newly created file.
120 * @throws IOException If there is any problem communicating with the backend
121 * system
123 AppEngineFile createNewBlobFile(String mimeType, String blobInfoUploadedFileName)
124 throws IOException;
127 * Creates a new writable file in Google Storage of the specified mime-type
128 * and returns an {@code AppEngineFile} representing the file. The returned
129 * instance can only be used for writing and must be
130 * {@link RecordWriteChannel#closeFinally() finalized} before reading it.
131 * The returned file will have a
132 * {@link AppEngineFile#getFileSystem() file system} of
133 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS GS}.
134 * For complete write/read lifecycle, please refer to the comments at the
135 * top of this file.
137 * @param fileOptions {@code GSFileOptions} for creating a Google Storage
138 * file.
139 * @return A writable {@code AppEngineFile}.
140 * @throws IOException If there is any problem communicating with the backend
141 * system
143 AppEngineFile createNewGSFile(final GSFileOptions fileOptions) throws IOException;
146 * Given an {@code AppEngineFile}, returns a {@code FileWriteChannel} that may
147 * be used for appending bytes to the file.
149 * @param file the file to which to append bytes. The file must exist and it
150 * must not yet have been finalized. Furthermore, if the file is a
151 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS GS}
152 * file then it must be {@link AppEngineFile#isWritable() writable}.
153 * @param lock should the file be locked for exclusive access?
154 * @throws FileNotFoundException if the file does not exist in the backend
155 * repository. The file may have been deleted by another request, or
156 * the file may have been lost due to system failure or a scheduled
157 * relocation. Each backend repository offers different guarantees
158 * regarding when this is possible.
159 * @throws FinalizationException if the file has already been finalized. The
160 * file may have been finalized by another request.
161 * @throws LockException if the file is locked in a different App Engine
162 * request, or if {@code lock = true} and the file is opened in a
163 * different App Engine request
164 * @throws IOException if any other unexpected problem occurs
166 FileWriteChannel openWriteChannel(AppEngineFile file, boolean lock)
167 throws FileNotFoundException, FinalizationException, LockException, IOException;
170 * Given an {@code AppEngineFile}, returns a {@code FileReadChannel} that may
171 * be used for reading bytes from the file.
173 * @param file The file from which to read bytes. The file must exist and it
174 * must have been finalized. Furthermore, if the file is a
175 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#GS GS}
176 * file then it must be {@link AppEngineFile#isReadable() readable}.
177 * @param lock Should the file be locked for exclusive access?
178 * @throws FileNotFoundException if the file does not exist in the backend
179 * repository. The file may have been deleted by another request, or
180 * the file may have been lost due to system failure or a scheduled
181 * relocation. Each backend repository offers different guarantees
182 * regarding when this is possible.
183 * @throws FinalizationException if the file has not yet been finalized
184 * @throws LockException if the file is locked in a different App Engine
185 * request, or if {@code lock = true} and the file is opened in a
186 * different App Engine request
187 * @throws IOException if any other problem occurs contacting the backend
188 * system
190 FileReadChannel openReadChannel(AppEngineFile file, boolean lock)
191 throws FileNotFoundException, LockException, IOException;
194 * Given a
195 * {@link com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
196 * BLOBSTORE} file that has been finalized, returns the {@code BlobKey} for
197 * the corresponding blob.
199 * @param file A {@link
200 * com.google.appengine.api.files.AppEngineFile.FileSystem#BLOBSTORE
201 * BLOBSTORE} file that has been finalized
202 * @return The corresponding {@code BlobKey}, or {@code null} if none can be
203 * found. This can occur if the file has not been finalized, or if it
204 * does not exist.
205 * @throws IllegalArgumentException if {@code file} is not a {@code BLOBSTORE}
206 * file.
208 BlobKey getBlobKey(AppEngineFile file);
211 * Given a {@code BlobKey}, returns an instance of {@code AppEngineFile} with
212 * the given key. This method does not check whether the file actually exists
213 * although the file corresponding to the key should be a finalized one.
215 * @param blobKey A blobkey
216 * @return an instance of {@code AppEngineFile} with the given key.
218 AppEngineFile getBlobFile(BlobKey blobKey);
221 * Given an {@link AppEngineFile} returns a {@link RecordWriteChannel} that may be used for
222 * writing to the file using the leveldb log format.
224 * @param file the file to which to write records. The file must exist, be closed, and it
225 * must not yet have been finalized.
226 * @param lock should the file be locked for exclusive access?
227 * @throws FileNotFoundException if the file does not exist in the backend
228 * repository. The file may have been deleted by another request, or
229 * the file may have been lost due to system failure or a scheduled
230 * relocation. Each backend repository offers different guarantees
231 * regarding when this is possible.
232 * @throws FinalizationException if the file has already been finalized. The
233 * file may have been finalized by another request.
234 * @throws LockException if the file is locked in a different App Engine
235 * request, or if {@code lock = true} and the file is opened in a
236 * different App Engine request
237 * @throws IOException if any other unexpected problem occurs
239 RecordWriteChannel openRecordWriteChannel(AppEngineFile file, boolean lock)
240 throws FileNotFoundException, FinalizationException, LockException, IOException;
243 * Given an {@link AppEngineFile}, returns a {@link RecordReadChannel} that may
244 * be used for reading records from a file written using the leveldb log format.
246 * @param file The file from which to read records. The file must exist, be closed, and it
247 * must have been finalized.
248 * @param lock Should the file be locked for exclusive access?
249 * @throws FileNotFoundException if the file does not exist in the backend
250 * repository. The file may have been deleted by another request, or
251 * the file may have been lost due to system failure or a scheduled
252 * relocation. Each backend repository offers different guarantees
253 * regarding when this is possible.
254 * @throws FinalizationException if the file has not yet been finalized
255 * @throws LockException if the file is locked in a different App Engine
256 * request, or if {@code lock = true} and the file is opened in a
257 * different App Engine request
258 * @throws IOException if any other problem occurs contacting the backend
259 * system
261 RecordReadChannel openRecordReadChannel(AppEngineFile file, boolean lock)
262 throws FileNotFoundException, LockException, IOException;
265 * Returns a string that represents the default Google Storage bucket name
266 * for the application.
268 * @throws IOException if any other problem occurs contacting the backend
269 * system
271 String getDefaultGsBucketName() throws IOException;
274 * Given a finalized {@link AppEngineFile}, return a {@link FileStat} object
275 * that contains information about it.
277 * Limitations in current implementation:
278 * <ol>
279 * <li>File must be finalized before stat can be called.</li>
280 * <li>Only {@code filename}, {@code finalized}, and {@code length} are filled
281 * in the {@link FileStat} returned.</li>
282 * </ol>
284 * @param file the appEngfile to fetch file stat.
285 * @return {@link FileStat} object that has metadata about the appEngineFile.
286 * @throws FileNotFoundException if the file does not exist in the backend
287 * repository. The file may have been deleted by another request, or
288 * the file may have been lost due to system failure or a scheduled
289 * relocation. Each backend repository offers different guarantees
290 * regarding when this is possible.
291 * @throws FinalizationException if the file has not yet been finalized.
292 * @throws LockException if the file is locked in a different App Engine
293 * request.
294 * @throws IOException if any other problem occurs contacting the backend
295 * system.
297 FileStat stat(AppEngineFile file) throws IOException;
300 * Given {@link AppEngineFile}s with finalized filename,
301 * permanently delete them in bulk.
303 * Delete on non-existent/non-finalized files is a no-op. Thus a file is
304 * guaranteed to be non-existent if no exception is thrown after delete.
306 * After validating file type, this method tries to delete as many files as
307 * possible and will throw an exception in the end if any single deletion failed.
309 * @param files to delete.
310 * @throws NullPointerException if files is {@code null} or any file is {@code null}.
311 * @throws UnsupportedOperationException if a file's type is not supported by delete
312 * or file does not have a finalized name.
313 * @throws IOException if any other problem occurs contacting the backend system.
315 void delete(AppEngineFile... files) throws IOException;