com.google.appengine.api.files.FileService Java Examples

The following examples show how to use com.google.appengine.api.files.FileService. 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: TestServlet.java    From wt1 with Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
	try {
	System.out.println(newFileName());
	 FileService fileService = FileServiceFactory.getFileService();
	GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
	.setBucket("wt1-test1")
	.setKey(newFileName());

	AppEngineFile aef = fileService.createNewGSFile(optionsBuilder.build());
	
	FileWriteChannel ch = fileService.openWriteChannel(aef, true);
	
	Channels.newWriter(ch, "utf8").append("pouet").flush();
	
	ch.closeFinally();
	System.out.println("SUCCESS **************");
	} catch (Exception e) {
		System.out.println("FAILURE **************************");
		e.printStackTrace();
	}
}
 
Example #2
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the content of the specified file
 * @param fileMetaData file meta data of the file
 * @param fileService  optionally you can pass the file service reference if you already have it
 * @return the content of the specified file or <code>null</code> if the file could not be found
 * @throws IOException thrown if reading the file from from the Blobstore fails
 */
public static byte[] getFileContent( final FileMetaData fileMetaData, FileService fileService ) throws IOException {
	if (true) {
		return null;
	}
	
	if ( fileMetaData.getContent() != null ) {
		// File content is in the file meta data
		return fileMetaData.getContent().getBytes();
	}
	else {
		// Get content from the Blobstore
		if ( fileService == null )
			fileService = FileServiceFactory.getFileService();
		
		// final AppEngineFile   appeFile = fileService.getBlobFile( file.getBlobKey() ); // This code throws exception on migrated blobs!
		final AppEngineFile   appeFile = new AppEngineFile( FileSystem.BLOBSTORE, fileMetaData.getBlobKey().getKeyString() );
		final FileReadChannel channel  = fileService.openReadChannel( appeFile, false );
		final byte[]          content  = new byte[ (int) fileMetaData.getSize() ];
		final ByteBuffer      wrapper  = ByteBuffer.wrap( content );
		
		while ( channel.read( wrapper ) > 0 )
			;
		
		channel.close();
		
		return content;
	}
}
 
Example #3
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 #4
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 #5
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);
}
 
Example #6
Source File: TaskServlet.java    From sc2gears with Apache License 2.0 4 votes vote down vote up
private void customTask( final HttpServletRequest request, final PersistenceManager pm ) throws IOException {
	LOGGER.fine( "Key: " + request.getParameter( "key" ) + ", file type: " + request.getParameter( "fileType" ) );
	
	final FileType fileType = FileType.fromString( request.getParameter( "fileType" ) );
	if ( fileType == null ) {
		LOGGER.severe( "Invalid File type!" );
		return;
	}
	
	final Key key = KeyFactory.stringToKey( request.getParameter( "key" ) );
	
	final DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
       final Entity e;
	try {
        e = ds.get( key );
       } catch ( final EntityNotFoundException enfe ) {
		LOGGER.log( Level.WARNING, "Entity not found!", enfe );
        return;
       }
	
       if ( !e.getKind().equals( "Rep" ) && !e.getKind().equals( "Smpd" ) && !e.getKind().equals( "OtherFile" ) ) {
		LOGGER.severe( "Invalid Entity kind:" + e.getKind() );
		return;
       }
       
       if ( (Long) e.getProperty( "v" ) == 4 ) {
		LOGGER.warning( "Entity is already v4!" );
		return;
	}
       
       // Update common properties:
       // TODO
       final int size = ( (Long) e.getProperty( "size" ) ).intValue();
       if ( size < FileServlet.DATASTORE_CONTENT_STORE_LIMIT ) {
           final FileService fileService = FileServiceFactory.getFileService();
   		final AppEngineFile   appeFile = new AppEngineFile( FileSystem.BLOBSTORE, ( (BlobKey) e.getProperty( "blobKey" ) ).getKeyString() );
   		final FileReadChannel channel  = fileService.openReadChannel( appeFile, false );
   		final byte[]          content  = new byte[ size ];
   		final ByteBuffer      wrapper  = ByteBuffer.wrap( content );
   		while ( channel.read( wrapper ) > 0 )
   			;
   		channel.close();
   		
   		e.setProperty( "content", new Blob( content ) );
   		e.setProperty( "blobKey", null );
   		fileService.delete( appeFile );
       }
       e.setUnindexedProperty( "blobKey", e.getProperty( "blobKey" ) );
       e.setUnindexedProperty( "content", e.getProperty( "content" ) );
       
       switch ( fileType ) {
       case SC2REPLAY :
           e.setUnindexedProperty( "matchup", e.getProperty( "matchup" ) );
       	break;
       case MOUSE_PRINT :
       	break;
       case OTHER :
       	break;
       default:
       	throw new RuntimeException( "Invalid file type!" );
       }
       
       // UPGRADE COMPLETE!
	e.setProperty( "v", 4 );
	ds.put( e );
}
 
Example #7
Source File: BlobstoreUploadTestBase.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private AppEngineFile getAppEngineFile(BlobKey blobKey) {
    FileService fileService = FileServiceFactory.getFileService();
    return fileService.getBlobFile(blobKey);
}