Search in sources :

Example 31 with StorageObject

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

the class GcsUtil method expand.

/**
   * Expands a pattern into matched paths. The pattern path may contain globs, which are expanded
   * in the result. For patterns that only match a single object, we ensure that the object
   * exists.
   */
public List<GcsPath> expand(GcsPath gcsPattern) throws IOException {
    Pattern p = null;
    String prefix = null;
    if (isWildcard(gcsPattern)) {
        // Part before the first wildcard character.
        prefix = getNonWildcardPrefix(gcsPattern.getObject());
        p = Pattern.compile(wildcardToRegexp(gcsPattern.getObject()));
    } else {
        // Not a wildcard.
        try {
            // Use a get request to fetch the metadata of the object, and ignore the return value.
            // The request has strong global consistency.
            getObject(gcsPattern);
            return ImmutableList.of(gcsPattern);
        } catch (FileNotFoundException e) {
            // If the path was not found, return an empty list.
            return ImmutableList.of();
        }
    }
    LOG.debug("matching files in bucket {}, prefix {} against pattern {}", gcsPattern.getBucket(), prefix, p.toString());
    String pageToken = null;
    List<GcsPath> results = new LinkedList<>();
    do {
        Objects objects = listObjects(gcsPattern.getBucket(), prefix, pageToken);
        if (objects.getItems() == null) {
            break;
        }
        // Filter objects based on the regex.
        for (StorageObject o : objects.getItems()) {
            String name = o.getName();
            // Skip directories, which end with a slash.
            if (p.matcher(name).matches() && !name.endsWith("/")) {
                LOG.debug("Matched object: {}", name);
                results.add(GcsPath.fromObject(o));
            }
        }
        pageToken = objects.getNextPageToken();
    } while (pageToken != null);
    return results;
}
Also used : Pattern(java.util.regex.Pattern) StorageObject(com.google.api.services.storage.model.StorageObject) FileNotFoundException(java.io.FileNotFoundException) GcsPath(org.apache.beam.sdk.util.gcsfs.GcsPath) Objects(com.google.api.services.storage.model.Objects) LinkedList(java.util.LinkedList)

Example 32 with StorageObject

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

the class GcsFileSystem method expand.

/**
   * Expands a pattern into {@link MatchResult}.
   *
   * @throws IllegalArgumentException if {@code gcsPattern} does not contain globs.
   */
@VisibleForTesting
MatchResult expand(GcsPath gcsPattern) throws IOException {
    String prefix = GcsUtil.getNonWildcardPrefix(gcsPattern.getObject());
    Pattern p = Pattern.compile(GcsUtil.wildcardToRegexp(gcsPattern.getObject()));
    LOG.debug("matching files in bucket {}, prefix {} against pattern {}", gcsPattern.getBucket(), prefix, p.toString());
    String pageToken = null;
    List<Metadata> results = new LinkedList<>();
    do {
        Objects objects = options.getGcsUtil().listObjects(gcsPattern.getBucket(), prefix, pageToken);
        if (objects.getItems() == null) {
            break;
        }
        // Filter objects based on the regex.
        for (StorageObject o : objects.getItems()) {
            String name = o.getName();
            // Skip directories, which end with a slash.
            if (p.matcher(name).matches() && !name.endsWith("/")) {
                LOG.debug("Matched object: {}", name);
                results.add(toMetadata(o));
            }
        }
        pageToken = objects.getNextPageToken();
    } while (pageToken != null);
    return MatchResult.create(Status.OK, results);
}
Also used : Pattern(java.util.regex.Pattern) StorageObject(com.google.api.services.storage.model.StorageObject) Metadata(org.apache.beam.sdk.io.fs.MatchResult.Metadata) Objects(com.google.api.services.storage.model.Objects) LinkedList(java.util.LinkedList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 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 34 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 35 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)

Aggregations

StorageObject (com.google.api.services.storage.model.StorageObject)35 Test (org.junit.Test)13 Objects (com.google.api.services.storage.model.Objects)10 RetryHelperException (com.google.cloud.RetryHelper.RetryHelperException)7 ArrayList (java.util.ArrayList)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)6 IOException (java.io.IOException)6 GcsPath (org.apache.beam.sdk.util.gcsfs.GcsPath)6 RpcBatch (com.google.cloud.storage.spi.v1.RpcBatch)5 Storage (com.google.api.services.storage.Storage)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 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 Map (java.util.Map)3 Callable (java.util.concurrent.Callable)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2