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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations