Search in sources :

Example 16 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class StorageSnippets method writer.

/**
   * Example of writing a blob's content through a writer.
   */
// [TARGET writer(BlobInfo, BlobWriteOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
public void writer(String bucketName, String blobName) throws IOException {
    // [START writer]
    BlobId blobId = BlobId.of(bucketName, blobName);
    byte[] content = "Hello, World!".getBytes(UTF_8);
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    try (WriteChannel writer = storage.writer(blobInfo)) {
        try {
            writer.write(ByteBuffer.wrap(content, 0, content.length));
        } catch (Exception ex) {
        // handle exception
        }
    }
// [END writer]
}
Also used : WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) BlobId(com.google.cloud.storage.BlobId) IOException(java.io.IOException) StorageException(com.google.cloud.storage.StorageException)

Example 17 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class StorageImplTest method testWriterWithEncryptionKey.

@Test
public void testWriterWithEncryptionKey() {
    BlobInfo info = BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build();
    EasyMock.expect(storageRpcMock.open(info.toPb(), ENCRYPTION_KEY_OPTIONS)).andReturn("upload-id").times(2);
    EasyMock.replay(storageRpcMock);
    initializeService();
    WriteChannel channel = storage.writer(info, BlobWriteOption.encryptionKey(KEY));
    assertNotNull(channel);
    assertTrue(channel.isOpen());
    channel = storage.writer(info, BlobWriteOption.encryptionKey(BASE64_KEY));
    assertNotNull(channel);
    assertTrue(channel.isOpen());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Example 18 with WriteChannel

use of com.google.cloud.WriteChannel in project google-cloud-java by GoogleCloudPlatform.

the class TableDataWriteChannelTest method testSaveAndRestoreClosed.

@Test
public void testSaveAndRestoreClosed() throws IOException {
    expect(bigqueryRpcMock.open(LOAD_CONFIGURATION.toPb())).andReturn(UPLOAD_ID);
    Capture<byte[]> capturedBuffer = Capture.newInstance();
    expect(bigqueryRpcMock.write(eq(UPLOAD_ID), capture(capturedBuffer), eq(0), eq(0L), eq(0), eq(true))).andReturn(job.toPb());
    replay(bigqueryRpcMock);
    writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION);
    writer.close();
    assertEquals(job, writer.getJob());
    RestorableState<WriteChannel> writerState = writer.capture();
    RestorableState<WriteChannel> expectedWriterState = TableDataWriteChannel.StateImpl.builder(options, LOAD_CONFIGURATION, UPLOAD_ID, job).setBuffer(null).setChunkSize(DEFAULT_CHUNK_SIZE).setIsOpen(false).setPosition(0).build();
    WriteChannel restoredWriter = writerState.restore();
    assertArrayEquals(new byte[0], capturedBuffer.getValue());
    assertEquals(expectedWriterState, restoredWriter.capture());
}
Also used : WriteChannel(com.google.cloud.WriteChannel) Test(org.junit.Test)

Aggregations

WriteChannel (com.google.cloud.WriteChannel)18 Test (org.junit.Test)17 BlobInfo (com.google.cloud.storage.BlobInfo)8 ByteBuffer (java.nio.ByteBuffer)7 ReadChannel (com.google.cloud.ReadChannel)5 StorageException (com.google.cloud.storage.StorageException)3 Random (java.util.Random)2 EasyMock.captureLong (org.easymock.EasyMock.captureLong)2 Blob (com.google.cloud.storage.Blob)1 BlobId (com.google.cloud.storage.BlobId)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1