use of com.google.api.services.storage.Storage.Objects.List 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();
}
}
Aggregations