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