Skip to content

Commit 58b60e5

Browse files
author
Ajay Kannan
committed
Make functional objects subclasses of metadata objects.
1 parent d3abf25 commit 58b60e5

File tree

18 files changed

+1203
-908
lines changed

18 files changed

+1203
-908
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ Here is a code snippet showing a simple usage example from within Compute/App En
248248
import static java.nio.charset.StandardCharsets.UTF_8;
249249
250250
import com.google.gcloud.storage.Blob;
251+
import com.google.gcloud.storage.BlobInfo;
251252
import com.google.gcloud.storage.BlobId;
252253
import com.google.gcloud.storage.Storage;
253254
import com.google.gcloud.storage.StorageOptions;
@@ -257,7 +258,7 @@ import java.nio.channels.WritableByteChannel;
257258
258259
Storage storage = StorageOptions.defaultInstance().service();
259260
BlobId blobId = BlobId.of("bucket", "blob_name");
260-
Blob blob = Blob.get(storage, blobId);
261+
Blob blob = storage.get(storage, blobId);
261262
if (blob == null) {
262263
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
263264
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));

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

+18-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.gcloud.storage.BlobId;
2626
import com.google.gcloud.storage.BlobInfo;
2727
import com.google.gcloud.storage.Bucket;
28-
import com.google.gcloud.storage.BucketInfo;
2928
import com.google.gcloud.storage.CopyWriter;
3029
import com.google.gcloud.storage.Storage;
3130
import com.google.gcloud.storage.Storage.ComposeRequest;
@@ -138,22 +137,22 @@ public void run(Storage storage, BlobId... blobIds) {
138137
System.out.println("No such bucket");
139138
return;
140139
}
141-
System.out.println("Bucket info: " + bucket.info());
140+
System.out.println("Bucket info: " + bucket);
142141
} else {
143142
// get Blob
144-
Blob blob = Blob.get(storage, blobIds[0]);
143+
Blob blob = storage.get(blobIds[0]);
145144
if (blob == null) {
146145
System.out.println("No such object");
147146
return;
148147
}
149-
System.out.println("Blob info: " + blob.info());
148+
System.out.println("Blob info: " + blob);
150149
}
151150
} else {
152151
// use batch to get multiple blobs.
153-
List<Blob> blobs = Blob.get(storage, Arrays.asList(blobIds));
152+
List<Blob> blobs = storage.get(blobIds);
154153
for (Blob blob : blobs) {
155154
if (blob != null) {
156-
System.out.println(blob.info());
155+
System.out.println(blob);
157156
}
158157
}
159158
}
@@ -184,7 +183,7 @@ private static class DeleteAction extends BlobsAction {
184183
@Override
185184
public void run(Storage storage, BlobId... blobIds) {
186185
// use batch operation
187-
List<Boolean> deleteResults = Blob.delete(storage, blobIds);
186+
List<Boolean> deleteResults = storage.delete(blobIds);
188187
int index = 0;
189188
for (Boolean deleted : deleteResults) {
190189
if (deleted) {
@@ -218,9 +217,9 @@ String parse(String... args) {
218217
public void run(Storage storage, String bucketName) {
219218
if (bucketName == null) {
220219
// list buckets
221-
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
222-
while (bucketInfoIterator.hasNext()) {
223-
System.out.println(bucketInfoIterator.next());
220+
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
221+
while (bucketIterator.hasNext()) {
222+
System.out.println(bucketIterator.next());
224223
}
225224
} else {
226225
// list a bucket's blobs
@@ -231,7 +230,7 @@ public void run(Storage storage, String bucketName) {
231230
}
232231
Iterator<Blob> blobIterator = bucket.list().iterateAll();
233232
while (blobIterator.hasNext()) {
234-
System.out.println(blobIterator.next().info());
233+
System.out.println(blobIterator.next());
235234
}
236235
}
237236
}
@@ -257,8 +256,7 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
257256
if (Files.size(uploadFrom) > 1_000_000) {
258257
// When content is not available or large (1MB or more) it is recommended
259258
// to write it in chunks via the blob's channel writer.
260-
Blob blob = new Blob(storage, blobInfo);
261-
try (WriteChannel writer = blob.writer()) {
259+
try (WriteChannel writer = storage.writer(blobInfo)) {
262260
byte[] buffer = new byte[1024];
263261
try (InputStream input = Files.newInputStream(uploadFrom)) {
264262
int limit;
@@ -311,7 +309,7 @@ public void run(Storage storage, Tuple<BlobId, Path> tuple) throws IOException {
311309
}
312310

313311
private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOException {
314-
Blob blob = Blob.get(storage, blobId);
312+
Blob blob = storage.get(blobId);
315313
if (blob == null) {
316314
System.out.println("No such object");
317315
return;
@@ -320,7 +318,7 @@ private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOExcep
320318
if (downloadTo != null) {
321319
writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile()));
322320
}
323-
if (blob.info().size() < 1_000_000) {
321+
if (blob.size() < 1_000_000) {
324322
// Blob is small read all its content in one request
325323
byte[] content = blob.content();
326324
writeTo.write(content);
@@ -438,13 +436,13 @@ public void run(Storage storage, Tuple<BlobId, Map<String, String>> tuple)
438436
}
439437

440438
private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
441-
Blob blob = Blob.get(storage, blobId);
439+
Blob blob = storage.get(blobId);
442440
if (blob == null) {
443441
System.out.println("No such object");
444442
return;
445443
}
446-
Blob updateBlob = blob.update(blob.info().toBuilder().metadata(metadata).build());
447-
System.out.println("Updated " + updateBlob.info());
444+
Blob updateBlob = blob.toBuilder().metadata(metadata).build().update();
445+
System.out.println("Updated " + updateBlob);
448446
}
449447

450448
@Override
@@ -488,9 +486,8 @@ public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo>
488486
run(storage, tuple.x(), tuple.y());
489487
}
490488

491-
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo)
492-
throws IOException {
493-
Blob blob = new Blob(storage, blobInfo);
489+
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo) {
490+
Blob blob = storage.get(blobInfo.blobId());
494491
System.out.println("Signed URL: "
495492
+ blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.serviceAccount(cred)));
496493
}

gcloud-java-storage/README.md

+17-13
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ Storage storage = StorageOptions.defaultInstance().service();
7777
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page.
7878

7979
#### 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.
80+
Stored objects are called "blobs" in `gcloud-java` 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.
8181

8282
Add the following imports at the top of your file:
8383

8484
```java
8585
import static java.nio.charset.StandardCharsets.UTF_8;
8686

87+
import com.google.gcloud.storage.Blob;
8788
import com.google.gcloud.storage.BlobId;
8889
import com.google.gcloud.storage.BlobInfo;
90+
import com.google.gcloud.storage.Bucket;
8991
import com.google.gcloud.storage.BucketInfo;
9092
```
9193

@@ -96,11 +98,11 @@ Then add the following code to create a bucket and upload a simple blob.
9698
```java
9799
// Create a bucket
98100
String bucketName = "my_unique_bucket"; // Change this to something unique
99-
BucketInfo bucketInfo = storage.create(BucketInfo.of(bucketName));
101+
Bucket bucket = storage.create(BucketInfo.of(bucketName));
100102

101103
// Upload a blob to the newly created bucket
102104
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
103-
BlobInfo blobInfo = storage.create(
105+
Blob blob = storage.create(
104106
BlobInfo.builder(blobId).contentType("text/plain").build(),
105107
"a simple blob".getBytes(UTF_8));
106108
```
@@ -125,14 +127,14 @@ Then add the following code to list all your buckets and all the blobs inside yo
125127

126128
```java
127129
// List all your buckets
128-
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
130+
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
129131
System.out.println("My buckets:");
130-
while (bucketInfoIterator.hasNext()) {
131-
System.out.println(bucketInfoIterator.next());
132+
while (bucketIterator.hasNext()) {
133+
System.out.println(bucketIterator.next());
132134
}
133135

134136
// List the blobs in a particular bucket
135-
Iterator<BlobInfo> blobIterator = storage.list(bucketName).iterateAll();
137+
Iterator<Blob> blobIterator = storage.list(bucketName).iterateAll();
136138
System.out.println("My blobs:");
137139
while (blobIterator.hasNext()) {
138140
System.out.println(blobIterator.next());
@@ -146,8 +148,10 @@ Here we put together all the code shown above into one program. This program as
146148
```java
147149
import static java.nio.charset.StandardCharsets.UTF_8;
148150

151+
import com.google.gcloud.storage.Blob;
149152
import com.google.gcloud.storage.BlobId;
150153
import com.google.gcloud.storage.BlobInfo;
154+
import com.google.gcloud.storage.Bucket;
151155
import com.google.gcloud.storage.BucketInfo;
152156
import com.google.gcloud.storage.Storage;
153157
import com.google.gcloud.storage.StorageOptions;
@@ -163,26 +167,26 @@ public class GcloudStorageExample {
163167

164168
// Create a bucket
165169
String bucketName = "my_unique_bucket"; // Change this to something unique
166-
BucketInfo bucketInfo = storage.create(BucketInfo.of(bucketName));
170+
Bucket bucket = storage.create(BucketInfo.of(bucketName));
167171

168172
// Upload a blob to the newly created bucket
169173
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
170-
BlobInfo blobInfo = storage.create(
174+
Blob blob = storage.create(
171175
BlobInfo.builder(blobId).contentType("text/plain").build(),
172176
"a simple blob".getBytes(UTF_8));
173177

174178
// Retrieve a blob from the server
175179
String blobContent = new String(storage.readAllBytes(blobId), UTF_8);
176180

177181
// List all your buckets
178-
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
182+
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
179183
System.out.println("My buckets:");
180-
while (bucketInfoIterator.hasNext()) {
181-
System.out.println(bucketInfoIterator.next());
184+
while (bucketIterator.hasNext()) {
185+
System.out.println(bucketIterator.next());
182186
}
183187

184188
// List the blobs in a particular bucket
185-
Iterator<BlobInfo> blobIterator = storage.list(bucketName).iterateAll();
189+
Iterator<Blob> blobIterator = storage.list(bucketName).iterateAll();
186190
System.out.println("My blobs:");
187191
while (blobIterator.hasNext()) {
188192
System.out.println(blobIterator.next());

gcloud-java-storage/src/main/java/com/google/gcloud/storage/BatchResponse.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public final class BatchResponse implements Serializable {
3131
private static final long serialVersionUID = 1057416839397037706L;
3232

3333
private final List<Result<Boolean>> deleteResult;
34-
private final List<Result<BlobInfo>> updateResult;
35-
private final List<Result<BlobInfo>> getResult;
34+
private final List<Result<Blob>> updateResult;
35+
private final List<Result<Blob>> getResult;
3636

3737
public static class Result<T extends Serializable> implements Serializable {
3838

@@ -113,8 +113,8 @@ static <T extends Serializable> Result<T> empty() {
113113
}
114114
}
115115

116-
BatchResponse(List<Result<Boolean>> deleteResult, List<Result<BlobInfo>> updateResult,
117-
List<Result<BlobInfo>> getResult) {
116+
BatchResponse(List<Result<Boolean>> deleteResult, List<Result<Blob>> updateResult,
117+
List<Result<Blob>> getResult) {
118118
this.deleteResult = ImmutableList.copyOf(deleteResult);
119119
this.updateResult = ImmutableList.copyOf(updateResult);
120120
this.getResult = ImmutableList.copyOf(getResult);
@@ -146,14 +146,14 @@ public List<Result<Boolean>> deletes() {
146146
/**
147147
* Returns the results for the update operations using the request order.
148148
*/
149-
public List<Result<BlobInfo>> updates() {
149+
public List<Result<Blob>> updates() {
150150
return updateResult;
151151
}
152152

153153
/**
154154
* Returns the results for the get operations using the request order.
155155
*/
156-
public List<Result<BlobInfo>> gets() {
156+
public List<Result<Blob>> gets() {
157157
return getResult;
158158
}
159159
}

0 commit comments

Comments
 (0)