Search in sources :

Example 1 with Objects

use of com.google.api.services.storage.model.Objects in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method list.

@Override
public Tuple<String, Iterable<StorageObject>> list(final String bucket, Map<Option, ?> options) {
    try {
        Objects objects = storage.objects().list(bucket).setProjection(DEFAULT_PROJECTION).setVersions(Option.VERSIONS.getBoolean(options)).setDelimiter(Option.DELIMITER.getString(options)).setPrefix(Option.PREFIX.getString(options)).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).setFields(Option.FIELDS.getString(options)).execute();
        Iterable<StorageObject> storageObjects = Iterables.concat(firstNonNull(objects.getItems(), ImmutableList.<StorageObject>of()), objects.getPrefixes() != null ? Lists.transform(objects.getPrefixes(), objectFromPrefix(bucket)) : ImmutableList.<StorageObject>of());
        return Tuple.of(objects.getNextPageToken(), storageObjects);
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) Objects(com.google.api.services.storage.model.Objects) IOException(java.io.IOException)

Example 2 with Objects

use of com.google.api.services.storage.model.Objects in project cloudbreak by hortonworks.

the class FilesystemUtil method deleteGcsObjects.

private static void deleteGcsObjects(ApplicationContext applicationContext, Map<String, String> cloudProviderParams, String bucketName, String folderPrefix) throws IOException, GeneralSecurityException {
    String serviceAccountPrivateKey = ResourceUtil.readBase64EncodedContentFromResource(applicationContext, cloudProviderParams.get("p12File"));
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new ByteArrayInputStream(Base64.decodeBase64(serviceAccountPrivateKey)), "notasecret", "privatekey", "notasecret");
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential googleCredential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(cloudProviderParams.get("serviceAccountId")).setServiceAccountScopes(Collections.singletonList(STORAGE_SCOPE)).setServiceAccountPrivateKey(privateKey).build();
    Storage storage = new Builder(httpTransport, jsonFactory, googleCredential).setApplicationName("Google-BucketsInsertExample/1.0").build();
    List listObjects = storage.objects().list(bucketName).setPrefix(folderPrefix);
    Objects objects = listObjects.execute();
    Assert.assertNotNull(objects.getItems(), "Not found any objects with " + folderPrefix + " prefix.");
    Iterable<StorageObject> storageObjects = new ArrayList<>(objects.getItems());
    for (StorageObject storageObject : storageObjects) {
        storage.objects().delete("hm-bucket", storageObject.getName()).execute();
    }
}
Also used : PrivateKey(java.security.PrivateKey) StorageObject(com.google.api.services.storage.model.StorageObject) Builder(com.google.api.services.storage.Storage.Builder) ArrayList(java.util.ArrayList) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Storage(com.google.api.services.storage.Storage) ByteArrayInputStream(java.io.ByteArrayInputStream) Objects(com.google.api.services.storage.model.Objects) ArrayList(java.util.ArrayList) List(com.google.api.services.storage.Storage.Objects.List)

Example 3 with Objects

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

the class GcsFileSystemTest method testGlobExpansion.

@Test
public void testGlobExpansion() throws IOException {
    Objects modelObjects = new Objects();
    List<StorageObject> items = new ArrayList<>();
    // A directory
    items.add(new StorageObject().setBucket("testbucket").setName("testdirectory/"));
    // Files within the directory
    items.add(createStorageObject("gs://testbucket/testdirectory/file1name", 1L));
    items.add(createStorageObject("gs://testbucket/testdirectory/file2name", 2L));
    items.add(createStorageObject("gs://testbucket/testdirectory/file3name", 3L));
    items.add(createStorageObject("gs://testbucket/testdirectory/otherfile", 4L));
    items.add(createStorageObject("gs://testbucket/testdirectory/anotherfile", 5L));
    items.add(createStorageObject("gs://testbucket/testotherdirectory/file4name", 6L));
    modelObjects.setItems(items);
    when(mockGcsUtil.listObjects(eq("testbucket"), anyString(), isNull(String.class))).thenReturn(modelObjects);
    // Test patterns.
    {
        GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/file*");
        List<String> expectedFiles = ImmutableList.of("gs://testbucket/testdirectory/file1name", "gs://testbucket/testdirectory/file2name", "gs://testbucket/testdirectory/file3name");
        assertThat(expectedFiles, contains(toFilenames(gcsFileSystem.expand(pattern)).toArray()));
    }
    {
        GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/file[1-3]*");
        List<String> expectedFiles = ImmutableList.of("gs://testbucket/testdirectory/file1name", "gs://testbucket/testdirectory/file2name", "gs://testbucket/testdirectory/file3name");
        assertThat(expectedFiles, contains(toFilenames(gcsFileSystem.expand(pattern)).toArray()));
    }
    {
        GcsPath pattern = GcsPath.fromUri("gs://testbucket/testdirectory/file?name");
        List<String> expectedFiles = ImmutableList.of("gs://testbucket/testdirectory/file1name", "gs://testbucket/testdirectory/file2name", "gs://testbucket/testdirectory/file3name");
        assertThat(expectedFiles, contains(toFilenames(gcsFileSystem.expand(pattern)).toArray()));
    }
    {
        GcsPath pattern = GcsPath.fromUri("gs://testbucket/test*ectory/fi*name");
        List<String> expectedFiles = ImmutableList.of("gs://testbucket/testdirectory/file1name", "gs://testbucket/testdirectory/file2name", "gs://testbucket/testdirectory/file3name", "gs://testbucket/testotherdirectory/file4name");
        assertThat(expectedFiles, contains(toFilenames(gcsFileSystem.expand(pattern)).toArray()));
    }
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) Objects(com.google.api.services.storage.model.Objects) ArrayList(java.util.ArrayList) GcsPath(org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 4 with Objects

use of com.google.api.services.storage.model.Objects in project druid by druid-io.

the class GoogleTestUtils method expectListObjects.

public static void expectListObjects(Storage.Objects.List listRequest, URI prefix, long maxListingLength, List<StorageObject> objects) throws IOException {
    EasyMock.expect(listRequest.setPrefix(StringUtils.maybeRemoveLeadingSlash(prefix.getPath()))).andReturn(listRequest);
    EasyMock.expect(listRequest.setMaxResults(maxListingLength)).andReturn(listRequest);
    EasyMock.expect(listRequest.setPageToken(EasyMock.anyString())).andReturn(listRequest).anyTimes();
    Objects resultObjects = new Objects();
    resultObjects.setItems(objects);
    EasyMock.expect(listRequest.execute()).andReturn(resultObjects).once();
}
Also used : Objects(com.google.api.services.storage.model.Objects)

Example 5 with Objects

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

the class GcsClient method list.

@Nullable
public List<String> list(URI uri) throws ResourceException {
    List<StorageObject> results = new ArrayList<StorageObject>();
    String path = cleanResourcePath(uri);
    try {
        Storage.Objects.List listRequest = storage.objects().list(uri.getHost()).setPrefix(path);
        Objects objects;
        // Iterate through each page of results, and add them to our results list.
        do {
            objects = listRequest.execute();
            // GCS API will return null on an empty list.
            if (objects.getItems() != null) {
                results.addAll(objects.getItems());
            }
            // Get the next page, in the next iteration of this loop.
            listRequest.setPageToken(objects.getNextPageToken());
        } while (null != objects.getNextPageToken());
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(uri, e);
    }
    List<String> resultStrings = new ArrayList<String>();
    for (StorageObject result : results) {
        resultStrings.add(result.getName());
    }
    return resultStrings;
}
Also used : StorageObject(com.google.api.services.storage.model.StorageObject) ArrayList(java.util.ArrayList) Objects(com.google.api.services.storage.model.Objects) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Aggregations

Objects (com.google.api.services.storage.model.Objects)17 StorageObject (com.google.api.services.storage.model.StorageObject)14 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)6 Storage (com.google.api.services.storage.Storage)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 List (java.util.List)5 GcsPath (org.apache.beam.sdk.extensions.gcp.util.gcsfs.GcsPath)5 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)4 LinkedList (java.util.LinkedList)3 Pattern (java.util.regex.Pattern)3 GcsPath (org.apache.beam.sdk.util.gcsfs.GcsPath)3 ImmutableList (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)2 ImmutableList (com.google.common.collect.ImmutableList)2 URI (java.net.URI)2 AccessDeniedException (java.nio.file.AccessDeniedException)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2