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;
}
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);
}
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 + ".");
}
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");
}
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);
}
Aggregations