Skip to content

Commit 6c59a54

Browse files
committed
add metadata update to example and fix builder user-settable fields
1 parent 9761b3c commit 6c59a54

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* -Dexec.args="[<project_id>] list [<bucket>]| info [<bucket> [<file>]]|
5858
* download <bucket> <path> [local_file]| upload <local_file> <bucket> [<path>]|
5959
* delete <bucket> <path>+| cp <from_bucket> <from_path> <to_bucket> <to_path>|
60-
* compose <bucket> <from_path>+ <to_path>"}
60+
* compose <bucket> <from_path>+ <to_path>| update_metadata <bucket> <file> [key=value]*"}
6161
* </li>
6262
* </ol>
6363
*/
@@ -336,6 +336,45 @@ public String params() {
336336
}
337337
}
338338

339+
private static class UpdateMetadata extends StorageAction<Tuple<Blob, Map<String, String>>> {
340+
341+
@Override
342+
public void run(StorageService storage, Tuple<Blob, Map<String, String>> tuple)
343+
throws IOException {
344+
Blob blob = storage.get(tuple.x().bucket(), tuple.x().name());
345+
if (blob == null) {
346+
System.out.println("No such object");
347+
return;
348+
}
349+
blob = blob.toBuilder().metadata(tuple.y()).build();
350+
System.out.println("before: " + blob);
351+
System.out.println(storage.update(blob));
352+
}
353+
354+
@Override
355+
Tuple<Blob, Map<String, String>> parse(String... args) {
356+
if (args.length < 2) {
357+
throw new IllegalArgumentException();
358+
}
359+
Blob blob = Blob.of(args[0], args[1]);
360+
Map<String ,String> metadata = new HashMap<>();
361+
for (int i = 2; i < args.length; i++) {
362+
int idx = args[i].indexOf('=');
363+
if (idx < 0) {
364+
metadata.put(args[i], "");
365+
} else {
366+
metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1));
367+
}
368+
}
369+
return Tuple.of(blob, metadata);
370+
}
371+
372+
@Override
373+
public String params() {
374+
return "<bucket> <path> [local_file]";
375+
}
376+
}
377+
339378
static {
340379
ACTIONS.put("info", new InfoAction());
341380
ACTIONS.put("delete", new DeleteAction());
@@ -344,6 +383,7 @@ public String params() {
344383
ACTIONS.put("download", new DownloadAction());
345384
ACTIONS.put("cp", new CopyAction());
346385
ACTIONS.put("compose", new ComposeAction());
386+
ACTIONS.put("update_metadata", new UpdateMetadata());
347387
}
348388

349389
public static void printUsage() {
@@ -378,7 +418,7 @@ public static void main(String... args) throws Exception {
378418
args = Arrays.copyOfRange(args, 1, args.length);
379419
}
380420
if (action == null) {
381-
System.out.println("Unrecognized action '" + args[1] + "'");
421+
System.out.println("Unrecognized action.");
382422
printUsage();
383423
return;
384424
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ public Builder contentType(String contentType) {
127127
return this;
128128
}
129129

130-
Builder contentDisposition(String contentDisposition) {
130+
public Builder contentDisposition(String contentDisposition) {
131131
this.contentDisposition = contentDisposition;
132132
return this;
133133
}
134134

135-
Builder contentLanguage(String contentLanguage) {
135+
public Builder contentLanguage(String contentLanguage) {
136136
this.contentLanguage = contentLanguage;
137137
return this;
138138
}
@@ -157,12 +157,12 @@ public Builder acl(List<Acl> acl) {
157157
return this;
158158
}
159159

160-
public Builder owner(Acl.Entity owner) {
160+
Builder owner(Acl.Entity owner) {
161161
this.owner = owner;
162162
return this;
163163
}
164164

165-
public Builder size(Long size) {
165+
Builder size(Long size) {
166166
this.size = size;
167167
return this;
168168
}
@@ -177,7 +177,7 @@ Builder selfLink(String selfLink) {
177177
return this;
178178
}
179179

180-
Builder md5(String md5) {
180+
public Builder md5(String md5) {
181181
this.md5 = md5;
182182
return this;
183183
}
@@ -187,7 +187,7 @@ public Builder crc32c(String crc32c) {
187187
return this;
188188
}
189189

190-
public Builder mediaLink(String mediaLink) {
190+
Builder mediaLink(String mediaLink) {
191191
this.mediaLink = mediaLink;
192192
return this;
193193
}
@@ -197,22 +197,22 @@ public Builder metadata(Map<String, String> metadata) {
197197
return this;
198198
}
199199

200-
public Builder generation(Long generation) {
200+
Builder generation(Long generation) {
201201
this.generation = generation;
202202
return this;
203203
}
204204

205-
public Builder metageneration(Long metageneration) {
205+
Builder metageneration(Long metageneration) {
206206
this.metageneration = metageneration;
207207
return this;
208208
}
209209

210-
public Builder deleteTime(Long deleteTime) {
210+
Builder deleteTime(Long deleteTime) {
211211
this.deleteTime = deleteTime;
212212
return this;
213213
}
214214

215-
public Builder updateTime(Long updateTime) {
215+
Builder updateTime(Long updateTime) {
216216
this.updateTime = updateTime;
217217
return this;
218218
}
@@ -365,7 +365,13 @@ public Builder toBuilder() {
365365

366366
@Override
367367
public String toString() {
368-
return MoreObjects.toStringHelper(this).add("bucket", bucket).add("name", name).toString();
368+
return MoreObjects.toStringHelper(this)
369+
.add("bucket", bucket)
370+
.add("name", name)
371+
.add("size", size)
372+
.add("content-type", contentType)
373+
.add("metadata", metadata)
374+
.toString();
369375
}
370376

371377
public static Blob of(String bucket, String name) {

src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,9 @@ public Builder toBuilder() {
561561

562562
@Override
563563
public String toString() {
564-
return MoreObjects.toStringHelper(this).add("name", name).toString();
564+
return MoreObjects.toStringHelper(this)
565+
.add("name", name)
566+
.toString();
565567
}
566568

567569
public static Bucket of(String name) {

src/main/java/com/google/gcloud/storage/StorageServiceImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH;
2929
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH;
3030
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH;
31+
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
3132
import static java.util.concurrent.Executors.callable;
3233

3334
import com.google.api.services.storage.model.StorageObject;
@@ -131,7 +132,7 @@ public com.google.api.services.storage.model.Bucket call() {
131132
try {
132133
return storageRpc.get(bucketPb, optionsMap);
133134
} catch (StorageServiceException ex) {
134-
if (ex.code() == 404) {
135+
if (ex.code() == HTTP_NOT_FOUND) {
135136
return null;
136137
}
137138
throw ex;
@@ -151,7 +152,7 @@ public StorageObject call() {
151152
try {
152153
return storageRpc.get(storedObject, optionsMap);
153154
} catch (StorageServiceException ex) {
154-
if (ex.code() == 404) {
155+
if (ex.code() == HTTP_NOT_FOUND) {
155156
return null;
156157
}
157158
throw ex;
@@ -328,7 +329,7 @@ public BatchResponse apply(BatchRequest batchRequest) {
328329
List<BatchResponse.Result<Blob>> updates = transformBatchResult(
329330
toUpdate, response.updates, Blob.FROM_PB_FUNCTION);
330331
List<BatchResponse.Result<Blob>> gets = transformBatchResult(
331-
toGet, response.gets, Blob.FROM_PB_FUNCTION, 404);
332+
toGet, response.gets, Blob.FROM_PB_FUNCTION, HTTP_NOT_FOUND);
332333
return new BatchResponse(deletes, updates, gets);
333334
}
334335

0 commit comments

Comments
 (0)