FileService (Google App Engine API for Java)

com.google.appengine.api.files

Interface FileService


  • Deprecated. 
    This api has been deprecated in favor of the App Engine GCS client.

    @Deprecated
    public interface FileService
    This is the interface for interacting with the Google App Engine File Service. A FileService instance is obtained via FileServiceFactory.getFileService(). Currently there are two file systems supported: BLOBSTORE and GS When writing files in GS (Google Storage), you have to first create a writable file handle by using createNewGSFile, append to it and when all done writing, you must finalize the file for it to persist. Typical usage to create a new file in Google Storage is as follows:
       
        FileService fileService = FileServiceFactory.getFileService();
        GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
            .setBucket("mybucket")
            .setKey("myfile")
            .setMimeType("text/html")
            .setAcl("public_read")
            .addUserMetadata("myfield1", "my field value");
        AppEngineFile writableFile =
            fileService.createNewGSFile(optionsBuilder.build());
        // Open a channel to write to it
        boolean lock = false;
        FileWriteChannel writeChannel =
            fileService.openWriteChannel(writableFile, lock);
        // Different standard Java ways of writing to the channel
        // are possible. Here we use a PrintWriter:
        PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
        out.println("The woods are lovely dark and deep.");
        out.println("But I have promises to keep.");
    
        // Close without finalizing and save the file path for writing later
        out.close();
        String path = writableFile.getFullPath();
    
        // Write more to the file in a separate request:
        writableFile = new AppEngineFile(path);
    
        // This time lock because we intend to finalize
        lock = true;
        writeChannel = fileService.openWriteChannel(writableFile, lock);
    
        // This time we write to the channel directly.
        writeChannel.write(ByteBuffer.wrap(
            "And miles to go before I sleep.".getBytes()));
    
        // Now finalize
        writeChannel.closeFinally();
        // At this point the file is visible in App Engine as:
        // "/gs/mybucket/myfile"
        // and to anybody on the Internet through Google Storage as:
        // (http://storage.googleapis.com/mybucket/myfile)
        // So reading it through Files API:
        String filename = "/gs/mybucket/myfile";
        AppEngineFile readableFile = new AppEngineFile(filename);
        FileReadChannel readChannel =
            fileService.openReadChannel(readableFile, false);
        // Again, different standard Java ways of reading from the channel.
        BufferedReader reader =
            new BufferedReader(Channels.newReader(readChannel, "UTF8"));
        String line = reader.readLine();
        // line = "The woods are lovely dark and deep."
        readChannel.close();
    See Also:
    App Engine GCS client documentation
    • Method Detail

      • createNewBlobFile

        AppEngineFile createNewBlobFile(java.lang.String mimeType)
                                 throws java.io.IOException
        Deprecated. 
        Creates a new empty file in the BlobStore of the specified mime-type and returns an AppEngineFile representing the file. The returned instance will have a file system of BLOBSTORE.
        Parameters:
        mimeType - the mime-type of the file to be created. This parameter may be used to inform the BlobStore of the mime-type for the file. The mime-type will be returned by the BlobStore in an HTTP response if the file is requested directly from the BlobStore using the blob-key.
        Returns:
        A AppEngineFile representing the newly created file.
        Throws:
        java.io.IOException - If there is any problem communicating with the backend system
      • createNewBlobFile

        AppEngineFile createNewBlobFile(java.lang.String mimeType,
                                        java.lang.String blobInfoUploadedFileName)
                                 throws java.io.IOException
        Deprecated. 
        Creates a new empty file in the BlobStore of the specified mime-type and returns an AppEngineFile representing the file. The returned instance will have a file system of BLOBSTORE.
        Parameters:
        mimeType - the mime-type of the file to be created. This parameter may be used to inform the BlobStore of the mime-type for the file. The mime-type will be returned by the BlobStore in an HTTP response if the file is requested directly from the BlobStore using the blob-key.
        blobInfoUploadedFileName - BlobStore will store this name in the BlobInfo's fileName field. This string will not be the name of the returned AppEngineFile. It will be returned by the BlobStore in an HTTP response if the file is requested directly from the BlobStore using the blob-key.
        Returns:
        A AppEngineFile representing the newly created file.
        Throws:
        java.io.IOException - If there is any problem communicating with the backend system
      • createNewGSFile

        AppEngineFile createNewGSFile(GSFileOptions fileOptions)
                               throws java.io.IOException
        Deprecated. 
        Creates a new writable file in Google Storage of the specified mime-type and returns an AppEngineFile representing the file. The returned instance can only be used for writing and must be finalized before reading it. The returned file will have a file system of GS. For complete write/read lifecycle, please refer to the comments at the top of this file.
        Parameters:
        fileOptions - GSFileOptions for creating a Google Storage file.
        Returns:
        A writable AppEngineFile.
        Throws:
        java.io.IOException - If there is any problem communicating with the backend system