Search in sources :

Example 21 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 22 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 23 with Storage

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);
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) LifecycleRule(com.google.cloud.storage.BucketInfo.LifecycleRule)

Example 24 with Storage

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;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) Date(java.util.Date)

Example 25 with Storage

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;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) Date(java.util.Date)

Aggregations

Storage (com.google.cloud.storage.Storage)341 Blob (com.google.cloud.storage.Blob)123 Bucket (com.google.cloud.storage.Bucket)91 BlobId (com.google.cloud.storage.BlobId)74 Test (org.junit.Test)65 BlobInfo (com.google.cloud.storage.BlobInfo)57 IOException (java.io.IOException)26 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ArrayList (java.util.ArrayList)23 Before (org.junit.Before)22 PrintStream (java.io.PrintStream)18 Acl (com.google.cloud.storage.Acl)16 Path (java.nio.file.Path)16 StorageException (com.google.cloud.storage.StorageException)14 StorageOptions (com.google.cloud.storage.StorageOptions)13 TestRunner (org.apache.nifi.util.TestRunner)12 WriteChannel (com.google.cloud.WriteChannel)11 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)10 File (java.io.File)10