Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add metadata update to example and fix builder user-settable fields #58

Merged
merged 5 commits into from
May 14, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/main/java/com/google/gcloud/examples/StorageExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* -Dexec.args="[<project_id>] list [<bucket>]| info [<bucket> [<file>]]|
* download <bucket> <path> [local_file]| upload <local_file> <bucket> [<path>]|
* delete <bucket> <path>+| cp <from_bucket> <from_path> <to_bucket> <to_path>|
* compose <bucket> <from_path>+ <to_path>"}
* compose <bucket> <from_path>+ <to_path>| update_metadata <bucket> <file> [key=value]*"}
* </li>
* </ol>
*/
Expand Down Expand Up @@ -336,6 +336,45 @@ public String params() {
}
}

private static class UpdateMetadata extends StorageAction<Tuple<Blob, Map<String, String>>> {

This comment was marked as spam.

This comment was marked as spam.


@Override
public void run(StorageService storage, Tuple<Blob, Map<String, String>> tuple)
throws IOException {
Blob blob = storage.get(tuple.x().bucket(), tuple.x().name());
if (blob == null) {
System.out.println("No such object");
return;
}
blob = blob.toBuilder().metadata(tuple.y()).build();
System.out.println("before: " + blob);
System.out.println(storage.update(blob));
}

@Override
Tuple<Blob, Map<String, String>> parse(String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
Blob blob = Blob.of(args[0], args[1]);
Map<String ,String> metadata = new HashMap<>();

This comment was marked as spam.

This comment was marked as spam.

for (int i = 2; i < args.length; i++) {
int idx = args[i].indexOf('=');
if (idx < 0) {
metadata.put(args[i], "");
} else {
metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1));
}
}
return Tuple.of(blob, metadata);
}

@Override
public String params() {
return "<bucket> <path> [local_file]";
}
}

static {
ACTIONS.put("info", new InfoAction());
ACTIONS.put("delete", new DeleteAction());
Expand All @@ -344,6 +383,7 @@ public String params() {
ACTIONS.put("download", new DownloadAction());
ACTIONS.put("cp", new CopyAction());
ACTIONS.put("compose", new ComposeAction());
ACTIONS.put("update_metadata", new UpdateMetadata());
}

public static void printUsage() {
Expand Down Expand Up @@ -378,7 +418,7 @@ public static void main(String... args) throws Exception {
args = Arrays.copyOfRange(args, 1, args.length);
}
if (action == null) {
System.out.println("Unrecognized action '" + args[1] + "'");
System.out.println("Unrecognized action.");
printUsage();
return;
}
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/com/google/gcloud/storage/Blob.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ public Builder contentType(String contentType) {
return this;
}

Builder contentDisposition(String contentDisposition) {
public Builder contentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
return this;
}

Builder contentLanguage(String contentLanguage) {
public Builder contentLanguage(String contentLanguage) {
this.contentLanguage = contentLanguage;
return this;
}
Expand All @@ -157,12 +157,12 @@ public Builder acl(List<Acl> acl) {
return this;
}

public Builder owner(Acl.Entity owner) {
Builder owner(Acl.Entity owner) {

This comment was marked as spam.

This comment was marked as spam.

this.owner = owner;
return this;
}

public Builder size(Long size) {
Builder size(Long size) {
this.size = size;
return this;
}
Expand All @@ -177,7 +177,7 @@ Builder selfLink(String selfLink) {
return this;
}

Builder md5(String md5) {
public Builder md5(String md5) {
this.md5 = md5;
return this;
}
Expand All @@ -187,7 +187,7 @@ public Builder crc32c(String crc32c) {
return this;
}

public Builder mediaLink(String mediaLink) {
Builder mediaLink(String mediaLink) {
this.mediaLink = mediaLink;
return this;
}
Expand All @@ -197,22 +197,22 @@ public Builder metadata(Map<String, String> metadata) {
return this;
}

public Builder generation(Long generation) {
Builder generation(Long generation) {
this.generation = generation;
return this;
}

public Builder metageneration(Long metageneration) {
Builder metageneration(Long metageneration) {
this.metageneration = metageneration;
return this;
}

public Builder deleteTime(Long deleteTime) {
Builder deleteTime(Long deleteTime) {
this.deleteTime = deleteTime;
return this;
}

public Builder updateTime(Long updateTime) {
Builder updateTime(Long updateTime) {
this.updateTime = updateTime;
return this;
}
Expand Down Expand Up @@ -365,7 +365,13 @@ public Builder toBuilder() {

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("bucket", bucket).add("name", name).toString();
return MoreObjects.toStringHelper(this)
.add("bucket", bucket)
.add("name", name)
.add("size", size)
.add("content-type", contentType)
.add("metadata", metadata)
.toString();
}

public static Blob of(String bucket, String name) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/google/gcloud/storage/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ public Builder toBuilder() {

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("name", name).toString();
return MoreObjects.toStringHelper(this)
.add("name", name)
.toString();
}

public static Bucket of(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_GENERATION_NOT_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH;
import static com.google.gcloud.spi.StorageRpc.Option.IF_SOURCE_METAGENERATION_NOT_MATCH;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.util.concurrent.Executors.callable;

import com.google.api.services.storage.model.StorageObject;
Expand Down Expand Up @@ -131,7 +132,7 @@ public com.google.api.services.storage.model.Bucket call() {
try {
return storageRpc.get(bucketPb, optionsMap);
} catch (StorageServiceException ex) {
if (ex.code() == 404) {
if (ex.code() == HTTP_NOT_FOUND) {
return null;
}
throw ex;
Expand All @@ -151,7 +152,7 @@ public StorageObject call() {
try {
return storageRpc.get(storedObject, optionsMap);
} catch (StorageServiceException ex) {
if (ex.code() == 404) {
if (ex.code() == HTTP_NOT_FOUND) {
return null;
}
throw ex;
Expand Down Expand Up @@ -328,7 +329,7 @@ public BatchResponse apply(BatchRequest batchRequest) {
List<BatchResponse.Result<Blob>> updates = transformBatchResult(
toUpdate, response.updates, Blob.FROM_PB_FUNCTION);
List<BatchResponse.Result<Blob>> gets = transformBatchResult(
toGet, response.gets, Blob.FROM_PB_FUNCTION, 404);
toGet, response.gets, Blob.FROM_PB_FUNCTION, HTTP_NOT_FOUND);
return new BatchResponse(deletes, updates, gets);
}

Expand Down