Java idiomatic client for Google Cloud Platform services.
- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/)
- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs)
This client supports the following Google Cloud Platform services:
- [Google Cloud Datastore] (#google-cloud-datastore)
- [Google Cloud Storage] (#google-cloud-storage)
Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.
Add this to your pom.xml file
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.0.10</version>
</dependency>
DatastoreExample
- A simple command line interface for the Cloud Datastore- Read more about using this application on the
gcloud-java-examples
docs page.
- Read more about using this application on the
StorageExample
- A simple command line interface providing some of Cloud Storage's functionality- Read more about using this application on the
gcloud-java-examples
docs page.
- Read more about using this application on the
Most gcloud-java
libraries require a project ID. There are multiple ways to specify this project ID.
- When using
gcloud-java
libraries from within Compute/App Engine, there's no need to specify a project ID. It is automatically inferred from the production environment. - When using
gcloud-java
elsewhere, you can do one of the following:
- Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
Datastore datastore = DatastoreOptions.builder().projectId("PROJECT_ID").build().service();
- Specify the environment variable
GCLOUD_PROJECT
. For example, type the following into command line:
export GCLOUD_PROJECT=PROJECT_ID
- Set the project ID using the Google Cloud SDK. To use the SDK, download the SDK if you haven't already, and set the project ID from the command line. For example:
gcloud config set project PROJECT_ID
gcloud-java
determines the project ID from the following sources in the listed order, stopping once it finds a value:
- Project ID supplied when building the service options.
- Project ID specified by the environment variable
GCLOUD_PROJECT
. - Project ID used by App Engine.
- Project ID specified in the Google Cloud SDK.
There are multiple ways to authenticate to use Google Cloud services.
- When using
gcloud-java
libraries from within Compute/App Engine, no additional authentication steps are necessary. - When using
gcloud-java
libraries elsewhere, there are two options:
- Generate a JSON service account key. After downloading that key, you must do one of the following:
- Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json
- Supply the JSON credentials file when building the service options. For example, this Storage object has the necessary permissions to interact with your Google Cloud Storage data:
Storage storage = StorageOptions.builder() .authCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json")) .build() .service();
- If running locally for development/testing, you can use use Google Cloud SDK. Download the SDK if you haven't already, then login using the SDK (
gcloud auth login
in command line). Be sure your current project is set correctly by runninggcloud config set project PROJECT_ID
.
gcloud-java
looks for credentials in the following order, stopping once it finds credentials:
- Credentials supplied when building the service options
- App Engine credentials
- Key file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
- Google Cloud SDK credentials
- Compute Engine credentials
Follow the activation instructions to use the Google Cloud Datastore API with your project.
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere.
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
Datastore datastore = DatastoreOptions.getDefaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
Key key = keyFactory.newKey(keyName);
Entity entity = datastore.get(key);
if (entity == null) {
entity = Entity.builder(key)
.set("name", "John Do")
.set("age", 30)
.set("access_time", DateTime.now())
.build();
datastore.put(entity);
} else {
System.out.println("Updating access_time for " + entity.getString("name"));
entity = Entity.builder(entity)
.set("access_time", DateTime.now())
.build();
datastore.update(entity);
}
Follow the activation instructions to use the Google Cloud Storage API with your project.
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must supply credentials and a project ID if running this snippet elsewhere.
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
StorageOptions options = StorageOptions.builder().projectId("project").build();
Storage storage = options.service();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = Blob.load(storage, blobId);
if (blob == null) {
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
} else {
System.out.println("Updating content for " + blobId.name());
byte[] prevContent = blob.content();
System.out.println(new String(prevContent, UTF_8));
WritableByteChannel channel = blob.writer();
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
channel.close();
}
Java 7 or above is required for using this client.
This library provides tools to help write tests for code that uses gcloud-java services.
See TESTING to read more about using our testing helpers.
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.
Contributions to this library are always welcome and highly encouraged.
See CONTRIBUTING 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 for more information.
Apache 2.0 - See LICENSE for more information.