com.google.cloud.storage.Bucket.getRetentionPeriod()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(4.6k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中com.google.cloud.storage.Bucket.getRetentionPeriod()方法的一些代码示例,展示了Bucket.getRetentionPeriod()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bucket.getRetentionPeriod()方法的具体详情如下:
包路径:com.google.cloud.storage.Bucket
类名称:Bucket
方法名:getRetentionPeriod

Bucket.getRetentionPeriod介绍

暂无

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/** 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;
}

代码示例来源:origin: googleapis/google-cloud-java

/** 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;
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testRetentionPolicyNoLock() throws ExecutionException, InterruptedException {
 String bucketName = RemoteStorageHelper.generateBucketName();
 Bucket remoteBucket =
   storage.create(
     BucketInfo.newBuilder(bucketName).setRetentionPeriod(RETENTION_PERIOD).build());
 try {
  assertEquals(RETENTION_PERIOD, remoteBucket.getRetentionPeriod());
  assertNotNull(remoteBucket.getRetentionEffectiveTime());
  assertNull(remoteBucket.retentionPolicyIsLocked());
  remoteBucket =
    storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.RETENTION_POLICY));
  assertEquals(RETENTION_PERIOD, remoteBucket.getRetentionPeriod());
  assertNotNull(remoteBucket.getRetentionEffectiveTime());
  assertNull(remoteBucket.retentionPolicyIsLocked());
  String blobName = "test-create-with-retention-policy-hold";
  BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
  Blob remoteBlob = storage.create(blobInfo);
  assertNotNull(remoteBlob.getRetentionExpirationTime());
  remoteBucket = remoteBucket.toBuilder().setRetentionPeriod(null).build().update();
  assertNull(remoteBucket.getRetentionPeriod());
  remoteBucket = remoteBucket.toBuilder().setRetentionPeriod(null).build().update();
  assertNull(remoteBucket.getRetentionPeriod());
 } finally {
  RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void testAttemptObjectDeleteWithRetentionPolicy()
  throws ExecutionException, InterruptedException {
 String bucketName = RemoteStorageHelper.generateBucketName();
 Bucket remoteBucket =
   storage.create(
     BucketInfo.newBuilder(bucketName).setRetentionPeriod(RETENTION_PERIOD).build());
 assertEquals(RETENTION_PERIOD, remoteBucket.getRetentionPeriod());
 String blobName = "test-create-with-retention-policy";
 BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
 Blob remoteBlob = storage.create(blobInfo);
 assertNotNull(remoteBlob.getRetentionExpirationTime());
 try {
  remoteBlob.delete();
  fail("Expected failure on delete from retentionPolicy");
 } catch (StorageException ex) {
  // expected
 } finally {
  Thread.sleep(RETENTION_PERIOD_IN_MILLISECONDS);
  RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
 }
}

代码示例来源:origin: googleapis/google-cloud-java

assertEquals(DEFAULT_EVENT_BASED_HOLD, bucket.getDefaultEventBasedHold());
assertEquals(RETENTION_EFFECTIVE_TIME, bucket.getRetentionEffectiveTime());
assertEquals(RETENTION_PERIOD, bucket.getRetentionPeriod());
assertEquals(RETENTION_POLICY_IS_LOCKED, bucket.retentionPolicyIsLocked());
assertEquals(storage.getOptions(), bucket.getStorage().getOptions());

相关文章