Google Cloud Java Client for Storage
====================================
Java idiomatic client for [Google Cloud Storage] (https://cloud.google.com/storage/).
[](https://travis-ci.org/GoogleCloudPlatform/gcloud-java)
[](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master)
[]( https://img.shields.io/maven-central/v/com.google.gcloud/gcloud-java-storage.svg)
- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/storage/package-summary.html)
> Note: This client is a work-in-progress, and may occasionally
> make backwards-incompatible changes.
Quickstart
----------
If you are using Maven, add this to your pom.xml file
```xml
com.google.gcloud
gcloud-java-storage
0.1.0
```
If you are using Gradle, add this to your dependencies
```Groovy
compile 'com.google.gcloud:gcloud-java-storage:jar:0.1.0'
```
If you are using SBT, add this to your dependencies
```Scala
libraryDependencies += "com.google.gcloud" % "gcloud-java-storage" % "0.1.0"
```
Example Application
-------------------
[`StorageExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java) is a simple command line interface that provides some of Cloud Storage's functionality. Read more about using the application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/StorageExample.html).
Authentication
--------------
See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README.
About Google Cloud Storage
--------------------------
[Google Cloud Storage][cloud-storage] is a durable and highly available
object storage service. Google Cloud Storage is almost infinitely scalable
and guarantees consistency: when a write succeeds, the latest copy of the
object will be returned to any GET, globally.
See the [Google Cloud Storage docs][cloud-storage-activation] for more details on how to activate
Cloud Storage for your project.
See the ``gcloud-java`` API [storage documentation][storage-api] to learn how to interact
with the Cloud Storage using this Client Library.
Getting Started
---------------
#### Prerequisites
For this tutorial, you will need a [Google Developers Console](https://console.developers.google.com/) project with the Storage JSON API enabled. You will need to [enable billing](https://support.google.com/cloud/answer/6158867?hl=en) to use Google Cloud Storage. [Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your project set up. You will also need to set up the local development environment by [installing the Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: `gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`.
#### Installation and setup
You'll need to obtain the `gcloud-java-storage` library. See the [Quickstart](#quickstart) section to add `gcloud-java-storage` as a dependency in your code.
#### Creating an authorized service object
To make authenticated requests to Google Cloud Storage, you must create a service object with credentials. You can then make API calls by calling methods on the Storage service object. The simplest way to authenticate is to use [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials). These credentials are automatically inferred from your environment, so you only need the following code to create your service object:
```java
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
Storage storage = StorageOptions.defaultInstance().service();
```
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page.
#### Storing data
Stored objects are called "blobs" in `gcloud-java` and are organized into containers called "buckets". In this code snippet, we will create a new bucket and upload a blob to that bucket.
Add the following imports at the top of your file:
```java
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.BucketInfo;
```
Then add the following code to create a bucket and upload a simple blob.
*Important: Bucket names have to be globally unique. If you choose a bucket name that already exists, you'll get a helpful error message telling you to choose another name. In the code below, replace "my_unique_bucket" with a unique bucket name. See more about naming rules [here](https://cloud.google.com/storage/docs/bucket-naming?hl=en#requirements).*
```java
// Create a bucket
String bucketName = "my_unique_bucket"; // Change this to something unique
BucketInfo bucketInfo = storage.create(BucketInfo.of(bucketName));
// Upload a blob to the newly created bucket
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
BlobInfo blobInfo = storage.create(
BlobInfo.builder(blobId).contentType("text/plain").build(),
"a simple blob".getBytes(UTF_8));
```
At this point, you will be able to see your newly created bucket and blob on the Google Developers Console.
#### Retrieving data
Now that we have content uploaded to the server, we can see how to read data from the server. Add the following line to your program to get back the blob we uploaded.
```java
String blobContent = new String(storage.readAllBytes(blobId), UTF_8);
```
#### Listing buckets and contents of buckets
Suppose that you've added more buckets and blobs, and now you want to see the names of your buckets and the contents of each one. Add the following imports:
```java
import java.util.Iterator;
```
Then add the following code to list all your buckets and all the blobs inside your newly created bucket.
```java
// List all your buckets
Iterator bucketInfoIterator = storage.list().iterateAll();
System.out.println("My buckets:");
while (bucketInfoIterator.hasNext()) {
System.out.println(bucketInfoIterator.next());
}
// List the blobs in a particular bucket
Iterator blobIterator = storage.list(bucketName).iterateAll();
System.out.println("My blobs:");
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next());
}
```
#### Complete source code
Here we put together all the code shown above into one program. This program assumes that you are running on Compute Engine or from your own desktop. To run this example on App Engine, simply move the code from the main method to your application's servlet class and change the print statements to display on your webpage.
```java
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.BucketInfo;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
import java.util.Iterator;
public class GcloudStorageExample {
public static void main(String[] args) {
// Create a service object
// Credentials are inferred from the environment.
Storage storage = StorageOptions.defaultInstance().service();
// Create a bucket
String bucketName = "my_unique_bucket"; // Change this to something unique
BucketInfo bucketInfo = storage.create(BucketInfo.of(bucketName));
// Upload a blob to the newly created bucket
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
BlobInfo blobInfo = storage.create(
BlobInfo.builder(blobId).contentType("text/plain").build(),
"a simple blob".getBytes(UTF_8));
// Retrieve a blob from the server
String blobContent = new String(storage.readAllBytes(blobId), UTF_8);
// List all your buckets
Iterator bucketInfoIterator = storage.list().iterateAll();
System.out.println("My buckets:");
while (bucketInfoIterator.hasNext()) {
System.out.println(bucketInfoIterator.next());
}
// List the blobs in a particular bucket
Iterator blobIterator = storage.list(bucketName).iterateAll();
System.out.println("My blobs:");
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next());
}
}
}
```
Troubleshooting
---------------
To get help, follow the `gcloud-java` links in the `gcloud-*` [shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting).
Java Versions
-------------
Java 7 or above is required for using this client.
Testing
-------
This library has tools to help make tests for code using Cloud Storage.
See [TESTING] to read more about testing.
Versioning
----------
This library follows [Semantic Versioning] (http://semver.org/).
It is currently in major version zero (``0.y.z``), which means that anything
may change at any time and the public API should not be considered
stable.
Contributing
------------
Contributions to this library are always welcome and highly encouraged.
See `gcloud-java`'s [CONTRIBUTING] documentation and the `gcloud-*` [shared documentation](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/contributing/readme.md#how-to-contribute-to-gcloud) for more information on how to get started.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See [Code of Conduct][code-of-conduct] for more information.
License
-------
Apache 2.0 - See [LICENSE] for more information.
[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md
[code-of-conduct]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CODE_OF_CONDUCT.md#contributor-code-of-conduct
[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE
[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-storage
[cloud-platform]: https://cloud.google.com/
[cloud-storage]: https://cloud.google.com/storage/
[cloud-storage-docs]: https://cloud.google.com/storage/docs/overview
[cloud-storage-create-bucket]: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
[storage-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/storage/package-summary.html
[cloud-storage-activation]:https://cloud.google.com/storage/docs/signup?hl=en