use of com.google.appengine.tools.cloudstorage.GcsInputChannel in project iosched by google.
the class CloudFileManager method readFileAsJsonObject.
public JsonObject readFileAsJsonObject(GcsFilename file) throws IOException {
GcsFileMetadata metadata = gcsService.getMetadata(file);
if (metadata == null) {
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Development) {
// In the development server, try to fetch files on cloud storage via HTTP
Logger.getAnonymousLogger().info("fetching " + file.getObjectName() + " at " + Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
return RemoteJsonHelper.fetchJsonFromPublicURL(Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
}
return null;
}
GcsInputChannel readChannel = null;
try {
readChannel = gcsService.openReadChannel(file, 0);
JsonElement element = new JsonParser().parse(Channels.newReader(readChannel, DEFAULT_CHARSET_NAME));
return element.getAsJsonObject();
} finally {
if (readChannel != null) {
readChannel.close();
}
}
}
use of com.google.appengine.tools.cloudstorage.GcsInputChannel in project pratilipi by Pratilipi.
the class BlobAccessorGcsImpl method getBlob.
@Override
public BlobEntry getBlob(String fileName) throws UnexpectedServerException {
GcsFilename gcsFileName = new GcsFilename(bucketName, fileName);
try {
GcsFileMetadata gcsFileMetadata = gcsService.getMetadata(gcsFileName);
if (gcsFileMetadata == null)
return null;
if (gcsFileMetadata.getLength() == 0)
return null;
GcsInputChannel gcsInputChannel = gcsService.openReadChannel(gcsFileName, 0);
ByteBuffer byteBuffer = ByteBuffer.allocate((int) gcsFileMetadata.getLength());
gcsInputChannel.read(byteBuffer);
if (byteBuffer.position() != gcsFileMetadata.getLength()) {
logger.log(Level.SEVERE, "Byte buffer size of " + byteBuffer.position() + " is not same as content lenght of " + gcsFileMetadata.getLength());
throw new UnexpectedServerException();
}
return new BlobEntryGcsImpl(byteBuffer, gcsFileMetadata);
} catch (IOException ex) {
logger.log(Level.INFO, "Failed to fetch blob with name '" + fileName + "'", ex);
throw new UnexpectedServerException();
}
}
Aggregations