use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testUpdateBlobUnsetMetadata.
@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());
assertTrue(updatedBlob.delete());
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testUpdateBlobs.
@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());
assertTrue(updatedBlobs.get(0).delete());
assertTrue(updatedBlobs.get(1).delete());
}
use of com.google.cloud.storage.Blob in project protoman by spotify.
the class GcsContentAddressedBlobStorage method get.
@Override
public Optional<byte[]> get(final HashCode contentHash) {
final String blobName = blobName(contentHash);
final Blob blob = storage.get(BlobId.of(bucket, blobName));
return Optional.ofNullable(blob.getContent());
}
use of com.google.cloud.storage.Blob in project protoman by spotify.
the class GcsGenerationalFile method replace.
public long replace(byte[] bytes) {
Preconditions.checkState(generation != null, "File is not loaded.");
try {
final Blob blob = storage.create(BlobInfo.newBuilder(bucket, path, generation).build(), bytes, Storage.BlobTargetOption.generationMatch());
generation = blob.getGeneration();
return generation;
} catch (StorageException ex) {
// allow precondition failed, which means we already have a file
if (ex.getCode() != GCS_PRECONDITION_FAILED_CODE) {
throw ex;
}
throw new OptimisticLockingException("Index file has been modified.", ex);
}
}
use of com.google.cloud.storage.Blob in project google-cloud-java by GoogleCloudPlatform.
the class BlobSnippets method copyToId.
/**
* Example of copying the blob to a different bucket with a different name.
*/
// [TARGET copyTo(BlobId, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "copy_blob_name"]
public Blob copyToId(String bucketName, String blobName) {
// [START copyToId]
CopyWriter copyWriter = blob.copyTo(BlobId.of(bucketName, blobName));
Blob copiedBlob = copyWriter.getResult();
// [END copyToId]
return copiedBlob;
}
Aggregations