Java Code Examples for com.google.appengine.api.files.FileService#createNewBlobFile()

The following examples show how to use com.google.appengine.api.files.FileService#createNewBlobFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskServlet.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
private void processFileWithContent( final PersistenceManager pm, final Key fileKey, final String fileTypeString ) throws IOException {
	LOGGER.fine( "File key: " + fileKey + ", file type: " + fileTypeString );
	
	final FileType fileType = FileType.fromString( fileTypeString );
	
	final FileMetaData fmd;
	try {
		fmd =  (FileMetaData) pm.getObjectById( FILE_TYPE_CLASS_MAP.get( fileType ), fileKey );
	} catch ( final JDOObjectNotFoundException jonfe ) {
		LOGGER.warning( "File not found! (Deleted?)" );
		return;
	}
	LOGGER.fine( "sha1: " + fmd.getSha1() );
	if ( fmd.getBlobKey() != null && fmd.getContent() == null ) {
		LOGGER.warning( "This file is already processed!" );
		return;
	}
	if ( fmd.getContent() == null ) {
		LOGGER.warning( "File does not have content!" );
		return;
	}
	
	// Store content in the Blobstore
	final FileService      fileService = FileServiceFactory.getFileService();
	final AppEngineFile    appeFile    = fileService.createNewBlobFile( FILE_TYPE_MIME_TYPE_MAP.get( fileType ), fmd.getSha1() );
	final FileWriteChannel channel     = fileService.openWriteChannel( appeFile, true );
	final ByteBuffer       bb          = ByteBuffer.wrap( fmd.getContent().getBytes() );
	while ( bb.hasRemaining() )
		channel.write( bb );
	channel.closeFinally();
	
	fmd.setBlobKey( fileService.getBlobKey( appeFile ) );
	fmd.setContent( null );
	
	// I do not catch exceptions (so the task will be retried)
}
 
Example 2
Source File: PrintServlet.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    log.info("Ping - " + req);

    if (requestHandler != null) {
        requestHandler.handleRequest(req);
    }

    lastRequest = req;

    final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    try {
        Entity entity = new Entity("Qwert");
        entity.setProperty("xyz", 123);
        Key key = ds.put(entity);

        entity = ds.get(key);
        log.info(entity.toString());

        FileService fs = FileServiceFactory.getFileService();
        AppEngineFile file = fs.createNewBlobFile("qwertfile");
        FileWriteChannel fwc = fs.openWriteChannel(file, false);
        try {
            log.info("b_l = " + fwc.write(ByteBuffer.wrap("qwert".getBytes())));
        } finally {
            fwc.close();
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
}
 
Example 3
Source File: BlobstoreTestBase.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
protected BlobKey writeNewBlobFile(String text) throws IOException {
    FileService fileService = FileServiceFactory.getFileService();
    AppEngineFile file = fileService.createNewBlobFile("text/plain", "uploadedText.txt");
    FileWriteChannel channel = fileService.openWriteChannel(file, true);
    try {
        channel.write(ByteBuffer.wrap(text.getBytes()));
    } finally {
        channel.closeFinally();
    }
    return fileService.getBlobKey(file);
}