Search in sources :

Example 1 with BlobEntryGcsImpl

use of com.pratilipi.data.type.gcs.BlobEntryGcsImpl 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)

Example 2 with BlobEntryGcsImpl

use of com.pratilipi.data.type.gcs.BlobEntryGcsImpl in project pratilipi by Pratilipi.

the class BlobAccessorGcsImpl2 method createOrUpdateBlob.

@Override
public BlobEntry createOrUpdateBlob(BlobEntry blobEntry) {
    BlobId blobId = BlobId.of(bucketName, blobEntry.getName());
    Builder blobInfoBuilder = BlobInfo.newBuilder(blobId);
    if (blobEntry.getMimeType() != null)
        blobInfoBuilder.setContentType(blobEntry.getMimeType());
    if (blobEntry.getCacheControl() != null)
        blobInfoBuilder.setCacheControl(blobEntry.getCacheControl());
    if (blobEntry.getMetaName() != null) {
        Map<String, String> metadata = new HashMap<>();
        metadata.put(BlobEntry.META_NAME, blobEntry.getMetaName());
        blobInfoBuilder.setMetadata(metadata);
    }
    return new BlobEntryGcsImpl(gcsService.create(blobInfoBuilder.build(), blobEntry.getData()));
}
Also used : HashMap(java.util.HashMap) Builder(com.google.cloud.storage.BlobInfo.Builder) BlobEntryGcsImpl(com.pratilipi.data.type.gcs.BlobEntryGcsImpl) BlobId(com.google.cloud.storage.BlobId)

Aggregations

BlobEntryGcsImpl (com.pratilipi.data.type.gcs.BlobEntryGcsImpl)2 GcsFileMetadata (com.google.appengine.tools.cloudstorage.GcsFileMetadata)1 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)1 GcsInputChannel (com.google.appengine.tools.cloudstorage.GcsInputChannel)1 BlobId (com.google.cloud.storage.BlobId)1 Builder (com.google.cloud.storage.BlobInfo.Builder)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1