@Override public List<? extends InputReader<byte[]>> createReaders() { GcsService gcsService = GcsServiceFactory.createGcsService(MapReduceConstants.GCS_RETRY_PARAMETERS); GcsFileMetadata metadata; try { metadata = gcsService.getMetadata(file); if (metadata == null) { throw new RuntimeException("File does not exist: " + file.toString()); } } catch (IOException e) { throw new RuntimeException("Unable to read file metadata: " + file.toString(), e); } long blobSize = metadata.getLength(); return split(file, blobSize, shardCount); }
public BlobEntryGcsImpl(ByteBuffer byteBuffer, GcsFileMetadata gcsFileMetadata) { this.fileName = gcsFileMetadata.getFilename().getObjectName(); this.data = byteBuffer.array(); this.length = gcsFileMetadata.getLength(); this.mimeType = gcsFileMetadata.getOptions().getMimeType(); this.cacheControl = gcsFileMetadata.getOptions().getCacheControl(); this.eTag = gcsFileMetadata.getEtag(); this.lastModified = gcsFileMetadata.getLastModified(); this.metaData = gcsFileMetadata.getOptions().getUserMetadata(); }
@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); return new BlobEntryGcsImpl(byteBuffer, gcsFileMetadata); } catch (IOException ex) { logger.log(Level.INFO, "Failed to fetch blob with name '" + fileName + "'", ex); throw new UnexpectedServerException(); } }