Search in sources :

Example 1 with Builder

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;
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Builder(com.google.api.services.storage.Storage.Builder) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)

Example 2 with Builder

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

Aggregations

GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)2 HttpTransport (com.google.api.client.http.HttpTransport)2 Builder (com.google.api.services.storage.Storage.Builder)2 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 Storage (com.google.api.services.storage.Storage)1 List (com.google.api.services.storage.Storage.Objects.List)1 Objects (com.google.api.services.storage.model.Objects)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 PrivateKey (java.security.PrivateKey)1 ArrayList (java.util.ArrayList)1