Java idiomatic client for [Google Cloud Storage] (https://cloud.google.com/storage/).
- [Homepage] (https://googlecloudplatform.github.io/google-cloud-java/)
- [API Documentation] (https://googlecloudplatform.github.io/google-cloud-java/apidocs/index.html?com/google/cloud/storage/package-summary.html)
Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>0.3.0</version>
</dependency>
If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:google-cloud-storage:0.3.0'
If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "0.3.0"
StorageExample
is a simple command line interface that provides some of Cloud Storage's functionality. Read more about using the application on the StorageExample
docs page.
See the Authentication section in the base directory's README.
Google 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 for more details on how to activate Cloud Storage for your project.
See the google-cloud
API storage documentation to learn how to interact
with the Cloud Storage using this Client Library.
For this tutorial, you will need a Google Developers Console project with "Google Cloud Storage" and "Google Cloud Storage JSON API" enabled via the console's API Manager. You will need to enable billing to use Google Cloud Storage. Follow these instructions to get your project set up. You will also need to set up the local development environment by installing the Google Cloud SDK and running the following commands in command line: gcloud auth login
and gcloud config set project [YOUR PROJECT ID]
.
You'll need to obtain the google-cloud-storage
library. See the Quickstart section to add google-cloud-storage
as a dependency in your code.
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. These credentials are automatically inferred from your environment, so you only need the following code to create your service object:
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
Storage storage = StorageOptions.defaultInstance().service();
For other authentication options, see the Authentication page.
Stored objects are called "blobs" in google-cloud
and are organized into containers called "buckets". Blob
, a subclass of BlobInfo
, adds a layer of service-related functionality over BlobInfo
. Similarly, Bucket
adds a layer of service-related functionality over BucketInfo
. 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:
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.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.
// 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
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
Blob blob = bucket.create(
"my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");
At this point, you will be able to see your newly created bucket and blob on the Google Developers Console.
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.
String blobContent = new String(blob.content(), UTF_8);
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:
import java.util.Iterator;
Then add the following code to list all your buckets and all the blobs inside your newly created bucket.
// List all your buckets
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
System.out.println("My buckets:");
while (bucketIterator.hasNext()) {
System.out.println(bucketIterator.next());
}
// List the blobs in a particular bucket
Iterator<Blob> blobIterator = bucket.list().iterateAll();
System.out.println("My blobs:");
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next());
}
In CreateAndListBucketsAndBlobs.java we put together all the code shown above into one program. The program assumes that you are running on Compute Engine or from your own desktop. To run the 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.
To get help, follow the instructions in the shared Troubleshooting document.
Java 7 or above is required for using this client.
This library has tools to help make tests for code using Cloud Storage.
See TESTING to read more about testing.
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 google-cloud
's CONTRIBUTING documentation and the shared documentation 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.