Skip to content

Commit 167197f

Browse files
committed
Update StorageExample actions to use functional Blob and Bucket
1 parent abc9a00 commit 167197f

1 file changed

Lines changed: 63 additions & 67 deletions

File tree

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

Lines changed: 63 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,21 @@ private static abstract class StorageAction<T> {
8888

8989
abstract void run(Storage storage, T request) throws Exception;
9090

91-
abstract T parse(String... args) throws Exception;
91+
abstract T parse(Storage storage, String... args) throws Exception;
9292

9393
protected String params() {
9494
return "";
9595
}
9696
}
9797

98-
private static abstract class BlobAction extends StorageAction<BlobInfo> {
98+
private static abstract class BlobAction extends StorageAction<Blob> {
9999

100100
@Override
101-
BlobInfo parse(String... args) {
101+
Blob parse(Storage storage, String... args) {
102102
if (args.length != 2) {
103103
throw new IllegalArgumentException();
104104
}
105-
return BlobInfo.of(args[0], args[1]);
105+
return new Blob(storage, args[0], args[1]);
106106
}
107107

108108
@Override
@@ -111,18 +111,18 @@ public String params() {
111111
}
112112
}
113113

114-
private static abstract class BlobsAction extends StorageAction<BlobInfo[]> {
114+
private static abstract class BlobsAction extends StorageAction<Blob[]> {
115115

116116
@Override
117-
BlobInfo[] parse(String... args) {
117+
Blob[] parse(Storage storage, String... args) {
118118
if (args.length < 2) {
119119
throw new IllegalArgumentException();
120120
}
121-
BlobInfo[] blobInfos = new BlobInfo[args.length - 1];
121+
Blob[] blobs = new Blob[args.length - 1];
122122
for (int i = 1; i < args.length; i++) {
123-
blobInfos[i - 1] = BlobInfo.of(args[0], args[i]);
123+
blobs[i - 1] = new Blob(storage, args[0], args[i]);
124124
}
125-
return blobInfos;
125+
return blobs;
126126
}
127127

128128
@Override
@@ -140,22 +140,21 @@ public String params() {
140140
*/
141141
private static class InfoAction extends BlobsAction {
142142
@Override
143-
public void run(Storage storage, BlobInfo... blobInfos) {
144-
if (blobInfos.length == 1) {
145-
if (blobInfos[0].name().isEmpty()) {
143+
public void run(Storage storage, Blob... blobs) {
144+
if (blobs.length == 1) {
145+
if (blobs[0].info().name().isEmpty()) {
146146
// get Bucket
147-
Bucket bucket = new Bucket(storage, blobInfos[0].bucket());
147+
Bucket bucket = new Bucket(storage, blobs[0].info().bucket());
148148
System.out.println("Bucket info: " + bucket.reload().info());
149149
} else {
150150
// get Blob
151-
Blob blob = new Blob(storage, blobInfos[0]);
152-
System.out.println("Blob info: " + blob.reload().info());
151+
System.out.println("Blob info: " + blobs[0].reload().info());
153152
}
154153
} else {
155154
// use batch to get multiple blobs.
156155
BatchRequest.Builder batch = BatchRequest.builder();
157-
for (BlobInfo blobInfo : blobInfos) {
158-
batch.get(blobInfo.bucket(), blobInfo.name());
156+
for (Blob blob : blobs) {
157+
batch.get(blob.info().bucket(), blob.info().name());
159158
}
160159
BatchResponse response = storage.apply(batch.build());
161160
for (BatchResponse.Result<BlobInfo> result : response.gets()) {
@@ -165,11 +164,11 @@ public void run(Storage storage, BlobInfo... blobInfos) {
165164
}
166165

167166
@Override
168-
BlobInfo[] parse(String... args) {
167+
Blob[] parse(Storage storage, String... args) {
169168
if (args.length < 2) {
170-
return new BlobInfo[] {BlobInfo.of(args[0], "")};
169+
return new Blob[] {new Blob(storage, args[0], "")};
171170
}
172-
return super.parse(args);
171+
return super.parse(storage, args);
173172
}
174173

175174
@Override
@@ -187,24 +186,24 @@ public String params() {
187186
*/
188187
private static class DeleteAction extends BlobsAction {
189188
@Override
190-
public void run(Storage storage, BlobInfo... blobInfos) {
191-
if (blobInfos.length == 1) {
192-
boolean wasDeleted = new Blob(storage, blobInfos[0]).delete();
189+
public void run(Storage storage, Blob... blobs) {
190+
if (blobs.length == 1) {
191+
boolean wasDeleted = blobs[0].delete();
193192
if (wasDeleted) {
194-
System.out.println("Blob " + blobInfos[0] + " was deleted");
193+
System.out.println("Blob " + blobs[0].info() + " was deleted");
195194
}
196195
} else {
197196
// use batch operation
198197
BatchRequest.Builder batch = BatchRequest.builder();
199-
for (BlobInfo blobInfo : blobInfos) {
200-
batch.delete(blobInfo.bucket(), blobInfo.name());
198+
for (Blob blob : blobs) {
199+
batch.delete(blob.info().bucket(), blob.info().name());
201200
}
202201
int index = 0;
203202
BatchResponse response = storage.apply(batch.build());
204203
for (BatchResponse.Result<Boolean> result : response.deletes()) {
205204
if (result.get()) {
206205
// request order is maintained
207-
System.out.println("Blob " + blobInfos[index] + " was deleted");
206+
System.out.println("Blob " + blobs[index].info() + " was deleted");
208207
}
209208
index++;
210209
}
@@ -220,7 +219,7 @@ public void run(Storage storage, BlobInfo... blobInfos) {
220219
private static class ListAction extends StorageAction<String> {
221220

222221
@Override
223-
String parse(String... args) {
222+
String parse(Storage storage, String... args) {
224223
if (args.length == 0) {
225224
return null;
226225
}
@@ -231,16 +230,16 @@ String parse(String... args) {
231230
}
232231

233232
@Override
234-
public void run(Storage storage, String bucket) {
235-
if (bucket == null) {
233+
public void run(Storage storage, String bucketName) {
234+
if (bucketName == null) {
236235
// list buckets
237236
for (BucketInfo b : storage.list()) {
238237
System.out.println(b);
239238
}
240239
} else {
241240
// list a bucket's blobs
242-
Bucket functionalBucket = new Bucket(storage, bucket);
243-
for (Blob b : functionalBucket.list()) {
241+
Bucket bucket = new Bucket(storage, bucketName);
242+
for (Blob b : bucket.list()) {
244243
System.out.println(b.info());
245244
}
246245
}
@@ -257,17 +256,16 @@ public String params() {
257256
*
258257
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/insert">Objects: insert</a>
259258
*/
260-
private static class UploadAction extends StorageAction<Tuple<Path, BlobInfo>> {
259+
private static class UploadAction extends StorageAction<Tuple<Path, Blob>> {
261260
@Override
262-
public void run(Storage storage, Tuple<Path, BlobInfo> tuple) throws Exception {
261+
public void run(Storage storage, Tuple<Path, Blob> tuple) throws Exception {
263262
run(storage, tuple.x(), tuple.y());
264263
}
265264

266-
private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOException {
265+
private void run(Storage storage, Path uploadFrom, Blob blob) throws IOException {
267266
if (Files.size(uploadFrom) > 1_000_000) {
268267
// When content is not available or large (1MB or more) it is recommended
269268
// to write it in chunks via the blob's channel writer.
270-
Blob blob = new Blob(storage, blobInfo);
271269
try (BlobWriteChannel writer = blob.writer()) {
272270
byte[] buffer = new byte[1024];
273271
try (InputStream input = Files.newInputStream(uploadFrom)) {
@@ -284,20 +282,21 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
284282
} else {
285283
byte[] bytes = Files.readAllBytes(uploadFrom);
286284
// create the blob in one request.
287-
storage.create(blobInfo, bytes);
285+
storage.create(blob.info(), bytes);
288286
}
289287
System.out.println("Blob was created");
290288
}
291289

292290
@Override
293-
Tuple<Path, BlobInfo> parse(String... args) throws IOException {
291+
Tuple<Path, Blob> parse(Storage storage, String... args) throws IOException {
294292
if (args.length < 2 || args.length > 3) {
295293
throw new IllegalArgumentException();
296294
}
297295
Path path = Paths.get(args[0]);
298296
String contentType = Files.probeContentType(path);
299297
String blob = args.length < 3 ? path.getFileName().toString() : args[2];
300-
return Tuple.of(path, BlobInfo.builder(args[1], blob).contentType(contentType).build());
298+
BlobInfo info = BlobInfo.builder(args[1], blob).contentType(contentType).build();
299+
return Tuple.of(path, new Blob(storage, info));
301300
}
302301

303302
@Override
@@ -313,17 +312,15 @@ public String params() {
313312
*
314313
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/get">Objects: get</a>
315314
*/
316-
private static class DownloadAction extends StorageAction<Tuple<BlobInfo, Path>> {
315+
private static class DownloadAction extends StorageAction<Tuple<Blob, Path>> {
317316

318317
@Override
319-
public void run(Storage storage, Tuple<BlobInfo, Path> tuple) throws IOException {
320-
run(storage, tuple.x().bucket(), tuple.x().name(), tuple.y());
318+
public void run(Storage storage, Tuple<Blob, Path> tuple) throws IOException {
319+
run(storage, tuple.x(), tuple.y());
321320
}
322321

323-
private void run(Storage storage, String bucket, String blobName, Path downloadTo)
324-
throws IOException {
325-
Blob blob = new Blob(storage, bucket, blobName);
326-
BlobInfo blobInfo = storage.get(bucket, blobName);
322+
private void run(Storage storage, Blob blob, Path downloadTo) throws IOException {
323+
blob = blob.reload();
327324
if (!blob.exists()) {
328325
System.out.println("No such object");
329326
return;
@@ -332,7 +329,7 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT
332329
if (downloadTo != null) {
333330
writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile()));
334331
}
335-
if (blobInfo.size() < 1_000_000) {
332+
if (blob.info().size() < 1_000_000) {
336333
// Blob is small read all its content in one request
337334
byte[] content = blob.content();
338335
writeTo.write(content);
@@ -356,7 +353,7 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT
356353
}
357354

358355
@Override
359-
Tuple<BlobInfo, Path> parse(String... args) {
356+
Tuple<Blob, Path> parse(Storage storage, String... args) {
360357
if (args.length < 2 || args.length > 3) {
361358
throw new IllegalArgumentException();
362359
}
@@ -369,7 +366,7 @@ Tuple<BlobInfo, Path> parse(String... args) {
369366
} else {
370367
path = null;
371368
}
372-
return Tuple.of(BlobInfo.of(args[0], args[1]), path);
369+
return Tuple.of(new Blob(storage, args[0], args[1]), path);
373370
}
374371

375372
@Override
@@ -391,7 +388,7 @@ public void run(Storage storage, CopyRequest request) {
391388
}
392389

393390
@Override
394-
CopyRequest parse(String... args) {
391+
CopyRequest parse(Storage storage, String... args) {
395392
if (args.length != 4) {
396393
throw new IllegalArgumentException();
397394
}
@@ -417,7 +414,7 @@ public void run(Storage storage, ComposeRequest request) {
417414
}
418415

419416
@Override
420-
ComposeRequest parse(String... args) {
417+
ComposeRequest parse(Storage storage, String... args) {
421418
if (args.length < 3) {
422419
throw new IllegalArgumentException();
423420
}
@@ -441,17 +438,16 @@ public String params() {
441438
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">Objects: update</a>
442439
*/
443440
private static class UpdateMetadataAction extends
444-
StorageAction<Tuple<BlobInfo, Map<String, String>>> {
441+
StorageAction<Tuple<Blob, Map<String, String>>> {
445442

446443
@Override
447-
public void run(Storage storage, Tuple<BlobInfo, Map<String, String>> tuple)
444+
public void run(Storage storage, Tuple<Blob, Map<String, String>> tuple)
448445
throws IOException {
449-
run(storage, tuple.x().bucket(), tuple.x().name(), tuple.y());
446+
run(storage, tuple.x(), tuple.y());
450447
}
451448

452-
private void run(Storage storage, String bucket, String blobName,
453-
Map<String, String> metadata) {
454-
Blob blob = new Blob(storage, bucket, blobName).reload();
449+
private void run(Storage storage, Blob blob, Map<String, String> metadata) {
450+
blob = blob.reload();
455451
if (!blob.exists()) {
456452
System.out.println("No such object");
457453
return;
@@ -461,11 +457,11 @@ private void run(Storage storage, String bucket, String blobName,
461457
}
462458

463459
@Override
464-
Tuple<BlobInfo, Map<String, String>> parse(String... args) {
460+
Tuple<Blob, Map<String, String>> parse(Storage storage, String... args) {
465461
if (args.length < 2) {
466462
throw new IllegalArgumentException();
467463
}
468-
BlobInfo blobInfo = BlobInfo.of(args[0], args[1]);
464+
Blob blob = new Blob(storage, args[0], args[1]);
469465
Map<String, String> metadata = new HashMap<>();
470466
for (int i = 2; i < args.length; i++) {
471467
int idx = args[i].indexOf('=');
@@ -475,7 +471,7 @@ Tuple<BlobInfo, Map<String, String>> parse(String... args) {
475471
metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1));
476472
}
477473
}
478-
return Tuple.of(blobInfo, metadata);
474+
return Tuple.of(blob, metadata);
479475
}
480476

481477
@Override
@@ -491,27 +487,27 @@ public String params() {
491487
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed URLs</a>
492488
*/
493489
private static class SignUrlAction extends
494-
StorageAction<Tuple<ServiceAccountAuthCredentials, BlobInfo>> {
490+
StorageAction<Tuple<ServiceAccountAuthCredentials, Blob>> {
495491

496492
private static final char[] PASSWORD = "notasecret".toCharArray();
497493

498494
@Override
499-
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo> tuple)
495+
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, Blob> tuple)
500496
throws Exception {
501497
run(storage, tuple.x(), tuple.y());
502498
}
503499

504-
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo)
500+
private void run(Storage storage, ServiceAccountAuthCredentials cred, Blob blob)
505501
throws IOException {
506502
Calendar cal = Calendar.getInstance();
507503
cal.add(Calendar.DATE, 1);
508504
long expiration = cal.getTimeInMillis() / 1000;
509505
System.out.println("Signed URL: " +
510-
new Blob(storage, blobInfo).signUrl(expiration, SignUrlOption.serviceAccount(cred)));
506+
blob.signUrl(expiration, SignUrlOption.serviceAccount(cred)));
511507
}
512508

513509
@Override
514-
Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(String... args)
510+
Tuple<ServiceAccountAuthCredentials, Blob> parse(Storage storage, String... args)
515511
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException,
516512
UnrecoverableKeyException {
517513
if (args.length != 4) {
@@ -521,7 +517,7 @@ Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(String... args)
521517
keystore.load(Files.newInputStream(Paths.get(args[0])), PASSWORD);
522518
PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", PASSWORD);
523519
ServiceAccountAuthCredentials cred = AuthCredentials.createFor(args[1], privateKey);
524-
return Tuple.of(cred, BlobInfo.of(args[2], args[3]));
520+
return Tuple.of(cred, new Blob(storage, args[2], args[3]));
525521
}
526522

527523
@Override
@@ -582,7 +578,7 @@ public static void main(String... args) throws Exception {
582578
Storage storage = StorageFactory.instance().get(optionsBuilder.build());
583579
Object request;
584580
try {
585-
request = action.parse(args);
581+
request = action.parse(storage, args);
586582
} catch (IllegalArgumentException ex) {
587583
System.out.println("Invalid input for action '" + args[1] + "'");
588584
System.out.println("Expected: " + action.params());

0 commit comments

Comments
 (0)