本文整理了Java中com.google.cloud.storage.Blob.update()
方法的一些代码示例,展示了Blob.update()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Blob.update()
方法的具体详情如下:
包路径:com.google.cloud.storage.Blob
类名称:Blob
方法名:update
[英]Updates the blob's information. Bucket or blob's name cannot be changed by this method. If you want to rename the blob or move it to a different bucket use the #copyTo and #delete operations. A new Blob object is returned. By default no checks are made on the metadata generation of the current blob. If you want to update the information only if the current blob metadata are at their latest version use the metagenerationMatch option: newBlob.update(BlobTargetOption.metagenerationMatch()).
Original metadata are merged with metadata in the provided in this blob. To replace metadata instead you first have to unset them. Unsetting metadata can be done by setting this blob's metadata to null.
Example of replacing blob's metadata.
Map newMetadata = new HashMap<>();
[中]更新blob的信息。此方法无法更改Bucket或blob的名称。如果要重命名blob或将其移动到其他bucket,请使用#copyTo和#delete操作。返回一个新的Blob对象。默认情况下,不会对当前blob的元数据生成进行检查。如果只想在当前blob元数据为最新版本时更新信息,请使用metagenerationMatch选项:newBlob。更新(BlobTargetOption.metagenerationMatch())。
原始元数据与此blob中提供的中的元数据合并。要替换元数据,首先必须取消设置元数据。可以通过将此blob的元数据设置为null来取消设置元数据。
替换blob元数据的示例。
Map newMetadata = new HashMap<>();
代码示例来源: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
@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 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
@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);
}
}
内容来源于网络,如有侵权,请联系作者删除!