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

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

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

Bucket.toBuilder介绍

暂无

代码示例

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

/** Example of updating the bucket's information. */
// [TARGET update(BucketTargetOption...)]
public Bucket update() {
 // [START update]
 Bucket updatedBucket = bucket.toBuilder().setVersioningEnabled(true).build().update();
 // [END update]
 return updatedBucket;
}

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

/** Example of removing a retention policy on a bucket */
public Bucket removeRetentionPolicy(String bucketName)
  throws StorageException, IllegalArgumentException {
 // [START storage_remove_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));
 if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
  throw new IllegalArgumentException(
    "Unable to remove retention period as retention policy is locked.");
 }
 Bucket bucketWithoutRetentionPolicy =
   bucket.toBuilder().setRetentionPeriod(null).build().update();
 System.out.println("Retention period for " + bucketName + " has been removed");
 // [END storage_remove_retention_policy]
 return bucketWithoutRetentionPolicy;
}

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

@Test
public void testToBuilder() {
 expect(storage.getOptions()).andReturn(mockOptions).times(4);
 replay(storage);
 Bucket fullBucket = new Bucket(storage, new BucketInfo.BuilderImpl(FULL_BUCKET_INFO));
 assertEquals(fullBucket, fullBucket.toBuilder().build());
 Bucket simpleBlob = new Bucket(storage, new BucketInfo.BuilderImpl(BUCKET_INFO));
 assertEquals(simpleBlob, simpleBlob.toBuilder().build());
}

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

@Test
public void testUpdate() throws Exception {
 initializeExpectedBucket(5);
 Bucket expectedUpdatedBucket = expectedBucket.toBuilder().setNotFoundPage("p").build();
 expect(storage.getOptions()).andReturn(mockOptions).times(2);
 expect(storage.update(expectedUpdatedBucket)).andReturn(expectedUpdatedBucket);
 replay(storage);
 initializeBucket();
 Bucket updatedBucket = new Bucket(storage, new BucketInfo.BuilderImpl(expectedUpdatedBucket));
 Bucket actualUpdatedBucket = updatedBucket.update();
 assertEquals(expectedUpdatedBucket, actualUpdatedBucket);
}

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

@Test
public void testLockRetention() throws Exception {
 initializeExpectedBucket(5);
 Bucket expectedRetentionLockedBucket =
   expectedBucket
     .toBuilder()
     .setRetentionPeriod(RETENTION_PERIOD)
     .setRetentionPolicyIsLocked(true)
     .build();
 expect(storage.getOptions()).andReturn(mockOptions).times(2);
 expect(
     storage.lockRetentionPolicy(
       expectedRetentionLockedBucket,
       Storage.BucketTargetOption.metagenerationMatch(),
       Storage.BucketTargetOption.userProject(USER_PROJECT)))
   .andReturn(expectedRetentionLockedBucket);
 replay(storage);
 initializeBucket();
 Bucket lockedRetentionPolicyBucket =
   new Bucket(storage, new BucketInfo.BuilderImpl(expectedRetentionLockedBucket));
 Bucket actualRetentionLockedBucket =
   lockedRetentionPolicyBucket.lockRetentionPolicy(
     Storage.BucketTargetOption.metagenerationMatch(),
     Storage.BucketTargetOption.userProject(USER_PROJECT));
 assertEquals(expectedRetentionLockedBucket, actualRetentionLockedBucket);
}

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

@Test
public void testUpdateBucketLabel() {
 Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
 assertNull(remoteBucket.getLabels());
 remoteBucket = remoteBucket.toBuilder().setLabels(BUCKET_LABELS).build();
 Bucket updatedBucket = storage.update(remoteBucket);
 assertEquals(BUCKET_LABELS, updatedBucket.getLabels());
}

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

Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
assertNull(remoteBucket.requesterPays());
remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
Bucket updatedBucket = storage.update(remoteBucket);
assertTrue(updatedBucket.requesterPays());

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

remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
Bucket updatedBucket = storage.update(remoteBucket);
assertTrue(updatedBucket.requesterPays());

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

@Test
public void testClearBucketDefaultKmsKeyName() throws ExecutionException, InterruptedException {
 String bucketName = RemoteStorageHelper.generateBucketName();
 Bucket remoteBucket =
   storage.create(
     BucketInfo.newBuilder(bucketName)
       .setDefaultKmsKeyName(kmsKeyOneResourcePath)
       .setLocation(KMS_KEY_RING_LOCATION)
       .build());
 try {
  assertEquals(kmsKeyOneResourcePath, remoteBucket.getDefaultKmsKeyName());
  Bucket updatedBucket = remoteBucket.toBuilder().setDefaultKmsKeyName(null).build().update();
  assertNull(updatedBucket.getDefaultKmsKeyName());
 } finally {
  RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
 }
}

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

@Test
public void testUpdateBucketDefaultKmsKeyName() throws ExecutionException, InterruptedException {
 String bucketName = RemoteStorageHelper.generateBucketName();
 Bucket remoteBucket =
   storage.create(
     BucketInfo.newBuilder(bucketName)
       .setDefaultKmsKeyName(kmsKeyOneResourcePath)
       .setLocation(KMS_KEY_RING_LOCATION)
       .build());
 try {
  assertEquals(kmsKeyOneResourcePath, remoteBucket.getDefaultKmsKeyName());
  Bucket updatedBucket =
    remoteBucket.toBuilder().setDefaultKmsKeyName(kmsKeyTwoResourcePath).build().update();
  assertEquals(kmsKeyTwoResourcePath, updatedBucket.getDefaultKmsKeyName());
 } finally {
  RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
 }
}

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

@Test
public void testUpdateBucketRequesterPays() {
 Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
 assertNull(remoteBucket.requesterPays());
 remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
 Bucket updatedBucket = storage.update(remoteBucket);
 assertTrue(updatedBucket.requesterPays());
 String projectId = remoteStorageHelper.getOptions().getProjectId();
 Bucket.BlobTargetOption option = Bucket.BlobTargetOption.userProject(projectId);
 String blobName = "test-create-empty-blob-requester-pays";
 Blob remoteBlob = updatedBucket.create(blobName, BLOB_BYTE_CONTENT, option);
 assertNotNull(remoteBlob);
 byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
 assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
}

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

private void testBucketAclRequesterPays(boolean requesterPays) {
 if (requesterPays) {
  Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID));
  assertNull(remoteBucket.requesterPays());
  remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build();
  Bucket updatedBucket = storage.update(remoteBucket);
  assertTrue(updatedBucket.requesterPays());
 }
 String projectId = remoteStorageHelper.getOptions().getProjectId();
 Storage.BucketSourceOption[] bucketOptions =
   requesterPays
     ? new Storage.BucketSourceOption[] {Storage.BucketSourceOption.userProject(projectId)}
     : new Storage.BucketSourceOption[] {};
 assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
 assertFalse(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
 Acl acl = Acl.of(User.ofAllAuthenticatedUsers(), Role.READER);
 assertNotNull(storage.createAcl(BUCKET, acl, bucketOptions));
 Acl updatedAcl =
   storage.updateAcl(BUCKET, acl.toBuilder().setRole(Role.WRITER).build(), bucketOptions);
 assertEquals(Role.WRITER, updatedAcl.getRole());
 Set<Acl> acls = new HashSet<>();
 acls.addAll(storage.listAcls(BUCKET, bucketOptions));
 assertTrue(acls.contains(updatedAcl));
 assertTrue(storage.deleteAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
 assertNull(storage.getAcl(BUCKET, User.ofAllAuthenticatedUsers(), bucketOptions));
}

代码示例来源: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 testEnableDisableBucketDefaultEventBasedHold()
  throws ExecutionException, InterruptedException {
 String bucketName = RemoteStorageHelper.generateBucketName();
 Bucket remoteBucket =
   storage.create(BucketInfo.newBuilder(bucketName).setDefaultEventBasedHold(true).build());
 try {
  assertTrue(remoteBucket.getDefaultEventBasedHold());
  remoteBucket =
    storage.get(
      bucketName, Storage.BucketGetOption.fields(BucketField.DEFAULT_EVENT_BASED_HOLD));
  assertTrue(remoteBucket.getDefaultEventBasedHold());
  String blobName = "test-create-with-event-based-hold";
  BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, blobName).build();
  Blob remoteBlob = storage.create(blobInfo);
  assertTrue(remoteBlob.getEventBasedHold());
  remoteBlob =
    storage.get(
      blobInfo.getBlobId(), Storage.BlobGetOption.fields(BlobField.EVENT_BASED_HOLD));
  assertTrue(remoteBlob.getEventBasedHold());
  remoteBlob = remoteBlob.toBuilder().setEventBasedHold(false).build().update();
  assertFalse(remoteBlob.getEventBasedHold());
  remoteBucket = remoteBucket.toBuilder().setDefaultEventBasedHold(false).build().update();
  assertFalse(remoteBucket.getDefaultEventBasedHold());
 } finally {
  RemoteStorageHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
 }
}

相关文章