Search in sources :

Example 96 with Storage

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

the class StorageSnippets method removeRetentionPolicy.

/**
 * Example of removing a retention policy on a bucket
 */
public Bucket removeRetentionPolicy(String bucketName) throws StorageException, IllegalArgumentException {
    // [START storage_remove_retention_policy]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The name of a bucket, e.g. "my-bucket"
    // String bucketName = "my-bucket";
    Bucket bucket = storage.get(bucketName, BucketGetOption.fields(BucketField.RETENTION_POLICY));
    if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
        throw new IllegalArgumentException("Unable to remove retention period as retention policy is locked.");
    }
    Bucket bucketWithoutRetentionPolicy = bucket.toBuilder().setRetentionPeriod(null).build().update();
    System.out.println("Retention period for " + bucketName + " has been removed");
    // [END storage_remove_retention_policy]
    return bucketWithoutRetentionPolicy;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 97 with Storage

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

the class AddBucketIamConditionalBinding method addBucketIamConditionalBinding.

/**
 * Example of adding a conditional binding to the Bucket-level IAM
 */
public static void addBucketIamConditionalBinding(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";
    String member = "group:example@google.com";
    // getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
    List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
    // Create a condition
    String conditionTitle = "Title";
    String conditionDescription = "Description";
    String conditionExpression = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")";
    Condition.Builder conditionBuilder = Condition.newBuilder();
    conditionBuilder.setTitle(conditionTitle);
    conditionBuilder.setDescription(conditionDescription);
    conditionBuilder.setExpression(conditionExpression);
    // Add condition to a binding
    Binding.Builder newBindingBuilder = Binding.newBuilder().setRole(role).setMembers(Arrays.asList(member)).setCondition(conditionBuilder.build());
    bindings.add(newBindingBuilder.build());
    // Update policy with new conditional binding
    Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
    updatedPolicyBuilder.setBindings(bindings).setVersion(3);
    Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
    System.out.printf("Added %s with role %s to %s with condition %s %s %s\n", member, role, bucketName, conditionTitle, conditionDescription, conditionExpression);
}
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)

Example 98 with Storage

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

the class AddBucketLabel method addBucketLabel.

public static void addBucketLabel(String projectId, String bucketName, String labelKey, String labelValue) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";
    // The key of the label to add
    // String labelKey = "label-key-to-add";
    // The value of the label to add
    // String labelValue = "label-value-to-add";
    Map<String, String> labelsToAdd = new HashMap<>();
    labelsToAdd.put(labelKey, labelValue);
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    Map<String, String> labels = bucket.getLabels();
    if (labels == null) {
        labels = labelsToAdd;
    } else {
        labels.putAll(labelsToAdd);
    }
    bucket.toBuilder().setLabels(labels).build().update();
    System.out.println("Added label " + labelKey + " with value " + labelValue + " to bucket " + bucketName + ".");
}
Also used : Storage(com.google.cloud.storage.Storage) HashMap(java.util.HashMap) Bucket(com.google.cloud.storage.Bucket)

Example 99 with Storage

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

the class DeleteBucket method deleteBucket.

public static void deleteBucket(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";
    // The ID of the bucket to delete
    // String bucketName = "your-unique-bucket-name";
    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    bucket.delete();
    System.out.println("Bucket " + bucket.getName() + " was deleted");
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 100 with Storage

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

the class DisableLifecycleManagement method disableLifecycleManagement.

public static void disableLifecycleManagement(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().deleteLifecycleRules().build().update();
    System.out.println("Lifecycle management was disabled for bucket " + 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