Created
December 28, 2013 02:33
-
-
Save hernanliendo/8155436 to your computer and use it in GitHub Desktop.
GCS Backup File Reader Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.zupcat.sca.initializer; | |
import com.google.appengine.api.datastore.Entity; | |
import com.google.appengine.api.datastore.EntityTranslator; | |
import com.google.appengine.api.files.*; | |
import com.google.appengine.tools.development.testing.LocalBlobstoreServiceTestConfig; | |
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | |
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig; | |
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | |
import java.io.BufferedInputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.InputStream; | |
import java.nio.ByteBuffer; | |
public final class GCSBackupFileReader { | |
private final LocalDatastoreServiceTestConfig dsConfig; | |
private final LocalBlobstoreServiceTestConfig blobConfig; | |
private final LocalMemcacheServiceTestConfig memcacheConfig; | |
private final LocalServiceTestHelper helper; | |
public static void main(final String[] args) throws Exception { | |
final GCSBackupFileReader gcsBackupFileReader = new GCSBackupFileReader(); | |
gcsBackupFileReader.read(); | |
} | |
public GCSBackupFileReader() throws Exception { | |
dsConfig = new LocalDatastoreServiceTestConfig(); | |
dsConfig.setNoStorage(true); | |
dsConfig.setStoreDelayMs(1); | |
blobConfig = new LocalBlobstoreServiceTestConfig(); | |
blobConfig.setNoStorage(true); | |
memcacheConfig = new LocalMemcacheServiceTestConfig(); | |
helper = new LocalServiceTestHelper(dsConfig, blobConfig, memcacheConfig).setEnvIsAdmin(true).setEnvIsLoggedIn(true); | |
helper.setUp(); | |
} | |
public void read() throws Exception { | |
InputStream is = new BufferedInputStream(new FileInputStream(new File("/Users/hernan/Downloads/output-0"))); | |
FileService fileService = FileServiceFactory.getFileService(); | |
AppEngineFile file = fileService.createNewBlobFile("application/octet-stream"); | |
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true); | |
byte[] buffer = new byte[32768]; | |
while (is.read(buffer) != -1) { | |
writeChannel.write(ByteBuffer.wrap(buffer)); | |
} | |
writeChannel.closeFinally(); | |
RecordReadChannel rrc = fileService.openRecordReadChannel(file, false); | |
ByteBuffer bf; | |
while ((bf = rrc.readRecord()) != null) { | |
final Entity entity = EntityTranslator.createFromPbBytes(bf.array()); | |
System.err.println("entity key: [" + entity.getKey() + "]"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Files API is disabled. Further information: https://cloud.google.com/appengine/docs/deprecations/files_api
Has anyone used the appengine gcp client api to read a gcs backup file?