use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testWriteChannelExistingBlob.
@Test
public void testWriteChannelExistingBlob() throws IOException {
String blobName = "test-write-channel-existing-blob";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
storage.create(blob);
byte[] stringBytes;
try (WriteChannel writer = storage.writer(blob)) {
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
writer.write(ByteBuffer.wrap(stringBytes));
}
assertArrayEquals(stringBytes, storage.readAllBytes(blob.getBlobId()));
assertTrue(storage.delete(BUCKET, blobName));
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testUpdateBlobReplaceMetadata.
@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());
assertTrue(updatedBlob.delete());
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testGetBlobs.
@Test
public void testGetBlobs() {
String sourceBlobName1 = "test-get-blobs-1";
String sourceBlobName2 = "test-get-blobs-2";
BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build();
BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build();
assertNotNull(storage.create(sourceBlob1));
assertNotNull(storage.create(sourceBlob2));
List<Blob> remoteBlobs = storage.get(sourceBlob1.getBlobId(), sourceBlob2.getBlobId());
assertEquals(sourceBlob1.getBucket(), remoteBlobs.get(0).getBucket());
assertEquals(sourceBlob1.getName(), remoteBlobs.get(0).getName());
assertEquals(sourceBlob2.getBucket(), remoteBlobs.get(1).getBucket());
assertEquals(sourceBlob2.getName(), remoteBlobs.get(1).getName());
assertTrue(remoteBlobs.get(0).delete());
assertTrue(remoteBlobs.get(1).delete());
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testReadCompressedBlob.
@Test
public void testReadCompressedBlob() throws IOException {
String blobName = "test-read-compressed-blob";
BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(BUCKET, blobName)).setContentType("text/plain").setContentEncoding("gzip").build();
Blob blob = storage.create(blobInfo, COMPRESSED_CONTENT);
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
try (ReadChannel reader = storage.reader(BlobId.of(BUCKET, blobName))) {
reader.setChunkSize(8);
ByteBuffer buffer = ByteBuffer.allocate(8);
while (reader.read(buffer) != -1) {
buffer.flip();
output.write(buffer.array(), 0, buffer.limit());
buffer.clear();
}
}
assertArrayEquals(BLOB_STRING_CONTENT.getBytes(UTF_8), storage.readAllBytes(BUCKET, blobName));
assertArrayEquals(COMPRESSED_CONTENT, output.toByteArray());
try (GZIPInputStream zipInput = new GZIPInputStream(new ByteArrayInputStream(output.toByteArray()))) {
assertArrayEquals(BLOB_STRING_CONTENT.getBytes(UTF_8), ByteStreams.toByteArray(zipInput));
}
}
blob.delete();
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageTest method testCreateBlobWithEncryptionKey.
@Test
public void testCreateBlobWithEncryptionKey() {
String blobName = "test-create-with-customer-key-blob";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.encryptionKey(KEY));
assertNotNull(remoteBlob);
assertEquals(blob.getBucket(), remoteBlob.getBucket());
assertEquals(blob.getName(), remoteBlob.getName());
byte[] readBytes = storage.readAllBytes(BUCKET, blobName, Storage.BlobSourceOption.decryptionKey(BASE64_KEY));
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
assertTrue(remoteBlob.delete());
}
Aggregations