Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hernanliendo
Created December 28, 2013 02:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hernanliendo/8155436 to your computer and use it in GitHub Desktop.
Save hernanliendo/8155436 to your computer and use it in GitHub Desktop.
GCS Backup File Reader Example
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() + "]");
}
}
}
@nlaney
Copy link

nlaney commented Oct 21, 2016

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment