Skip to content

Commit 044ce7f

Browse files
---
yaml --- r: 8347 b: refs/heads/snehashah-snippets c: 768ec4a h: refs/heads/master i: 8345: 9c8e09e 8343: daffa25
1 parent 6ac9ebf commit 044ce7f

3 files changed

Lines changed: 34 additions & 63 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5656
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5757
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5858
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
59-
refs/heads/snehashah-snippets: 5517ae88b34c19572eaa9496b5b3e79b66d5087e
59+
refs/heads/snehashah-snippets: 768ec4a0009499f1d359b1ea6a7465be965e1478
6060
refs/tags/v0.20.2: 5a53aa06f268b74dc192204f4f83e1a04d40f65d
6161
refs/tags/v0.20.3: 269025fdc14af0b68df214a4518be5379b2fe569
6262
refs/tags/v0.21.0: f88b200e00e41ba6262ee88a92abba38b1e2417e

branches/snehashah-snippets/README.md

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Java idiomatic client for [Google Cloud Platform][cloud-platform] services.
1515
This client supports the following Google Cloud Platform services at a [GA](#versioning) quality level:
1616
- [Stackdriver Logging](#stackdriver-logging-ga) (GA)
1717
- [Cloud Datastore](#google-cloud-datastore-ga) (GA)
18-
- [Cloud Storage](#google-cloud-storage-ga) (GA)
18+
- [Cloud Storage](google-cloud-storage) (GA)
1919
- [Cloud Translation](#google-translation-ga) (GA)
2020

2121
This client supports the following Google Cloud Platform services at a [Beta](#versioning) quality level:
@@ -352,63 +352,8 @@ if (entity != null) {
352352
.build();
353353
datastore.update(entity);
354354
}
355-
```
356-
357-
Google Cloud Storage (GA)
358-
----------------------
359-
360-
- [API Documentation][storage-api]
361-
- [Official Documentation][cloud-storage-docs]
362-
363-
*Follow the [activation instructions][cloud-storage-activation] to use the Google Cloud Storage API with your project.*
364-
365-
#### Preview
366-
367-
Here are two code snippets showing simple usage examples from within Compute/App Engine. Note that you must [supply credentials](#authentication) and a project ID if running this snippet elsewhere.
368-
369-
The first snippet shows how to create a Storage blob. Complete source code can be found at
370-
[CreateBlob.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateBlob.java).
371-
372-
```java
373-
import static java.nio.charset.StandardCharsets.UTF_8;
374355
375-
import com.google.cloud.storage.Blob;
376-
import com.google.cloud.storage.BlobId;
377-
import com.google.cloud.storage.BlobInfo;
378-
import com.google.cloud.storage.Storage;
379-
import com.google.cloud.storage.StorageOptions;
380-
381-
Storage storage = StorageOptions.getDefaultInstance().getService();
382-
BlobId blobId = BlobId.of("bucket", "blob_name");
383-
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
384-
Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
385356
```
386-
The second snippet shows how to update a Storage blob if it exists. Complete source code can be
387-
found at
388-
[UpdateBlob.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/UpdateBlob.java).
389-
```java
390-
import static java.nio.charset.StandardCharsets.UTF_8;
391-
392-
import com.google.cloud.storage.Blob;
393-
import com.google.cloud.storage.BlobId;
394-
import com.google.cloud.storage.Storage;
395-
import com.google.cloud.storage.StorageOptions;
396-
397-
import java.nio.ByteBuffer;
398-
import java.nio.channels.WritableByteChannel;
399-
400-
Storage storage = StorageOptions.getDefaultInstance().getService();
401-
BlobId blobId = BlobId.of("bucket", "blob_name");
402-
Blob blob = storage.get(blobId);
403-
if (blob != null) {
404-
byte[] prevContent = blob.getContent();
405-
System.out.println(new String(prevContent, UTF_8));
406-
WritableByteChannel channel = blob.writer();
407-
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
408-
channel.close();
409-
}
410-
```
411-
412357
Google Translation (GA)
413358
----------------
414359

branches/snehashah-snippets/google-cloud-storage/README.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ import com.google.cloud.storage.BucketInfo;
9090

9191
Then add the following code to create a bucket and upload a simple blob.
9292

93-
*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).*
93+
*Important: Bucket names have to be globally unique (among all users of Cloud Storage). 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).*
9494

9595
```java
9696
// Create a bucket
@@ -99,19 +99,45 @@ Bucket bucket = storage.create(BucketInfo.of(bucketName));
9999

100100
// Upload a blob to the newly created bucket
101101
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
102-
Blob blob = bucket.create(
103-
"my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");
102+
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
103+
Blob blob = storage.create(blobInfo, "a simple blob".getBytes(UTF_8));
104104
```
105105

106+
A complete example for creating a blob can be found at
107+
[CreateBlob.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateBlob.java).
108+
106109
At this point, you will be able to see your newly created bucket and blob on the Google Developers Console.
107110

108111
#### Retrieving data
109112
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.
110113

111114
```java
112-
String blobContent = new String(blob.getContent(), UTF_8);
115+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
116+
byte[] content = storage.readAllBytes(blobId);
117+
String contentString = new String(content, UTF_8);
113118
```
114119

120+
A complete example for accessing blobs can be found at
121+
[CreateBlob.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateBlob.java).
122+
123+
#### Updating data
124+
Another thing we may want to do is update a blob. The following snippet shows how to update a Storage blob if it exists.
125+
126+
``` java
127+
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
128+
Blob blob = storage.get(blobId);
129+
if (blob != null) {
130+
byte[] prevContent = blob.getContent();
131+
System.out.println(new String(prevContent, UTF_8));
132+
WritableByteChannel channel = blob.writer();
133+
channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));
134+
channel.close();
135+
}
136+
```
137+
138+
The complete source code can be found at
139+
[UpdateBlob.java](./google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/UpdateBlob.java).
140+
115141
#### Listing buckets and contents of buckets
116142
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 code to list all your buckets and all the blobs inside each bucket.
117143

@@ -125,15 +151,15 @@ for (Bucket bucket : storage.list().iterateAll()) {
125151
System.out.println("Blobs in the bucket:");
126152
for (Blob blob : bucket.list().iterateAll()) {
127153
System.out.println(blob);
128-
}
154+
}
129155
}
130156
```
131157

132158
#### Complete source code
133159

134160
In
135161
[CreateAndListBucketsAndBlobs.java](../google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/CreateAndListBucketsAndBlobs.java)
136-
we put together all the code shown above into one program. The program assumes that you are
162+
we put together examples creating and listing buckets and blobs into one program. The program assumes that you are
137163
running on Compute Engine or from your own desktop. To run the example on App Engine, simply move
138164
the code from the main method to your application's servlet class and change the print statements to
139165
display on your webpage.

0 commit comments

Comments
 (0)