Skip navigation links

Package com.google.cloud.storage

A client for Cloud Storage - Unified object storage.

See: Description

Package com.google.cloud.storage Description

A client for Cloud Storage - Unified object storage.

Here's a simple usage example for using google-cloud from App/Compute Engine. This example shows how to create a Storage blob. For the complete source code see CreateBlob.java.


 Storage storage = StorageOptions.getDefaultInstance().getService();
 BlobId blobId = BlobId.of("bucket", "blob_name");
 BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
 Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
 

This second example shows how to update the blob's content if the blob exists. For the complete source code see UpdateBlob.java.


 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();
 }
 

When using google-cloud from outside of App/Compute Engine, you have to specify a project ID and provide credentials.

See Also:
Google Cloud Storage
Skip navigation links

Copyright © 2019 Google LLC. All rights reserved.