Search in sources :

Example 51 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class GetBucketMetadata method getBucketMetadata.

public static void getBucketMetadata(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    // Select all fields. Fields can be selected individually e.g. Storage.BucketField.NAME
    Bucket bucket = storage.get(bucketName, Storage.BucketGetOption.fields(Storage.BucketField.values()));
    // Print bucket metadata
    System.out.println("BucketName: " + bucket.getName());
    System.out.println("DefaultEventBasedHold: " + bucket.getDefaultEventBasedHold());
    System.out.println("DefaultKmsKeyName: " + bucket.getDefaultKmsKeyName());
    System.out.println("Id: " + bucket.getGeneratedId());
    System.out.println("IndexPage: " + bucket.getIndexPage());
    System.out.println("Location: " + bucket.getLocation());
    System.out.println("LocationType: " + bucket.getLocationType());
    System.out.println("Metageneration: " + bucket.getMetageneration());
    System.out.println("NotFoundPage: " + bucket.getNotFoundPage());
    System.out.println("RetentionEffectiveTime: " + bucket.getRetentionEffectiveTime());
    System.out.println("RetentionPeriod: " + bucket.getRetentionPeriod());
    System.out.println("RetentionPolicyIsLocked: " + bucket.retentionPolicyIsLocked());
    System.out.println("RequesterPays: " + bucket.requesterPays());
    System.out.println("SelfLink: " + bucket.getSelfLink());
    System.out.println("StorageClass: " + bucket.getStorageClass().name());
    System.out.println("TimeCreated: " + bucket.getCreateTime());
    System.out.println("VersioningEnabled: " + bucket.versioningEnabled());
    if (bucket.getLabels() != null) {
        System.out.println("\n\n\nLabels:");
        for (Map.Entry<String, String> label : bucket.getLabels().entrySet()) {
            System.out.println(label.getKey() + "=" + label.getValue());
        }
    }
    if (bucket.getLifecycleRules() != null) {
        System.out.println("\n\n\nLifecycle Rules:");
        for (BucketInfo.LifecycleRule rule : bucket.getLifecycleRules()) {
            System.out.println(rule);
        }
    }
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) BucketInfo(com.google.cloud.storage.BucketInfo) Map(java.util.Map)

Example 52 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class GetPublicAccessPrevention method getPublicAccessPrevention.

public static void getPublicAccessPrevention(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    // Gets Bucket Metadata and prints publicAccessPrevention value (either 'inherited' or
    // 'enforced').
    BucketInfo.PublicAccessPrevention publicAccessPrevention = bucket.getIamConfiguration().getPublicAccessPrevention();
    System.out.println("Public access prevention is set to " + publicAccessPrevention.getValue() + " for " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) BucketInfo(com.google.cloud.storage.BucketInfo)

Example 53 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class ListBuckets method listBuckets.

public static void listBuckets(String projectId) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Page<Bucket> buckets = storage.list();
    for (Bucket bucket : buckets.iterateAll()) {
        System.out.println(bucket.getName());
    }
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 54 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class RemoveBucketIamConditionalBinding method removeBucketIamConditionalBinding.

/**
 * Example of removing a conditional binding to the Bucket-level IAM
 */
public static void removeBucketIamConditionalBinding(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // For more information please read:
    // https://cloud.google.com/storage/docs/access-control/iam
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Policy originalPolicy = storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));
    String role = "roles/storage.objectViewer";
    // getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
    List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
    // Create a condition to compare against
    Condition.Builder conditionBuilder = Condition.newBuilder();
    conditionBuilder.setTitle("Title");
    conditionBuilder.setDescription("Description");
    conditionBuilder.setExpression("resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")");
    Iterator iterator = bindings.iterator();
    while (iterator.hasNext()) {
        Binding binding = (Binding) iterator.next();
        boolean foundRole = binding.getRole().equals(role);
        boolean conditionsEqual = conditionBuilder.build().equals(binding.getCondition());
        // Remove condition when the role and condition are equal
        if (foundRole && conditionsEqual) {
            iterator.remove();
            break;
        }
    }
    // Update policy to remove conditional binding
    Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
    updatedPolicyBuilder.setBindings(bindings).setVersion(3);
    Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
    System.out.println("Conditional Binding was removed.");
}
Also used : Policy(com.google.cloud.Policy) Binding(com.google.cloud.Binding) Condition(com.google.cloud.Condition) Storage(com.google.cloud.storage.Storage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator)

Example 55 with Storage

use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.

the class SetDefaultRpo method setDefaultRpo.

public static void setDefaultRpo(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    bucket.toBuilder().setRpo(Rpo.DEFAULT).build().update();
    System.out.println("Replication was set to default for " + bucketName);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Aggregations

Storage (com.google.cloud.storage.Storage)140 Bucket (com.google.cloud.storage.Bucket)45 Blob (com.google.cloud.storage.Blob)44 Test (org.junit.Test)30 BlobId (com.google.cloud.storage.BlobId)24 BlobInfo (com.google.cloud.storage.BlobInfo)14 TestRunner (org.apache.nifi.util.TestRunner)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 Policy (com.google.cloud.Policy)9 ArrayList (java.util.ArrayList)8 Acl (com.google.cloud.storage.Acl)7 Date (java.util.Date)7 HashMap (java.util.HashMap)7 BucketInfo (com.google.cloud.storage.BucketInfo)6 HmacKeyMetadata (com.google.cloud.storage.HmacKey.HmacKeyMetadata)6 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)5 Binding (com.google.cloud.Binding)5 WriteChannel (com.google.cloud.WriteChannel)4 IOException (java.io.IOException)4