Skip to content

Commit 08c0f28

Browse files
author
Ajay Kannan
committed
Add step-by-step guide for storage README
1 parent b48d163 commit 08c0f28

1 file changed

Lines changed: 147 additions & 16 deletions

File tree

gcloud-java-storage/README.md

Lines changed: 147 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,160 @@ Cloud Storage for your project.
5656
See the ``gcloud-java`` API [storage documentation][storage-api] to learn how to interact
5757
with the Cloud Storage using this Client Library.
5858

59-
Here is a code snippet showing a simple usage example from within Compute/App Engine. Note that you must [supply credentials](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) and a project ID if running this snippet elsewhere.
59+
Getting Started
60+
---------------
61+
#### Prerequisites
62+
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]`.
63+
64+
#### Installation and setup
65+
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.
66+
67+
#### Creating an authorized service object
68+
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:
69+
70+
```java
71+
import com.google.gcloud.storage.Storage;
72+
import com.google.gcloud.storage.StorageOptions;
73+
74+
Storage storage = StorageOptions.defaultInstance().service();
75+
```
76+
77+
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page.
78+
79+
#### Storing data
80+
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.
81+
82+
Add the following imports at the top of your file:
6083

6184
```java
6285
import static java.nio.charset.StandardCharsets.UTF_8;
6386

87+
import com.google.gcloud.storage.BlobId;
88+
import com.google.gcloud.storage.BlobInfo;
89+
import com.google.gcloud.storage.BucketInfo;
90+
```
91+
92+
Then add the following code to create a bucket and upload a simple blob.
93+
94+
*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.*
95+
96+
```java
97+
// Create a bucket
98+
String bucketName = "my_unique_bucket"; // Remember to change this to something unique
99+
BucketInfo bucketInfo = storage.create(BucketInfo.builder(bucketName).build());
100+
101+
// Upload a blob to the newly created bucket
102+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
103+
BlobInfo blobInfo = storage.create(BlobInfo.builder(blobId).build(),
104+
"a simple blob".getBytes(UTF_8));
105+
```
106+
107+
At this point, you will be able to see your newly created bucket and blob on the Google Developers Console.
108+
109+
#### Retrieving data
110+
Now that we have content uploaded to the server, we can see how to read data from the server. Add the following import:
111+
112+
```java
64113
import com.google.gcloud.storage.Blob;
65-
import com.google.gcloud.storage.Storage;
66-
import com.google.gcloud.storage.StorageOptions;
114+
```
67115

68-
import java.nio.ByteBuffer;
69-
import java.nio.channels.WritableByteChannel;
116+
Then add the following lines to your program to get back the blob we uploaded.
70117

71-
Storage storage = StorageOptions.defaultInstance().service();
72-
Blob blob = new Blob(storage, "bucket", "blob_name");
73-
if (!blob.exists()) {
74-
storage2.create(blob.info(), "Hello, Cloud Storage!".getBytes(UTF_8));
75-
} else {
76-
System.out.println("Updating content for " + blob.info().name());
77-
byte[] prevContent = blob.content();
78-
System.out.println(new String(prevContent, UTF_8));
79-
WritableByteChannel channel = blob.writer();
80-
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
81-
channel.close();
118+
```java
119+
Blob blob = Blob.load(storage, blobInfo.blobId());
120+
String blobContent = new String(blob.content(), UTF_8);
121+
```
122+
123+
If others have permission to edit the blob, then you may want to call `reload` to get a more up to date copy of the blob later in your program. The following snippet shows how to get a new Blob object containing updated information.
124+
125+
```
126+
Blob refreshedBlob = blob.reload();
127+
```
128+
129+
#### Listing buckets and contents of buckets
130+
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:
131+
132+
```java
133+
import com.google.gcloud.storage.Bucket;
134+
135+
import java.util.Iterator;
136+
```
137+
138+
Then add the following code to list all your buckets and all the blobs inside your newly created bucket.
139+
140+
```java
141+
// List all your buckets
142+
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
143+
System.out.println("My buckets:");
144+
while (bucketInfoIterator.hasNext()) {
145+
System.out.println(bucketInfoIterator.next());
146+
}
147+
148+
// List the blobs in a particular bucket
149+
Bucket bucket = Bucket.load(storage, bucketName);
150+
Iterator<Blob> blobIterator = bucket.list().iterateAll();
151+
System.out.println("My blobs:");
152+
while (blobIterator.hasNext()) {
153+
System.out.println(blobIterator.next().info());
154+
}
155+
```
156+
157+
#### Complete source code
158+
159+
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.
160+
161+
```java
162+
import static java.nio.charset.StandardCharsets.UTF_8;
163+
164+
import com.google.gcloud.storage.Storage;
165+
import com.google.gcloud.storage.StorageOptions;
166+
import com.google.gcloud.storage.Blob;
167+
import com.google.gcloud.storage.BlobId;
168+
import com.google.gcloud.storage.BlobInfo;
169+
import com.google.gcloud.storage.Bucket;
170+
import com.google.gcloud.storage.BucketInfo;
171+
172+
import java.util.Iterator;
173+
174+
public class GcloudStorageExample {
175+
176+
public static void main(String[] args) {
177+
// Create a service object
178+
// Credentials are inferred from the environment.
179+
Storage storage = StorageOptions.defaultInstance().service();
180+
181+
// Create a bucket
182+
String bucketName = "my_unique_bucket-1323252"; // Remember to change this to something unique
183+
BucketInfo bucketInfo = storage.create(BucketInfo.builder(bucketName).build());
184+
185+
// Upload a blob to the newly created bucket
186+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
187+
BlobInfo blobInfo = storage.create(BlobInfo.builder(blobId).build(),
188+
"a simple blob".getBytes(UTF_8));
189+
190+
// Retrieve a blob from the server
191+
Blob blob = Blob.load(storage, blobInfo.blobId());
192+
String blobContent = new String(blob.content(), UTF_8);
193+
194+
// An example of how to get an updated blob
195+
// This is useful in cases where others with access to your blob may have changed it.
196+
Blob refreshedBlob = blob.reload();
197+
198+
// List all your buckets
199+
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
200+
System.out.println("My buckets:");
201+
while (bucketInfoIterator.hasNext()) {
202+
System.out.println(bucketInfoIterator.next());
203+
}
204+
205+
// List the blobs in a particular bucket
206+
Bucket bucket = Bucket.load(storage, bucketName);
207+
Iterator<Blob> blobIterator = bucket.list().iterateAll();
208+
System.out.println("My blobs:");
209+
while (blobIterator.hasNext()) {
210+
System.out.println(blobIterator.next().info());
211+
}
212+
}
82213
}
83214
```
84215

0 commit comments

Comments
 (0)