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);
}
}
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();
}
}
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()));
}
}
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();
}
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;
}
Aggregations