use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class DisableBucketVersioning method disableBucketVersioning.
public static void disableBucketVersioning(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().setVersioningEnabled(false).build().update();
System.out.println("Versioning is now disabled for bucket " + bucketName);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class DisableRequesterPays method disableRequesterPays.
public static void disableRequesterPays(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().setRequesterPays(false).build().update();
System.out.println("Requester pays disabled for bucket " + bucketName);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class EnableLifecycleManagement method enableLifecycleManagement.
public static void enableLifecycleManagement(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);
// See the LifecycleRule documentation for additional info on what you can do with lifecycle
// management rules. This one deletes objects that are over 100 days old.
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/BucketInfo.LifecycleRule.html
bucket.toBuilder().setLifecycleRules(ImmutableList.of(new LifecycleRule(LifecycleRule.LifecycleAction.newDeleteAction(), LifecycleRule.LifecycleCondition.newBuilder().setAge(100).build()))).build().update();
System.out.println("Lifecycle management was enabled and configured for bucket " + bucketName);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method getRetentionPolicy.
/**
* Example of how to get a bucket's retention policy
*/
public Bucket getRetentionPolicy(String bucketName) throws StorageException {
// [START storage_get_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));
System.out.println("Retention Policy for " + bucketName);
System.out.println("Retention Period: " + bucket.getRetentionPeriod());
if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
System.out.println("Retention Policy is locked");
}
if (bucket.getRetentionEffectiveTime() != null) {
System.out.println("Effective Time: " + new Date(bucket.getRetentionEffectiveTime()));
}
// [END storage_get_retention_policy]
return bucket;
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class StorageSnippets method lockRetentionPolicy.
/**
* Example of how to lock a bucket retention policy
*/
public Bucket lockRetentionPolicy(String bucketName) throws StorageException {
// [START storage_lock_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, Storage.BucketGetOption.fields(BucketField.METAGENERATION));
Bucket lockedBucket = bucket.lockRetentionPolicy(Storage.BucketTargetOption.metagenerationMatch());
System.out.println("Retention period for " + bucketName + " is now locked");
System.out.println("Retention policy effective as of " + new Date(lockedBucket.getRetentionEffectiveTime()));
// [END storage_lock_retention_policy]
return lockedBucket;
}
Aggregations