Search in sources :

Example 1 with GcsInputChannel

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();
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) JsonParser(com.google.gson.JsonParser)

Example 2 with GcsInputChannel

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();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) BlobEntryGcsImpl(com.pratilipi.data.type.gcs.BlobEntryGcsImpl) IOException(java.io.IOException) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) ByteBuffer(java.nio.ByteBuffer) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Aggregations

GcsFileMetadata (com.google.appengine.tools.cloudstorage.GcsFileMetadata)2 GcsInputChannel (com.google.appengine.tools.cloudstorage.GcsInputChannel)2 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 BlobEntryGcsImpl (com.pratilipi.data.type.gcs.BlobEntryGcsImpl)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1