public long size(final String bucket, final String path) throws IOException { return storage.objects().get(bucket, path).execute().getSize().longValue(); }
static void show(StorageObject object) { System.out.println("name: " + object.getName()); System.out.println("size: " + object.getSize()); System.out.println("contentType: " + object.getContentType()); System.out.println("updated: " + object.getUpdated()); System.out.println("owner: " + object.getOwner()); // should only show up if projection is full. // System.out.println("acl: " + object.getAcl()); }
System.out.println(object.getName() + " (" + object.getSize() + " bytes)");
public static void main(String[] args) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); Credential credential = CredentialsProvider.authorize(httpTransport, jsonFactory); Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName("Google-ObjectsUploadExample/1.0").build(); StorageObject object = uploadSimple(storage, BUCKET_NAME, OBJECT_NAME, new File(FILE_NAME)); System.out.println(object.getName() + " (size: " + object.getSize() + ")"); }
public static void main(String[] args) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); Credential credential = CredentialsProvider.authorize(httpTransport, jsonFactory); Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName("Google-ObjectsGetMetadataExample/1.0").build(); StorageObject object = get(storage, BUCKET_NAME, OBJECT_NAME); System.out.println(object.getName() + " (size: " + object.getSize() + ")"); }
public static void main(String[] args) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); Credential credential = CredentialsProvider.authorize(httpTransport, jsonFactory); Storage storage = new Storage.Builder(httpTransport, jsonFactory, credential) .setApplicationName("Google-ObjectsListExample/1.0").build(); for (StorageObject object : list(storage, BUCKET_NAME)) { System.out.println(object.getName() + " (size: " + object.getSize() + ")"); } } }
/** * Returns the file size from GCS or throws {@link FileNotFoundException} if the resource does not * exist. */ public long fileSize(GcsPath path) throws IOException { return getObject(path).getSize().longValue(); }
Storage.Objects.Get getObject = client.objects().get(bucketName, objectname); StorageObject object = getObject.execute(); System.out.println("Size is: " + object.getSize().longValue());
/** * @param name of the file we're interested in * @return size of the file, in bytes * @throws IOException */ public long getFileSize(String bucket, String name) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucket, name); StorageObject object = getObject.execute(); BigInteger size = object.getSize(); if (size.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { throw new RuntimeException("File size is too big for a long!"); } return size.longValue(); }
/** * @param name of the file we're interested in * @return size of the file, in bytes * @throws IOException */ public long getFileSize(String bucket, String name) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucket, name); StorageObject object = getObject.execute(); BigInteger size = object.getSize(); if (size.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { throw new RuntimeException("File size is too big for a long!"); } return size.longValue(); }
private Long toFileSize(StorageObjectOrIOException storageObjectOrIOException) throws IOException { if (storageObjectOrIOException.ioException() != null) { throw storageObjectOrIOException.ioException(); } else { return storageObjectOrIOException.storageObject().getSize().longValue(); } }
private Metadata toMetadata(StorageObject storageObject) { // TODO: Address https://issues.apache.org/jira/browse/BEAM-1494 // It is incorrect to set IsReadSeekEfficient true for files with content encoding set to gzip. Metadata.Builder ret = Metadata.builder() .setIsReadSeekEfficient(true) .setResourceId(GcsResourceId.fromGcsPath(GcsPath.fromObject(storageObject))); BigInteger size = firstNonNull(storageObject.getSize(), BigInteger.ZERO); ret.setSizeBytes(size.longValue()); return ret.build(); }
private static void checkGcsUrlExists(String url) throws IOException { // Ensure data is accessible. // If we can read the size, then surely we can read the file. GcsPath fn = GcsPath.fromUri(url); Storage.Objects storageClient = GCSOptions.Methods.createStorageClient(pipelineOptions, auth); Storage.Objects.Get getter = storageClient.get(fn.getBucket(), fn.getObject()); StorageObject object = getter.execute(); BigInteger size = object.getSize(); }
private static boolean GCSURLExists(String url) { // ensure data is accessible try { // if we can read the size, then surely we can read the file GcsPath fn = GcsPath.fromUri(url); Storage.Objects storageClient = GCSOptions.Methods.createStorageClient(pipelineOptions, auth); Storage.Objects.Get getter = storageClient.get(fn.getBucket(), fn.getObject()); StorageObject object = getter.execute(); BigInteger size = object.getSize(); return true; } catch (Exception x) { return false; } }
private static boolean GCSURLExists(String url) { // ensure data is accessible try { // if we can read the size, then surely we can read the file GcsPath fn = GcsPath.fromUri(url); Storage.Objects storageClient = GCSOptions.Methods.createStorageClient(pipelineOptions, auth); Storage.Objects.Get getter = storageClient.get(fn.getBucket(), fn.getObject()); StorageObject object = getter.execute(); BigInteger size = object.getSize(); return true; } catch (Exception x) { return false; } }
resourceId, object.getUpdated().getValue(), object.getSize().longValue(),
@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()); }