use of com.google.api.services.storage.Storage.Builder in project cloudbreak by hortonworks.
the class GcpStackUtil method buildStorage.
public static Storage buildStorage(CloudCredential gcpCredential, String name) {
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = buildCredential(gcpCredential, httpTransport);
return new Builder(httpTransport, JSON_FACTORY, null).setApplicationName(name).setHttpRequestInitializer(credential).build();
} catch (Exception e) {
LOGGER.error("Error occurred while building Google Storage access.", e);
}
return null;
}
use of com.google.api.services.storage.Storage.Builder 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