public static boolean deleteImage(String objectName) { try { GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); GcsFilename fileName = new GcsFilename(getBucket(), objectName); return gcsService.delete(fileName); } catch (IOException ex) { log.log(Level.WARNING, "Exception while deleting file", ex); return false; } }
@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); }
@Override public void deleteBlob(BlobEntry blobEntry) throws UnexpectedServerException { try { gcsService.delete(new GcsFilename(bucketName, blobEntry.getName())); } catch (IOException ex) { logger.log(Level.INFO, "Failed to delete blob with name '" + blobEntry.getName() + "'", ex); throw new UnexpectedServerException(); } }
@Override public List<String> getNameList(String prefix) throws IOException { ListOptions.Builder options = new ListOptions.Builder(); options.setPrefix(prefix); ListResult result = gcsService.list(bucketName, options.build()); List<String> fileNameList = new LinkedList<>(); while (result.hasNext()) { String fileName = result.next().getName(); if (!fileName.equals(prefix)) fileNameList.add(fileName.substring(prefix.length())); } return fileNameList; }
@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(); } }
@Override public void createOrUpdateBlob(BlobEntry blobEntry) throws UnexpectedServerException { GcsFilename gcsFileName = new GcsFilename(bucketName, blobEntry.getName()); Builder builder = new GcsFileOptions.Builder(); if (blobEntry.getMimeType() != null) builder.mimeType(blobEntry.getMimeType()); if (blobEntry.getCacheControl() != null) builder.cacheControl(blobEntry.getCacheControl()); GcsFileOptions gcsFileOptions = builder.build(); try { GcsOutputChannel gcsOutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions); gcsOutputChannel.write(ByteBuffer.wrap(blobEntry.getData())); gcsOutputChannel.close(); } catch (IOException ex) { logger.log( Level.INFO, "Failed to create/update blob with name '" + blobEntry.getName() + "'", ex); throw new UnexpectedServerException(); } }