/** * Example of reading all bytes of the blob, if its generation matches the {@link * Blob#getGeneration()} value, otherwise a {@link StorageException} is thrown. */ // [TARGET getContent(BlobSourceOption...)] public byte[] getContent() { // [START getContent] byte[] content = blob.getContent(BlobSourceOption.generationMatch()); // [END getContent] return content; }
assertNotNull(copyWriter.getResult().getKmsKeyName()); assertTrue(copyWriter.getResult().getKmsKeyName().startsWith(kmsKeyOneResourcePath)); assertArrayEquals(BLOB_BYTE_CONTENT, copyWriter.getResult().getContent()); assertEquals(metadata, copyWriter.getResult().getMetadata()); assertTrue(copyWriter.isDone());
assertArrayEquals( BLOB_BYTE_CONTENT, copyWriter.getResult().getContent(Blob.BlobSourceOption.decryptionKey(OTHER_BASE64_KEY))); assertEquals(metadata, copyWriter.getResult().getMetadata()); assertTrue(copyWriter.isDone()); assertEquals(targetBlobName, copyWriter.getResult().getName()); assertEquals(CONTENT_TYPE, copyWriter.getResult().getContentType()); assertArrayEquals(BLOB_BYTE_CONTENT, copyWriter.getResult().getContent()); assertEquals(metadata, copyWriter.getResult().getMetadata()); assertTrue(copyWriter.isDone());
private byte[] getRawBytes(String blobName) { Bucket bucket = storage.get(bucketName); Preconditions.checkNotNull(bucket, "Bucket [%s] not found", bucketName); Blob blob = bucket.get(blobName); Preconditions.checkNotNull(blob, "blob [%s] not found", blobName); return blob.getContent(); }
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(); } } }
public static void main(String... args) { // Create a service object // Credentials are inferred from the environment. Storage storage = StorageOptions.getDefaultInstance().getService(); // Create a bucket String bucketName = "my_unique_bucket"; // Change this to something unique Bucket bucket = storage.create(BucketInfo.of(bucketName)); // Upload a blob to the newly created bucket Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain"); // Read the blob content from the server String blobContent = new String(blob.getContent(), UTF_8); // List all your buckets System.out.println("My buckets:"); for (Bucket currentBucket : storage.list().iterateAll()) { System.out.println(currentBucket); } // List the blobs in a particular bucket System.out.println("My blobs:"); for (Blob currentBlob : bucket.list().iterateAll()) { System.out.println(currentBlob); } } }
@Test public void testContent() throws Exception { initializeExpectedBlob(2); byte[] content = {1, 2}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.readAllBytes(BLOB_INFO.getBlobId())).andReturn(content); replay(storage); initializeBlob(); assertArrayEquals(content, blob.getContent()); }
@Test public void testContentWithDecryptionKey() throws Exception { initializeExpectedBlob(2); byte[] content = {1, 2}; expect(storage.getOptions()).andReturn(mockOptions); expect( storage.readAllBytes( BLOB_INFO.getBlobId(), Storage.BlobSourceOption.decryptionKey(BASE64_KEY))) .andReturn(content) .times(2); replay(storage); initializeBlob(); assertArrayEquals(content, blob.getContent(BlobSourceOption.decryptionKey(BASE64_KEY))); assertArrayEquals(content, blob.getContent(BlobSourceOption.decryptionKey(KEY))); }
String jsonContents = new String(firstOutputFile.getContent()); Builder builder = AnnotateFileResponse.newBuilder(); JsonFormat.parser().merge(jsonContents, builder);