Search in sources :

Example 31 with Storage

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

the class StorageSnippets method setRetentionPolicy.

/**
 * Example of setting a retention policy on a bucket
 */
public Bucket setRetentionPolicy(String bucketName, Long retentionPeriod) throws StorageException {
    // [START storage_set_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";
    // The retention period for objects in bucket
    // Long retentionPeriod = 3600L; // 1 hour in seconds
    Bucket bucketWithRetentionPolicy = storage.update(BucketInfo.newBuilder(bucketName).setRetentionPeriod(retentionPeriod).build());
    System.out.println("Retention period for " + bucketName + " is now " + bucketWithRetentionPolicy.getRetentionPeriod());
    // [END storage_set_retention_policy]
    return bucketWithRetentionPolicy;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 32 with Storage

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

the class StorageSnippets method getHmacKey.

/**
 * Example of retrieving the metadata of an existing HMAC key. *
 */
public HmacKeyMetadata getHmacKey(String accessId, String projectId) throws StorageException {
    // [START storage_get_hmac_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The access ID of the HMAC key, e.g. "GOOG0234230X00"
    // String accessId = "GOOG0234230X00";
    // 
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
    System.out.println("The HMAC key metadata is:");
    System.out.println("ID: " + metadata.getId());
    System.out.println("Access ID: " + metadata.getAccessId());
    System.out.println("Project ID: " + metadata.getProjectId());
    System.out.println("Service Account Email: " + metadata.getServiceAccount().getEmail());
    System.out.println("State: " + metadata.getState().toString());
    System.out.println("Time Created: " + new Date(metadata.getCreateTime()).toString());
    System.out.println("Time Updated: " + new Date(metadata.getUpdateTime()).toString());
    System.out.println("ETag: " + metadata.getEtag());
    // [END storage_get_hmac_key]
    return metadata;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage) Date(java.util.Date)

Example 33 with Storage

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

the class StorageSnippets method enableDefaultEventBasedHold.

/**
 * Example of how to enable default event-based hold for a bucket
 */
public Bucket enableDefaultEventBasedHold(String bucketName) throws StorageException {
    // [START storage_enable_default_event_based_hold]
    // 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.update(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(true).build());
    System.out.println("Default event-based hold was enabled for " + bucketName);
    // [END storage_enable_default_event_based_hold]
    return bucket;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket)

Example 34 with Storage

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

the class StorageSnippets method getUniformBucketLevelAccess.

/**
 * Example of how to get uniform bucket-level access metadata for a bucket
 */
public Bucket getUniformBucketLevelAccess(String bucketName) throws StorageException {
    // [START storage_get_uniform_bucket_level_access]
    // 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.IAMCONFIGURATION));
    BucketInfo.IamConfiguration iamConfiguration = bucket.getIamConfiguration();
    Boolean enabled = iamConfiguration.isUniformBucketLevelAccessEnabled();
    Date lockedTime = new Date(iamConfiguration.getUniformBucketLevelAccessLockedTime());
    if (enabled != null && enabled) {
        System.out.println("Uniform bucket-level access is enabled for " + bucketName);
        System.out.println("Bucket will be locked on " + lockedTime);
    } else {
        System.out.println("Uniform bucket-level access is disabled for " + bucketName);
    }
    // [END storage_get_uniform_bucket_level_access]
    return bucket;
}
Also used : Storage(com.google.cloud.storage.Storage) Bucket(com.google.cloud.storage.Bucket) BucketInfo(com.google.cloud.storage.BucketInfo) Date(java.util.Date)

Example 35 with Storage

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

the class StorageSnippets method activateHmacKey.

/**
 * Example of activating a previously deactivated HMAC key. *
 */
public HmacKeyMetadata activateHmacKey(String accessId, String projectId) throws StorageException {
    // [START storage_activate_hmac_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    // The access ID of the HMAC key, e.g. "GOOG0234230X00"
    // String accessId = "GOOG0234230X00";
    // 
    // The ID of the project to which the service account belongs.
    // String projectId = "project-id";
    HmacKeyMetadata metadata = storage.getHmacKey(accessId, Storage.GetHmacKeyOption.projectId(projectId));
    HmacKeyMetadata newMetadata = storage.updateHmacKeyState(metadata, HmacKeyState.ACTIVE);
    System.out.println("The HMAC key is now active.");
    System.out.println("The HMAC key metadata is:");
    System.out.println("ID: " + newMetadata.getId());
    System.out.println("Access ID: " + newMetadata.getAccessId());
    System.out.println("Project ID: " + newMetadata.getProjectId());
    System.out.println("Service Account Email: " + newMetadata.getServiceAccount().getEmail());
    System.out.println("State: " + newMetadata.getState().toString());
    System.out.println("Time Created: " + new Date(newMetadata.getCreateTime()).toString());
    System.out.println("Time Updated: " + new Date(newMetadata.getUpdateTime()).toString());
    System.out.println("ETag: " + newMetadata.getEtag());
    // [END storage_activate_hmac_key]
    return newMetadata;
}
Also used : HmacKeyMetadata(com.google.cloud.storage.HmacKey.HmacKeyMetadata) Storage(com.google.cloud.storage.Storage) Date(java.util.Date)

Aggregations

Storage (com.google.cloud.storage.Storage)339 Blob (com.google.cloud.storage.Blob)122 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)56 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)21 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