Skip to content

Commit 14d5bb1

Browse files
committed
Update storage examples, add storage section to readme
1 parent e3dba5e commit 14d5bb1

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Java idiomatic client for [Google Cloud Platform][cloud-platform] services.
1414
This client supports the following Google Cloud Platform services:
1515

1616
- [Google Cloud Datastore] (#google-cloud-datastore)
17+
- [Google Cloud Storage] (#google-cloud-storage)
1718

1819
<!---
1920
- [Google Cloud Storage] (https://cloud.google.com/storage/)
@@ -86,6 +87,45 @@ if (entity == null) {
8687
}
8788
```
8889

90+
Google Cloud Storage
91+
----------------------
92+
93+
Google [Cloud Storage][cloud-storage] is a durable and highly available
94+
object storage service. Google Cloud Storage is almost infinitely scalable
95+
and guarantees consistency: when a write succeeds, the latest copy of the
96+
object will be returned to any GET, globally.
97+
98+
See the [Google Cloud Storage docs][cloud-storage-activation] for more details on how to activate
99+
Cloud Storage for your project.
100+
101+
See the ``gcloud-java`` API [storage documentation][storage-api] to learn how to interact
102+
with the Cloud Storage using this Client Library.
103+
104+
```java
105+
import com.google.gcloud.storage.Blob;
106+
import com.google.gcloud.storage.Storage;
107+
import com.google.gcloud.storage.StorageFactory;
108+
import com.google.gcloud.storage.StorageOptions;
109+
import java.nio.ByteBuffer;
110+
import java.nio.channels.WritableByteChannel;
111+
112+
StorageOptions options = StorageOptions.builder().projectId(PROJECT_ID).build();
113+
Storage storage = StorageFactory.instance().get(options);
114+
byte[] content = readContent();
115+
Blob blob = new Blob(storage, "bucket", "blob_name");
116+
if (!blob.exists()) {
117+
storage.create(blob.info(), content);
118+
} else {
119+
System.out.println("Updating content for " + blob.info().name());
120+
byte[] prevContent = blob.content();
121+
content = mergeContent(prevContent, content);
122+
WritableByteChannel channel = blob.writer();
123+
channel.write(ByteBuffer.wrap(content));
124+
channel.close();
125+
}
126+
}
127+
```
128+
89129
Contributing
90130
------------
91131

@@ -130,3 +170,5 @@ Apache 2.0 - See [LICENSE] for more information.
130170
[cloud-storage]: https://cloud.google.com/storage/
131171
[cloud-storage-docs]: https://cloud.google.com/storage/docs/overview
132172
[cloud-storage-create-bucket]: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
173+
[cloud-storage-activation]: https://cloud.google.com/storage/docs/signup
174+
[storage-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/storage/package-summary.html

gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java

+24-18
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import com.google.gcloud.spi.StorageRpc.Tuple;
2323
import com.google.gcloud.storage.BatchRequest;
2424
import com.google.gcloud.storage.BatchResponse;
25+
import com.google.gcloud.storage.Blob;
2526
import com.google.gcloud.storage.BlobInfo;
2627
import com.google.gcloud.storage.BlobReadChannel;
2728
import com.google.gcloud.storage.BlobWriteChannel;
29+
import com.google.gcloud.storage.Bucket;
2830
import com.google.gcloud.storage.BucketInfo;
2931
import com.google.gcloud.storage.Storage;
3032
import com.google.gcloud.storage.Storage.ComposeRequest;
@@ -142,12 +144,12 @@ public void run(Storage storage, BlobInfo... blobInfos) {
142144
if (blobInfos.length == 1) {
143145
if (blobInfos[0].name().isEmpty()) {
144146
// get Bucket
145-
BucketInfo bucketInfo = storage.get(blobInfos[0].bucket());
146-
System.out.println("Bucket info: " + bucketInfo);
147+
Bucket bucket = new Bucket(storage, blobInfos[0].bucket());
148+
System.out.println("Bucket info: " + bucket.reload().info());
147149
} else {
148150
// get Blob
149-
BlobInfo blobInfo = storage.get(blobInfos[0].bucket(), blobInfos[0].name());
150-
System.out.println("Blob info: " + blobInfo);
151+
Blob blob = new Blob(storage, blobInfos[0]);
152+
System.out.println("Blob info: " + blob.reload().info());
151153
}
152154
} else {
153155
// use batch to get multiple blobs.
@@ -187,7 +189,7 @@ private static class DeleteAction extends BlobsAction {
187189
@Override
188190
public void run(Storage storage, BlobInfo... blobInfos) {
189191
if (blobInfos.length == 1) {
190-
boolean wasDeleted = storage.delete(blobInfos[0].bucket(), blobInfos[0].name());
192+
boolean wasDeleted = new Blob(storage, blobInfos[0]).delete();
191193
if (wasDeleted) {
192194
System.out.println("Blob " + blobInfos[0] + " was deleted");
193195
}
@@ -237,8 +239,9 @@ public void run(Storage storage, String bucket) {
237239
}
238240
} else {
239241
// list a bucket's blobs
240-
for (BlobInfo b : storage.list(bucket)) {
241-
System.out.println(b);
242+
Bucket functionalBucket = new Bucket(storage, bucket);
243+
for (Blob b : functionalBucket.list()) {
244+
System.out.println(b.info());
242245
}
243246
}
244247
}
@@ -264,7 +267,8 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
264267
if (Files.size(uploadFrom) > 1_000_000) {
265268
// When content is not available or large (1MB or more) it is recommended
266269
// to write it in chunks via the blob's channel writer.
267-
try (BlobWriteChannel writer = storage.writer(blobInfo)) {
270+
Blob blob = new Blob(storage, blobInfo);
271+
try (BlobWriteChannel writer = blob.writer()) {
268272
byte[] buffer = new byte[1024];
269273
try (InputStream input = Files.newInputStream(uploadFrom)) {
270274
int limit;
@@ -318,8 +322,9 @@ public void run(Storage storage, Tuple<BlobInfo, Path> tuple) throws IOException
318322

319323
private void run(Storage storage, String bucket, String blobName, Path downloadTo)
320324
throws IOException {
325+
Blob blob = new Blob(storage, bucket, blobName);
321326
BlobInfo blobInfo = storage.get(bucket, blobName);
322-
if (blobInfo == null) {
327+
if (!blob.exists()) {
323328
System.out.println("No such object");
324329
return;
325330
}
@@ -329,11 +334,11 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT
329334
}
330335
if (blobInfo.size() < 1_000_000) {
331336
// Blob is small read all its content in one request
332-
byte[] content = storage.readAllBytes(blobInfo.bucket(), blobInfo.name());
337+
byte[] content = blob.content();
333338
writeTo.write(content);
334339
} else {
335340
// When Blob size is big or unknown use the blob's channel reader.
336-
try (BlobReadChannel reader = storage.reader(blobInfo.bucket(), blobInfo.name())) {
341+
try (BlobReadChannel reader = blob.reader()) {
337342
WritableByteChannel channel = Channels.newChannel(writeTo);
338343
ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
339344
while (reader.read(bytes) > 0) {
@@ -435,7 +440,8 @@ public String params() {
435440
*
436441
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">Objects: update</a>
437442
*/
438-
private static class UpdateMetadataAction extends StorageAction<Tuple<BlobInfo, Map<String, String>>> {
443+
private static class UpdateMetadataAction extends
444+
StorageAction<Tuple<BlobInfo, Map<String, String>>> {
439445

440446
@Override
441447
public void run(Storage storage, Tuple<BlobInfo, Map<String, String>> tuple)
@@ -445,13 +451,13 @@ public void run(Storage storage, Tuple<BlobInfo, Map<String, String>> tuple)
445451

446452
private void run(Storage storage, String bucket, String blobName,
447453
Map<String, String> metadata) {
448-
BlobInfo blobInfo = storage.get(bucket, blobName);
449-
if (blobInfo == null) {
454+
Blob blob = new Blob(storage, bucket, blobName).reload();
455+
if (!blob.exists()) {
450456
System.out.println("No such object");
451457
return;
452458
}
453-
blobInfo = storage.update(blobInfo.toBuilder().metadata(metadata).build());
454-
System.out.println("Updated " + blobInfo);
459+
Blob updateBlob = blob.update(blob.info().toBuilder().metadata(metadata).build());
460+
System.out.println("Updated " + updateBlob.info());
455461
}
456462

457463
@Override
@@ -487,7 +493,7 @@ public String params() {
487493
private static class SignUrlAction extends
488494
StorageAction<Tuple<ServiceAccountAuthCredentials, BlobInfo>> {
489495

490-
private static final char[] PASSWORD = "notasecret".toCharArray();
496+
private static final char[] PASSWORD = "notasecret".toCharArray();
491497

492498
@Override
493499
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo> tuple)
@@ -501,7 +507,7 @@ private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo b
501507
cal.add(Calendar.DATE, 1);
502508
long expiration = cal.getTimeInMillis() / 1000;
503509
System.out.println("Signed URL: " +
504-
storage.signUrl(blobInfo, expiration, SignUrlOption.serviceAccount(cred)));
510+
new Blob(storage, blobInfo).signUrl(expiration, SignUrlOption.serviceAccount(cred)));
505511
}
506512

507513
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/storage/package-info.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
* StorageOptions options = StorageOptions.builder().projectId("project").build();
2323
* Storage storage = StorageFactory.instance().get(options);
2424
* byte[] content = readContent();
25-
* BlobInfo blobInfo = storage.get("bucket", "blob_name");
26-
* if (blobInfo == null) {
27-
* storage.create(BlobInfo.of("bucket", "blob_name"), content);
25+
* Blob blob = new Blob(storage, "bucket", "blob_name");
26+
* if (!blob.exists()) {
27+
* storage.create(blob.info(), content);
2828
* } else {
29-
* byte[] prevContent = storage.readAllBytes("bucket", "blob_name");
29+
* byte[] prevContent = blob.content();
3030
* content = mergeContent(prevContent, content);
31-
* WritableByteChannel channel = storage.writer(blob);
31+
* WritableByteChannel channel = blob.writer();
3232
* channel.write(ByteBuffer.wrap(content));
3333
* channel.close();
3434
* }}</pre>

0 commit comments

Comments
 (0)