Java Code Examples for com.google.cloud.storage.Blob#getContent()

The following examples show how to use com.google.cloud.storage.Blob#getContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GoogleAppCredentialStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 2
Source File: StorageClientIT.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
private void testBucket(Bucket bucket) {
  assertEquals(IntegrationTestUtils.getStorageBucket(), bucket.getName());
  String fileName = createTextBlob(bucket, "Hello World").getName();

  Blob blob = bucket.get(fileName);
  byte[] content = blob.getContent();
  assertEquals("Hello World", new String(content));

  assertTrue(blob.delete());
  assertNull(bucket.get(fileName));
}
 
Example 3
Source File: OcrPageRange.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private static List<TextAnnotation> parseJsonBlob(Blob blob)
		throws InvalidProtocolBufferException {

	AnnotateFileResponse.Builder annotateFileResponseBuilder = AnnotateFileResponse.newBuilder();
	String jsonContent = new String(blob.getContent());
	JsonFormat.parser().ignoringUnknownFields().merge(jsonContent, annotateFileResponseBuilder);

	AnnotateFileResponse annotateFileResponse = annotateFileResponseBuilder.build();

	return annotateFileResponse.getResponsesList().stream()
			.map(AnnotateImageResponse::getFullTextAnnotation)
			.collect(Collectors.toList());
}
 
Example 4
Source File: CreateAndListBucketsAndBlobs.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 5
Source File: UpdateBlob.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example 6
Source File: ImageGcsDAOImpl.java    From android-uiconductor with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] getImage(String uuid) {
  Blob blob = storage.get(BlobId.of(BUCKET_NAME, uuid));
  return blob.getContent(Blob.BlobSourceOption.generationMatch());
}