Skip to content

Commit 6a7ca65

Browse files
committed
Add snippets to Blob's javadoc and BlobSnippets class to gcloud-java-examples
1 parent 1c30bf9 commit 6a7ca65

File tree

2 files changed

+291
-10
lines changed
  • gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets
  • gcloud-java-storage/src/main/java/com/google/cloud/storage

2 files changed

+291
-10
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Blob's javadoc. Any change to this file should be reflected in Blob's
20+
* javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.storage.snippets;
24+
25+
import static java.nio.charset.StandardCharsets.UTF_8;
26+
27+
import com.google.cloud.AuthCredentials;
28+
import com.google.cloud.ReadChannel;
29+
import com.google.cloud.ServiceAccountSigner;
30+
import com.google.cloud.WriteChannel;
31+
import com.google.cloud.storage.Blob;
32+
import com.google.cloud.storage.Blob.BlobSourceOption;
33+
import com.google.cloud.storage.BlobId;
34+
import com.google.cloud.storage.CopyWriter;
35+
import com.google.cloud.storage.Storage.SignUrlOption;
36+
import com.google.cloud.storage.StorageException;
37+
38+
import java.io.FileInputStream;
39+
import java.io.IOException;
40+
import java.nio.ByteBuffer;
41+
import java.util.HashMap;
42+
import java.util.Map;
43+
import java.util.concurrent.TimeUnit;
44+
45+
/**
46+
* This class contains a number of snippets for the {@link Blob} class.
47+
*/
48+
public class BlobSnippets {
49+
50+
private final Blob blob;
51+
52+
public BlobSnippets(Blob blob) {
53+
this.blob = blob;
54+
}
55+
56+
/**
57+
* Example of checking if the blob exists.
58+
*/
59+
public void exists() {
60+
boolean exists = blob.exists();
61+
if (exists) {
62+
// the blob exists
63+
} else {
64+
// the blob was not found
65+
}
66+
}
67+
68+
/**
69+
* Example of reading all bytes of the blob, if its generation matches the
70+
* {@link Blob#generation()} value, otherwise a {@link StorageException} is thrown.
71+
*/
72+
public void content() {
73+
byte[] content = blob.content(BlobSourceOption.generationMatch());
74+
}
75+
76+
/**
77+
* Example of getting the blob's latest information, if its generation does not match the
78+
* {@link Blob#generation()} value, otherwise a {@link StorageException} is thrown.
79+
*/
80+
public void reload() {
81+
Blob latestBlob = blob.reload(BlobSourceOption.generationNotMatch());
82+
if (latestBlob == null) {
83+
// the blob was not found
84+
}
85+
}
86+
87+
/**
88+
* Example of replacing blob's metadata.
89+
*/
90+
public void update() {
91+
Map<String, String> newMetadata = new HashMap<>();
92+
newMetadata.put("key", "value");
93+
blob.toBuilder().metadata(null).build().update();
94+
blob.toBuilder().metadata(newMetadata).build().update();
95+
}
96+
97+
/**
98+
* Example of deleting the blob, if its generation matches the {@link Blob#generation()} value,
99+
* otherwise a {@link StorageException} is thrown.
100+
*/
101+
public void delete() {
102+
boolean deleted = blob.delete(BlobSourceOption.generationMatch());
103+
if (deleted) {
104+
// the blob was deleted
105+
} else {
106+
// the blob was not found
107+
}
108+
}
109+
110+
/**
111+
* Example of copying the blob to a different bucket with a different name.
112+
*/
113+
public void copyToId() {
114+
CopyWriter copyWriter = blob.copyTo(BlobId.of("my_other_unique_bucket", "copy_blob_name"));
115+
Blob copiedBlob = copyWriter.result();
116+
}
117+
118+
/**
119+
* Example of copying the blob to a different bucket, keeping the original name.
120+
*/
121+
public void copyToBucket() {
122+
CopyWriter copyWriter = blob.copyTo("my_other_unique_bucket");
123+
Blob copiedBlob = copyWriter.result();
124+
}
125+
126+
/**
127+
* Example of copying the blob to a different bucket with a different name.
128+
*/
129+
public void copyToStrings() {
130+
CopyWriter copyWriter = blob.copyTo("my_other_unique_bucket", "copy_blob_name");
131+
Blob copiedBlob = copyWriter.result();
132+
}
133+
134+
/**
135+
* Example of reading the blob's content through a reader.
136+
*/
137+
public void reader() throws IOException {
138+
try (ReadChannel reader = blob.reader()) {
139+
ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
140+
while (reader.read(bytes) > 0) {
141+
bytes.flip();
142+
// do something with bytes
143+
bytes.clear();
144+
}
145+
}
146+
}
147+
148+
/**
149+
* Example of writing the blob's content through a writer.
150+
*/
151+
public void writer() throws IOException {
152+
byte[] content = "Hello, World!".getBytes(UTF_8);
153+
try (WriteChannel writer = blob.writer()) {
154+
try {
155+
writer.write(ByteBuffer.wrap(content, 0, content.length));
156+
} catch (Exception ex) {
157+
// handle exception
158+
}
159+
}
160+
}
161+
162+
/**
163+
* Example of creating a signed URL for the blob that is valid for 2 weeks, using the default
164+
* credentials for signing the URL.
165+
*/
166+
public void signUrl() {
167+
blob.signUrl(14, TimeUnit.DAYS);
168+
}
169+
170+
/**
171+
* Example of creating a signed URL for the blob passing the
172+
* {@link SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used to sign the URL.
173+
*/
174+
public void signUrlWithSigner() throws IOException {
175+
blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.signWith(
176+
AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));
177+
}
178+
}

gcloud-java-storage/src/main/java/com/google/cloud/storage/Blob.java

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,17 @@ public Blob build() {
322322
/**
323323
* Checks if this blob exists.
324324
*
325+
* <p>Example of checking if the blob exists (see {@code exists()} in
326+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
327+
* BlobSnippets.java</a>):
328+
* <pre> {@code
329+
* boolean exists = blob.exists();
330+
* if (exists) {
331+
* // the blob exists
332+
* } else {
333+
* // the blob was not found
334+
* }}</pre>
335+
*
325336
* @param options blob read options
326337
* @return true if this blob exists, false otherwise
327338
* @throws StorageException upon failure
@@ -336,16 +347,36 @@ public boolean exists(BlobSourceOption... options) {
336347
/**
337348
* Returns this blob's content.
338349
*
350+
* <p>Example of reading all bytes of the blob, if its generation matches the
351+
* {@link Blob#generation()} value, otherwise a {@link StorageException} is thrown (see
352+
* {@code content()} in
353+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
354+
* BlobSnippets.java</a>):
355+
* <pre> {@code
356+
* byte[] content = blob.content(BlobSourceOption.generationMatch());
357+
* }</pre>
358+
*
339359
* @param options blob read options
340360
* @throws StorageException upon failure
341361
*/
342-
public byte[] content(Storage.BlobSourceOption... options) {
343-
return storage.readAllBytes(blobId(), options);
362+
public byte[] content(BlobSourceOption... options) {
363+
return storage.readAllBytes(blobId(), toSourceOptions(this, options));
344364
}
345365

346366
/**
347367
* Fetches current blob's latest information. Returns {@code null} if the blob does not exist.
348368
*
369+
* <p>Example of getting the blob's latest information, if its generation does not match the
370+
* {@link Blob#generation()} value, otherwise a {@link StorageException} is thrown (see
371+
* {@code reload()} in
372+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
373+
* BlobSnippets.java</a>):
374+
* <pre> {@code
375+
* Blob latestBlob = blob.reload(BlobSourceOption.generationNotMatch());
376+
* if (latestBlob == null) {
377+
* // the blob was not found
378+
* }}</pre>
379+
*
349380
* @param options blob read options
350381
* @return a {@code Blob} object with latest information or {@code null} if not found
351382
* @throws StorageException upon failure
@@ -367,12 +398,15 @@ public Blob reload(BlobSourceOption... options) {
367398
* {@code blob}'s metadata to {@code null}.
368399
* </p>
369400
*
370-
* <p>Example usage of replacing blob's metadata:
401+
* <p>Example of replacing blob's metadata (see {@code update()} in
402+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
403+
* BlobSnippets.java</a>):
371404
* <pre> {@code
405+
* Map<String, String> newMetadata = new HashMap<>();
406+
* newMetadata.put("key", "value");
372407
* blob.toBuilder().metadata(null).build().update();
373408
* blob.toBuilder().metadata(newMetadata).build().update();
374-
* }
375-
* </pre>
409+
* }</pre>
376410
*
377411
* @param options update options
378412
* @return a {@code Blob} object with updated information
@@ -385,6 +419,18 @@ public Blob update(BlobTargetOption... options) {
385419
/**
386420
* Deletes this blob.
387421
*
422+
* <p>Example of deleting the blob, if its generation matches the {@link Blob#generation()} value,
423+
* otherwise a {@link StorageException} is thrown (see {@code delete()} in
424+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
425+
* BlobSnippets.java</a>):
426+
* <pre> {@code
427+
* boolean deleted = blob.delete(BlobSourceOption.generationMatch());
428+
* if (deleted) {
429+
* // the blob was deleted
430+
* } else {
431+
* // the blob was not found
432+
* }}</pre>
433+
*
388434
* @param options blob delete options
389435
* @return {@code true} if blob was deleted, {@code false} if it was not found
390436
* @throws StorageException upon failure
@@ -397,6 +443,15 @@ public boolean delete(BlobSourceOption... options) {
397443
* Sends a copy request for the current blob to the target blob. Possibly also some of the
398444
* metadata are copied (e.g. content-type).
399445
*
446+
* <p>Example of copying the blob to a different bucket with a different name (see
447+
* {@code copyToId()} in
448+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
449+
* BlobSnippets.java</a>):
450+
* <pre> {@code
451+
* CopyWriter copyWriter = blob.copyTo(BlobId.of("my_other_unique_bucket", "copy_blob_name"));
452+
* Blob copiedBlob = copyWriter.result();
453+
* }</pre>
454+
*
400455
* @param targetBlob target blob's id
401456
* @param options source blob options
402457
* @return a {@link CopyWriter} object that can be used to get information on the newly created
@@ -416,6 +471,15 @@ public CopyWriter copyTo(BlobId targetBlob, BlobSourceOption... options) {
416471
* Sends a copy request for the current blob to the target bucket, preserving its name. Possibly
417472
* copying also some of the metadata (e.g. content-type).
418473
*
474+
* <p>Example of copying the blob to a different bucket, keeping the original name (see
475+
* {@code copyToBucket()} in
476+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
477+
* BlobSnippets.java</a>):
478+
* <pre> {@code
479+
* CopyWriter copyWriter = blob.copyTo("my_other_unique_bucket");
480+
* Blob copiedBlob = copyWriter.result();
481+
* }</pre>
482+
*
419483
* @param targetBucket target bucket's name
420484
* @param options source blob options
421485
* @return a {@link CopyWriter} object that can be used to get information on the newly created
@@ -430,6 +494,15 @@ public CopyWriter copyTo(String targetBucket, BlobSourceOption... options) {
430494
* Sends a copy request for the current blob to the target blob. Possibly also some of the
431495
* metadata are copied (e.g. content-type).
432496
*
497+
* <p>Example of copying the blob to a different bucket with a different name (see
498+
* {@code copyToStrings()} in
499+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
500+
* BlobSnippets.java</a>):
501+
* <pre> {@code
502+
* CopyWriter copyWriter = blob.copyTo("my_other_unique_bucket");
503+
* Blob copiedBlob = copyWriter.result();
504+
* }</pre>
505+
*
433506
* @param targetBucket target bucket's name
434507
* @param targetBlob target blob's name
435508
* @param options source blob options
@@ -444,6 +517,19 @@ public CopyWriter copyTo(String targetBucket, String targetBlob, BlobSourceOptio
444517
/**
445518
* Returns a {@code ReadChannel} object for reading this blob's content.
446519
*
520+
* <p>Example of reading the blob's content through a reader (see {@code reader()} in
521+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
522+
* BlobSnippets.java</a>):
523+
* <pre> {@code
524+
* try (ReadChannel reader = blob.reader()) {
525+
* ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
526+
* while (reader.read(bytes) > 0) {
527+
* bytes.flip();
528+
* // do something with bytes
529+
* bytes.clear();
530+
* }
531+
* }}</pre>
532+
*
447533
* @param options blob read options
448534
* @throws StorageException upon failure
449535
*/
@@ -456,6 +542,19 @@ public ReadChannel reader(BlobSourceOption... options) {
456542
* crc32c values in the current blob are ignored unless requested via the
457543
* {@code BlobWriteOption.md5Match} and {@code BlobWriteOption.crc32cMatch} options.
458544
*
545+
* <p>Example of writing the blob's content through a writer (see {@code writer()} in
546+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
547+
* BlobSnippets.java</a>):
548+
* <pre> {@code
549+
* byte[] content = "Hello, World!".getBytes(UTF_8);
550+
* try (WriteChannel writer = blob.writer()) {
551+
* try {
552+
* writer.write(ByteBuffer.wrap(content, 0, content.length));
553+
* } catch (Exception ex) {
554+
* // handle exception
555+
* }
556+
* }}</pre>
557+
*
459558
* @param options target blob options
460559
* @throws StorageException upon failure
461560
*/
@@ -485,15 +584,19 @@ public WriteChannel writer(BlobWriteOption... options) {
485584
* <li>The default credentials, if no credentials were passed to {@link StorageOptions}
486585
* </ol>
487586
*
488-
* <p>Example usage of creating a signed URL that is valid for 2 weeks, using the default
489-
* credentials for signing the URL:
587+
* <p>Example of creating a signed URL for the blob that is valid for 2 weeks, using the default
588+
* credentials for signing the URL (see {@code signUrl()} in
589+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
590+
* BlobSnippets.java</a>):
490591
* <pre> {@code
491592
* blob.signUrl(14, TimeUnit.DAYS);
492593
* }</pre>
493594
*
494-
* <p>Example usage of creating a signed URL passing the
495-
* {@link SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used for signing the
496-
* URL:
595+
* <p>Example of creating a signed URL for the blob passing the
596+
* {@link SignUrlOption#signWith(ServiceAccountSigner)} option, that will be used to sign the URL
597+
* (see {@code signUrlWithSigner()} in
598+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java">
599+
* BlobSnippets.java</a>):
497600
* <pre> {@code
498601
* blob.signUrl(14, TimeUnit.DAYS, SignUrlOption.signWith(
499602
* AuthCredentials.createForJson(new FileInputStream("/path/to/key.json"))));

0 commit comments

Comments
 (0)