/** Returns the requested storage object or {@code null} if not found. */ @Override public StorageObject get(StorageObject object, Map<Option, ?> options) throws StorageException { // we allow the "ID" option because we need to, but then we give a whole answer anyways // because the caller won't mind the extra fields. if (throwIfOption && !options.isEmpty() && options.size() > 1 && options.keySet().toArray()[0] != Storage.BlobGetOption.fields(Storage.BlobField.ID)) { throw new UnsupportedOperationException(); } String key = fullname(object); if (metadata.containsKey(key)) { StorageObject ret = metadata.get(key); if (contents.containsKey(key)) { ret.setSize(BigInteger.valueOf(contents.get(key).length)); } ret.setId(key); return ret; } return null; }
@Override public StorageObject apply(String prefix) { return new StorageObject() .set("isDirectory", true) .setBucket(bucket) .setName(prefix) .setSize(BigInteger.ZERO); } };
storageObject.setSize(BigInteger.valueOf(size));
.setName("n/") .setBucket("b") .setSize(BigInteger.ZERO) .set("isDirectory", true); blobInfo = BlobInfo.fromPb(object);
/** * Returns the requested storage object or {@code null} if not found. */ @Override public StorageObject get(StorageObject object, Map<Option, ?> options) throws StorageException { // we allow the "ID" option because we need to, but then we give a whole answer anyways // because the caller won't mind the extra fields. if (throwIfOption && !options.isEmpty() && options.size() > 1 && options.keySet().toArray()[0] != Storage.BlobGetOption.fields(Storage.BlobField.ID)) { throw new UnsupportedOperationException(); } String key = fullname(object); if (metadata.containsKey(key)) { StorageObject ret = metadata.get(key); if (contents.containsKey(key)) { ret.setSize(BigInteger.valueOf(contents.get(key).length)); } ret.setId(key); return ret; } return null; }
@Override public StorageObject apply(String prefix) { return new StorageObject() .set("isDirectory", true) .setBucket(bucket) .setName(prefix) .setSize(BigInteger.ZERO); } };
@Override public StorageObject apply(String prefix) { return new StorageObject() .set("isDirectory", true) .setBucket(bucket) .setName(prefix) .setSize(BigInteger.ZERO); } };
@Test public void testFileSizeNonBatch() throws Exception { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class); Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class); when(mockStorage.objects()).thenReturn(mockStorageObjects); when(mockStorageObjects.get("testbucket", "testobject")).thenReturn(mockStorageGet); when(mockStorageGet.execute()) .thenReturn(new StorageObject().setSize(BigInteger.valueOf(1000))); assertEquals(1000, gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject"))); }
private StorageObject createStorageObject(String gcsFilename, long fileSize) { GcsPath gcsPath = GcsPath.fromUri(gcsFilename); // Google APIs will use null for empty files. @Nullable BigInteger size = (fileSize == 0) ? null : BigInteger.valueOf(fileSize); return new StorageObject() .setBucket(gcsPath.getBucket()) .setName(gcsPath.getObject()) .setSize(size); }
storageObject.setSize(BigInteger.valueOf(size));
@Test public void testRetryFileSizeNonBatch() throws IOException { GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); Storage mockStorage = Mockito.mock(Storage.class); gcsUtil.setStorageClient(mockStorage); Storage.Objects mockStorageObjects = Mockito.mock(Storage.Objects.class); Storage.Objects.Get mockStorageGet = Mockito.mock(Storage.Objects.Get.class); BackOff mockBackOff = BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.withMaxRetries(2).backoff()); when(mockStorage.objects()).thenReturn(mockStorageObjects); when(mockStorageObjects.get("testbucket", "testobject")).thenReturn(mockStorageGet); when(mockStorageGet.execute()) .thenThrow(new SocketTimeoutException("SocketException")) .thenThrow(new SocketTimeoutException("SocketException")) .thenReturn(new StorageObject().setSize(BigInteger.valueOf(1000))); assertEquals( 1000, gcsUtil .getObject( GcsPath.fromComponents("testbucket", "testobject"), mockBackOff, new FastNanoClockAndSleeper()) .getSize() .longValue()); assertEquals(BackOff.STOP, mockBackOff.nextBackOffMillis()); }
private StorageObject createStorageObject(String gcsFilename, long fileSize) { GcsPath gcsPath = GcsPath.fromUri(gcsFilename); return new StorageObject() .setBucket(gcsPath.getBucket()) .setName(gcsPath.getObject()) .setSize(BigInteger.valueOf(fileSize)); } }
storageObject.setSize(BigInteger.valueOf(size));