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

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

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

Blob.toBuilder介绍

暂无

代码示例

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

/** Example of replacing blob's metadata. */
// [TARGET update(BlobTargetOption...)]
public Blob update() {
 // [START update]
 Map<String, String> newMetadata = new HashMap<>();
 newMetadata.put("key", "value");
 blob.toBuilder().setMetadata(null).build().update();
 Blob updatedBlob = blob.toBuilder().setMetadata(newMetadata).build().update();
 // [END update]
 return updatedBlob;
}

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

private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
 Blob blob = storage.get(blobId);
 if (blob == null) {
  System.out.println("No such object");
  return;
 }
 Blob updateBlob = blob.toBuilder().setMetadata(metadata).build().update();
 System.out.printf("Updated %s%n", updateBlob);
}

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

/** Example of updating information on several blobs using a single batch request. */
// [TARGET update(Iterable)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public List<Blob> batchUpdateIterable(String bucketName, String blobName1, String blobName2) {
 // [START batchUpdateIterable]
 Blob firstBlob = storage.get(bucketName, blobName1);
 Blob secondBlob = storage.get(bucketName, blobName2);
 List<BlobInfo> blobs = new LinkedList<>();
 blobs.add(firstBlob.toBuilder().setContentType("text/plain").build());
 blobs.add(secondBlob.toBuilder().setContentType("text/plain").build());
 List<Blob> updatedBlobs = storage.update(blobs);
 // [END batchUpdateIterable]
 return updatedBlobs;
}

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

/** Example of updating information on several blobs using a single batch request. */
// [TARGET update(BlobInfo...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name1"]
// [VARIABLE "my_blob_name2"]
public List<Blob> batchUpdate(String bucketName, String blobName1, String blobName2) {
 // [START batchUpdate]
 Blob firstBlob = storage.get(bucketName, blobName1);
 Blob secondBlob = storage.get(bucketName, blobName2);
 List<Blob> updatedBlobs =
   storage.update(
     firstBlob.toBuilder().setContentType("text/plain").build(),
     secondBlob.toBuilder().setContentType("text/plain").build());
 // [END batchUpdate]
 return updatedBlobs;
}

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

/**
 * Example of udating a blob, only if the blob's metageneration matches a value, otherwise a
 * {@link StorageException} is thrown.
 */
// [TARGET update(BlobInfo, BlobTargetOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public Blob updateBlobWithMetageneration(String bucketName, String blobName) {
 // [START updateBlobWithMetageneration]
 Blob blob = storage.get(bucketName, blobName);
 BlobInfo updatedInfo = blob.toBuilder().setContentType("text/plain").build();
 storage.update(updatedInfo, BlobTargetOption.metagenerationMatch());
 // [END updateBlobWithMetageneration]
 return blob;
}

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

@Test
public void testReload() throws Exception {
 initializeExpectedBlob(2);
 Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build();
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.get(BLOB_INFO.getBlobId(), new Storage.BlobGetOption[0]))
   .andReturn(expectedReloadedBlob);
 replay(storage);
 initializeBlob();
 Blob updatedBlob = blob.reload();
 assertEquals(expectedReloadedBlob, updatedBlob);
}

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

@Test
public void testReloadWithOptions() throws Exception {
 initializeExpectedBlob(2);
 Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build();
 Storage.BlobGetOption[] options = {Storage.BlobGetOption.metagenerationMatch(42L)};
 expect(storage.getOptions()).andReturn(mockOptions);
 expect(storage.get(BLOB_INFO.getBlobId(), options)).andReturn(expectedReloadedBlob);
 replay(storage);
 initializeBlob();
 Blob updatedBlob = blob.reload(BlobSourceOption.metagenerationMatch());
 assertEquals(expectedReloadedBlob, updatedBlob);
}

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

@Test
public void testToBuilder() {
 expect(storage.getOptions()).andReturn(mockOptions).times(6);
 replay(storage);
 Blob fullBlob = new Blob(storage, new BlobInfo.BuilderImpl(FULL_BLOB_INFO));
 assertEquals(fullBlob, fullBlob.toBuilder().build());
 Blob simpleBlob = new Blob(storage, new BlobInfo.BuilderImpl(BLOB_INFO));
 assertEquals(simpleBlob, simpleBlob.toBuilder().build());
 Blob directory = new Blob(storage, new BlobInfo.BuilderImpl(DIRECTORY_INFO));
 assertEquals(directory, directory.toBuilder().build());
}

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

@Test
public void testUpdate() throws Exception {
 initializeExpectedBlob(2);
 Blob expectedUpdatedBlob = expectedBlob.toBuilder().setCacheControl("c").build();
 expect(storage.getOptions()).andReturn(mockOptions).times(2);
 expect(storage.update(eq(expectedUpdatedBlob), new Storage.BlobTargetOption[0]))
   .andReturn(expectedUpdatedBlob);
 replay(storage);
 initializeBlob();
 Blob updatedBlob = new Blob(storage, new BlobInfo.BuilderImpl(expectedUpdatedBlob));
 Blob actualUpdatedBlob = updatedBlob.update();
 assertEquals(expectedUpdatedBlob, actualUpdatedBlob);
}

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

@Test
public void testUpdateBlobUnsetMetadata() {
 String blobName = "test-update-blob-unset-metadata";
 ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a", "k2", "b");
 Map<String, String> newMetadata = new HashMap<>();
 newMetadata.put("k1", "a");
 newMetadata.put("k2", null);
 ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a");
 BlobInfo blob =
   BlobInfo.newBuilder(BUCKET, blobName)
     .setContentType(CONTENT_TYPE)
     .setMetadata(metadata)
     .build();
 Blob remoteBlob = storage.create(blob);
 assertNotNull(remoteBlob);
 Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update();
 assertNotNull(updatedBlob);
 assertEquals(blob.getName(), updatedBlob.getName());
 assertEquals(blob.getBucket(), updatedBlob.getBucket());
 assertEquals(expectedMetadata, updatedBlob.getMetadata());
}

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

@Test
public void testUpdateBlobs() {
 String sourceBlobName1 = "test-update-blobs-1";
 String sourceBlobName2 = "test-update-blobs-2";
 BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
 BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
 Blob remoteBlob1 = storage.create(sourceBlob1);
 Blob remoteBlob2 = storage.create(sourceBlob2);
 assertNotNull(remoteBlob1);
 assertNotNull(remoteBlob2);
 List<Blob> updatedBlobs =
   storage.update(
     remoteBlob1.toBuilder().setContentType(CONTENT_TYPE).build(),
     remoteBlob2.toBuilder().setContentType(CONTENT_TYPE).build());
 assertEquals(sourceBlob1.getBucket(), updatedBlobs.get(0).getBucket());
 assertEquals(sourceBlob1.getName(), updatedBlobs.get(0).getName());
 assertEquals(CONTENT_TYPE, updatedBlobs.get(0).getContentType());
 assertEquals(sourceBlob2.getBucket(), updatedBlobs.get(1).getBucket());
 assertEquals(sourceBlob2.getName(), updatedBlobs.get(1).getName());
 assertEquals(CONTENT_TYPE, updatedBlobs.get(1).getContentType());
}

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

@Test
public void testUpdateBlobMergeMetadata() {
 String blobName = "test-update-blob-merge-metadata";
 ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a");
 ImmutableMap<String, String> newMetadata = ImmutableMap.of("k2", "b");
 ImmutableMap<String, String> expectedMetadata = ImmutableMap.of("k1", "a", "k2", "b");
 BlobInfo blob =
   BlobInfo.newBuilder(BUCKET, blobName)
     .setContentType(CONTENT_TYPE)
     .setMetadata(metadata)
     .build();
 Blob remoteBlob = storage.create(blob);
 assertNotNull(remoteBlob);
 Blob updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update();
 assertNotNull(updatedBlob);
 assertEquals(blob.getName(), updatedBlob.getName());
 assertEquals(blob.getBucket(), updatedBlob.getBucket());
 assertEquals(expectedMetadata, updatedBlob.getMetadata());
}

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

@Test
public void testUpdateBlobReplaceMetadata() {
 String blobName = "test-update-blob-replace-metadata";
 ImmutableMap<String, String> metadata = ImmutableMap.of("k1", "a");
 ImmutableMap<String, String> newMetadata = ImmutableMap.of("k2", "b");
 BlobInfo blob =
   BlobInfo.newBuilder(BUCKET, blobName)
     .setContentType(CONTENT_TYPE)
     .setMetadata(metadata)
     .build();
 Blob remoteBlob = storage.create(blob);
 assertNotNull(remoteBlob);
 Blob updatedBlob = remoteBlob.toBuilder().setMetadata(null).build().update();
 assertNotNull(updatedBlob);
 assertNull(updatedBlob.getMetadata());
 updatedBlob = remoteBlob.toBuilder().setMetadata(newMetadata).build().update();
 assertEquals(blob.getName(), updatedBlob.getName());
 assertEquals(blob.getBucket(), updatedBlob.getBucket());
 assertEquals(newMetadata, updatedBlob.getMetadata());
}

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

@Override
 public void run() {
  Page<Bucket> buckets =
    storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
  for (Bucket bucket : buckets.iterateAll()) {
   if (bucket.getCreateTime() < olderThan) {
    try {
     for (Blob blob :
       bucket
         .list(
           BlobListOption.fields(
             Storage.BlobField.EVENT_BASED_HOLD,
             Storage.BlobField.TEMPORARY_HOLD))
         .iterateAll()) {
      if (blob.getEventBasedHold() == true || blob.getTemporaryHold() == true) {
       storage.update(
         blob.toBuilder()
           .setTemporaryHold(false)
           .setEventBasedHold(false)
           .build());
      }
     }
     forceDelete(storage, bucket.getName());
    } catch (Exception e) {
     // Ignore the exception, maybe the bucket is being deleted by someone else.
    }
   }
  }
 }
};

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

@Test
public void testAttemptDeletionObjectTemporaryHold() {
 String blobName = "test-create-with-temporary-hold";
 BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).setTemporaryHold(true).build();
 Blob remoteBlob = storage.create(blobInfo);
 assertTrue(remoteBlob.getTemporaryHold());
 try {
  remoteBlob.delete();
  fail("Expected failure on delete from temporaryHold");
 } catch (StorageException ex) {
  // expected
 } finally {
  remoteBlob.toBuilder().setEventBasedHold(false).build().update();
 }
}

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

@Test
public void testAttemptObjectDeleteWithEventBasedHold() {
 String blobName = "test-create-with-event-based-hold";
 BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).setEventBasedHold(true).build();
 Blob remoteBlob = storage.create(blobInfo);
 assertTrue(remoteBlob.getEventBasedHold());
 try {
  remoteBlob.delete();
  fail("Expected failure on delete from eventBasedHold");
 } catch (StorageException ex) {
  // expected
 } finally {
  remoteBlob.toBuilder().setEventBasedHold(false).build().update();
 }
}

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

@Test
public void testEnableDisableTemporaryHold() {
 String blobName = "test-create-with-temporary-hold";
 BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).setTemporaryHold(true).build();
 Blob remoteBlob = storage.create(blobInfo);
 assertTrue(remoteBlob.getTemporaryHold());
 remoteBlob =
   storage.get(remoteBlob.getBlobId(), Storage.BlobGetOption.fields(BlobField.TEMPORARY_HOLD));
 assertTrue(remoteBlob.getTemporaryHold());
 remoteBlob = remoteBlob.toBuilder().setTemporaryHold(false).build().update();
 assertFalse(remoteBlob.getTemporaryHold());
}

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

@Test
public void testUpdateBlob() {
 String blobName = "test-update-blob";
 BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
 Blob remoteBlob = storage.create(blob);
 assertNotNull(remoteBlob);
 Blob updatedBlob = remoteBlob.toBuilder().setContentType(CONTENT_TYPE).build().update();
 assertNotNull(updatedBlob);
 assertEquals(blob.getName(), updatedBlob.getName());
 assertEquals(blob.getBucket(), updatedBlob.getBucket());
 assertEquals(CONTENT_TYPE, updatedBlob.getContentType());
}

代码示例来源: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);
 }
}

代码示例来源:origin: com.google.cloud/google-cloud-storage

@Override
 public void run() {
  Page<Bucket> buckets =
    storage.list(Storage.BucketListOption.prefix(BUCKET_NAME_PREFIX));
  for (Bucket bucket : buckets.iterateAll()) {
   if (bucket.getCreateTime() < olderThan) {
    try {
     for (Blob blob :
       bucket
         .list(
           BlobListOption.fields(
             Storage.BlobField.EVENT_BASED_HOLD,
             Storage.BlobField.TEMPORARY_HOLD))
         .iterateAll()) {
      if (blob.getEventBasedHold() == true || blob.getTemporaryHold() == true) {
       storage.update(
         blob.toBuilder()
           .setTemporaryHold(false)
           .setEventBasedHold(false)
           .build());
      }
     }
     forceDelete(storage, bucket.getName());
    } catch (Exception e) {
     // Ignore the exception, maybe the bucket is being deleted by someone else.
    }
   }
  }
 }
};

相关文章