Search in sources :

Example 51 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project beam by apache.

the class GcsUtilTest method testFileSizeNonBatch.

@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")));
}
Also used : Storage(com.google.api.services.storage.Storage) StorageObject(com.google.api.services.storage.model.StorageObject) Objects(com.google.api.services.storage.model.Objects) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) Test(org.junit.Test)

Example 52 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project beam by apache.

the class GcsUtilTest method testRetryFileSizeNonBatch.

@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());
}
Also used : Storage(com.google.api.services.storage.Storage) SocketTimeoutException(java.net.SocketTimeoutException) StorageObject(com.google.api.services.storage.model.StorageObject) Objects(com.google.api.services.storage.model.Objects) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) BackOff(com.google.api.client.util.BackOff) Test(org.junit.Test)

Example 53 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project ignite by apache.

the class TcpDiscoveryGoogleStorageIpFinder method getRegisteredAddresses.

/**
 * {@inheritDoc}
 */
@Override
public Collection<InetSocketAddress> getRegisteredAddresses() throws IgniteSpiException {
    init();
    Collection<InetSocketAddress> addrs = new ArrayList<>();
    try {
        Storage.Objects.List listObjects = storage.objects().list(bucketName);
        com.google.api.services.storage.model.Objects objects;
        do {
            objects = listObjects.execute();
            if (objects == null || objects.getItems() == null)
                break;
            for (StorageObject object : objects.getItems()) addrs.add(addrFromString(object.getName()));
            listObjects.setPageToken(objects.getNextPageToken());
        } while (null != objects.getNextPageToken());
    } catch (Exception e) {
        throw new IgniteSpiException("Failed to get content from the bucket: " + bucketName, e);
    }
    return addrs;
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GeneralSecurityException(java.security.GeneralSecurityException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IOException(java.io.IOException)

Example 54 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project gradle by gradle.

the class GcsResourceConnector method getMetaData.

@Nullable
@Override
public ExternalResourceMetaData getMetaData(URI location, boolean revalidate) throws ResourceException {
    LOGGER.debug("Attempting to get resource metadata: {}", location);
    StorageObject gcsObject = gcsClient.getResource(location);
    if (gcsObject == null) {
        return null;
    }
    return toExternalResourceMetaData(location, gcsObject);
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) Nullable(javax.annotation.Nullable)

Example 55 with StorageObject

use of com.google.api.services.storage.model.StorageObject in project java-docs-samples by GoogleCloudPlatform.

the class StorageSample method main.

// [END delete_object]
/**
 * Exercises the class's functions - gets and lists a bucket, uploads and deletes an object.
 *
 * @param args the command-line arguments. The first argument should be the bucket name.
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: StorageSample <bucket-name>");
        System.exit(1);
    }
    String bucketName = args[0];
    try {
        // Get metadata about the specified bucket.
        Bucket bucket = getBucket(bucketName);
        System.out.println("name: " + bucketName);
        System.out.println("location: " + bucket.getLocation());
        System.out.println("timeCreated: " + bucket.getTimeCreated());
        System.out.println("owner: " + bucket.getOwner());
        // List the contents of the bucket.
        List<StorageObject> bucketContents = listBucket(bucketName);
        if (null == bucketContents) {
            System.out.println("There were no objects in the given bucket; try adding some and re-running.");
        }
        for (StorageObject object : bucketContents) {
            System.out.println(object.getName() + " (" + object.getSize() + " bytes)");
        }
        // Create a temp file to upload
        Path tempPath = Files.createTempFile("StorageSample", "txt");
        Files.write(tempPath, "Sample file".getBytes());
        File tempFile = tempPath.toFile();
        tempFile.deleteOnExit();
        // Upload it
        uploadFile(TEST_FILENAME, "text/plain", tempFile, bucketName);
        // Now delete the file
        deleteObject(TEST_FILENAME, bucketName);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) StorageObject(com.google.api.services.storage.model.StorageObject) Bucket(com.google.api.services.storage.model.Bucket) IOException(java.io.IOException) File(java.io.File)

Aggregations

StorageObject (com.google.api.services.storage.model.StorageObject)81 Test (org.junit.Test)33 Objects (com.google.api.services.storage.model.Objects)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)16 Storage (com.google.api.services.storage.Storage)12 List (java.util.List)9 URI (java.net.URI)8 RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)7 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)7 GcsPath (org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)6 Nullable (javax.annotation.Nullable)6 File (java.io.File)5 Pattern (java.util.regex.Pattern)5 DateTime (com.google.api.client.util.DateTime)4 BlobSourceOption (com.google.cloud.storage.Storage.BlobSourceOption)4 BlobTargetOption (com.google.cloud.storage.Storage.BlobTargetOption)4 BlobWriteOption (com.google.cloud.storage.Storage.BlobWriteOption)4 BucketSourceOption (com.google.cloud.storage.Storage.BucketSourceOption)4