/** Example of writing the blob's content through a writer. */ // [TARGET writer(BlobWriteOption...)] public void writer() throws IOException { // [START writer] byte[] content = "Hello, World!".getBytes(UTF_8); try (WriteChannel writer = blob.writer()) { try { writer.write(ByteBuffer.wrap(content, 0, content.length)); } catch (Exception ex) { // handle exception } } // [END writer] }
public static void main(String... args) throws IOException { Storage storage = StorageOptions.getDefaultInstance().getService(); BlobId blobId = BlobId.of("bucket", "blob_name"); Blob blob = storage.get(blobId); if (blob != null) { byte[] prevContent = blob.getContent(); System.out.println(new String(prevContent, UTF_8)); WritableByteChannel channel = blob.writer(); channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8))); channel.close(); } } }
@Test public void testWriter() throws Exception { initializeExpectedBlob(2); BlobWriteChannel channel = createMock(BlobWriteChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.writer(eq(expectedBlob))).andReturn(channel); replay(storage); initializeBlob(); assertSame(channel, blob.writer()); }
@Test public void testWriterWithEncryptionKey() throws Exception { initializeExpectedBlob(2); BlobWriteChannel channel = createMock(BlobWriteChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.encryptionKey(BASE64_KEY)))) .andReturn(channel) .times(2); replay(storage); initializeBlob(); assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(BASE64_KEY))); assertSame(channel, blob.writer(BlobWriteOption.encryptionKey(KEY))); }
@Test public void testWriterWithKmsKeyName() throws Exception { initializeExpectedBlob(2); BlobWriteChannel channel = createMock(BlobWriteChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.writer(eq(expectedBlob), eq(BlobWriteOption.kmsKeyName(KMS_KEY_NAME)))) .andReturn(channel); replay(storage); initializeBlob(); assertSame(channel, blob.writer(BlobWriteOption.kmsKeyName(KMS_KEY_NAME))); }
/** * Returns the output stream for a Google Cloud Storage file. * @return the object's output stream or {@code null} if the object doesn't exist and cannot be * created * @throws IOException if an issue occurs getting the OutputStream */ @Override public OutputStream getOutputStream() throws IOException { if (isBucket()) { throw new IllegalStateException("Cannot open an output stream to a bucket: '" + this.location + "'"); } else { Blob blob = getBlob(); if (blob == null || !blob.exists()) { if (!this.autoCreateFiles) { throw new FileNotFoundException("The blob was not found: " + this.location); } blob = createBlob(); } return Channels.newOutputStream(blob.writer()); } }
/** * Returns the output stream for a Google Cloud Storage file. * @return the object's output stream or {@code null} if the object doesn't exist and cannot be * created * @throws IOException if an issue occurs getting the OutputStream */ @Override public OutputStream getOutputStream() throws IOException { if (isBucket()) { throw new IllegalStateException("Cannot open an output stream to a bucket: '" + this.location + "'"); } else { Blob blob = getBlob(); if (blob == null || !blob.exists()) { if (!this.autoCreateFiles) { throw new FileNotFoundException("The blob was not found: " + this.location); } blob = createBlob(); } return Channels.newOutputStream(blob.writer()); } }
@Override public void store(Value v) throws IOException { if (!v._key.home()) return; final byte payload[] = v.memOrLoad(); final GcsBlob blob = GcsBlob.of(v._key); Log.debug("Storing: " + blob.toString()); final ByteBuffer buffer = ByteBuffer.wrap(payload); storageProvider.getStorage().create(blob.getBlobInfo()).writer().write(buffer); }
@Override public OutputStream create(String path, boolean overwrite) { final GcsBlob gcsBlob = GcsBlob.of(path); Log.debug("Creating: " + gcsBlob.getCanonical()); final WriteChannel writer = storageProvider.getStorage().create(gcsBlob.getBlobInfo()).writer(); return new OutputStream() { @Override public void write(int b) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(new byte[]{(byte) b}); writer.write(buffer); } @Override public void write(byte[] b) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(b); writer.write(buffer); } @Override public void write(byte[] b, int off, int len) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(b, off, len); writer.write(buffer); } @Override public void close() throws IOException { writer.close(); } }; }