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 static load method to Blob and Bucket, add tests, update example #257

Merged
merged 5 commits into from
Oct 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -96,22 +95,6 @@ protected String params() {
}
}

private static abstract class BlobAction extends StorageAction<Blob> {

@Override
Blob parse(Storage storage, String... args) {
if (args.length != 2) {
throw new IllegalArgumentException();
}
return new Blob(storage, args[0], args[1]);
}

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

private static abstract class BlobsAction extends StorageAction<Blob[]> {

@Override
Expand All @@ -121,7 +104,7 @@ Blob[] parse(Storage storage, String... args) {
}
Blob[] blobs = new Blob[args.length - 1];
for (int i = 1; i < args.length; i++) {
blobs[i - 1] = new Blob(storage, args[0], args[i]);
blobs[i - 1] = Blob.load(storage, args[0], args[i]);

This comment was marked as spam.

This comment was marked as spam.

}
return blobs;
}
Expand All @@ -145,11 +128,11 @@ public void run(Storage storage, Blob... blobs) {
if (blobs.length == 1) {
if (blobs[0].info().name().isEmpty()) {
// get Bucket
Bucket bucket = new Bucket(storage, blobs[0].info().bucket());
System.out.println("Bucket info: " + bucket.reload().info());
Bucket bucket = Bucket.load(storage, blobs[0].info().bucket());
System.out.println("Bucket info: " + bucket.info());
} else {
// get Blob
System.out.println("Blob info: " + blobs[0].reload().info());
System.out.println("Blob info: " + blobs[0].info());
}
} else {
// use batch to get multiple blobs.
Expand All @@ -167,7 +150,7 @@ public void run(Storage storage, Blob... blobs) {
@Override
Blob[] parse(Storage storage, String... args) {
if (args.length < 2) {
return new Blob[] {new Blob(storage, args[0], "")};
return new Blob[] {new Blob(storage, BlobInfo.builder(args[0], "").build())};
}
return super.parse(storage, args);
}
Expand Down Expand Up @@ -239,7 +222,7 @@ public void run(Storage storage, String bucketName) {
}
} else {
// list a bucket's blobs
Bucket bucket = new Bucket(storage, bucketName);
Bucket bucket = Bucket.load(storage, bucketName);
for (Blob b : bucket.list()) {
System.out.println(b.info());
}
Expand Down Expand Up @@ -321,7 +304,6 @@ public void run(Storage storage, Tuple<Blob, Path> tuple) throws IOException {
}

private void run(Storage storage, Blob blob, Path downloadTo) throws IOException {
blob = blob.reload();
if (!blob.exists()) {
System.out.println("No such object");
return;
Expand Down Expand Up @@ -367,7 +349,7 @@ Tuple<Blob, Path> parse(Storage storage, String... args) {
} else {
path = null;
}
return Tuple.of(new Blob(storage, args[0], args[1]), path);
return Tuple.of(Blob.load(storage, args[0], args[1]), path);
}

@Override
Expand Down Expand Up @@ -448,7 +430,6 @@ public void run(Storage storage, Tuple<Blob, Map<String, String>> tuple)
}

private void run(Storage storage, Blob blob, Map<String, String> metadata) {
blob = blob.reload();
if (!blob.exists()) {
System.out.println("No such object");
return;
Expand All @@ -462,7 +443,7 @@ Tuple<Blob, Map<String, String>> parse(Storage storage, String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
Blob blob = new Blob(storage, args[0], args[1]);
Blob blob = Blob.load(storage, args[0], args[1]);
Map<String, String> metadata = new HashMap<>();
for (int i = 2; i < args.length; i++) {
int idx = args[i].indexOf('=');
Expand Down Expand Up @@ -515,7 +496,7 @@ Tuple<ServiceAccountAuthCredentials, Blob> parse(Storage storage, String... args
keystore.load(Files.newInputStream(Paths.get(args[0])), PASSWORD);
PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", PASSWORD);
ServiceAccountAuthCredentials cred = AuthCredentials.createFor(args[1], privateKey);
return Tuple.of(cred, new Blob(storage, args[2], args[3]));
return Tuple.of(cred, Blob.load(storage, args[2], args[3]));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,27 @@ public Blob(Storage storage, BlobInfo info) {
}

/**
* Constructs a {@code Blob} object for the provided bucket and blob names. The storage service is
* used to issue requests.
* Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
* get the latest blob information.
*
* @param storage the storage service used for issuing requests
* @param bucket bucket's name
* @param blob blob's name
*/
public Blob(Storage storage, String bucket, String blob) {
this.storage = checkNotNull(storage);
this.info = BlobInfo.builder(BlobId.of(bucket, blob)).build();
public static Blob load(Storage storage, String bucket, String blob) {
return load(storage, BlobId.of(bucket, blob));
}

/**
* Constructs a {@code Blob} object for the provided {@code BlobId}. The storage service is used
* to issue requests.
* Creates a {@code Blob} object for the provided {@code blobId}. Performs an RPC call to get the
* latest blob information.
*
* @param storage the storage service used for issuing requests
* @param blobId blob's identifier
*/
public Blob(Storage storage, BlobId blobId) {
this.storage = checkNotNull(storage);
this.info = BlobInfo.builder(blobId).build();
public static Blob load(Storage storage, BlobId blobId) {
BlobInfo info = storage.get(blobId);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

return new Blob(storage, info);
}

/**
Expand Down Expand Up @@ -209,9 +208,8 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
*/
public Blob copyTo(BlobId targetBlob, BlobSourceOption... options) {
BlobInfo updatedInfo = info.toBuilder().blobId(targetBlob).build();
CopyRequest copyRequest =
CopyRequest.builder().source(info.bucket(), info.name())
.sourceOptions(convert(info, options)).target(updatedInfo).build();
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
.sourceOptions(convert(info, options)).target(updatedInfo).build();
return new Blob(storage, storage.copy(copyRequest));
}

Expand Down Expand Up @@ -251,9 +249,8 @@ public Blob copyTo(String targetBucket, BlobSourceOption... options) {
*/
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
BlobInfo updatedInfo = info.toBuilder().blobId(BlobId.of(targetBucket, targetBlob)).build();
CopyRequest copyRequest =
CopyRequest.builder().source(info.bucket(), info.name())
.sourceOptions(convert(info, options)).target(updatedInfo).build();
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
.sourceOptions(convert(info, options)).target(updatedInfo).build();
return new Blob(storage, storage.copy(copyRequest));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public Bucket(Storage storage, BucketInfo info) {
}

/**
* Constructs a {@code Bucket} object for the provided name. The storage service is used to issue
* requests.
* Creates a {@code Bucket} object for the provided bucket name. Performs an RPC call to get the
* latest bucket information.
*
* @param storage the storage service used for issuing requests
* @param bucket bucket's name
*/
public Bucket(Storage storage, String bucket) {
this.storage = checkNotNull(storage);
this.info = BucketInfo.of(checkNotNull(bucket));
public static Bucket load(Storage storage, String bucket) {
BucketInfo info = storage.get(bucket);
return new Bucket(storage, info);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,20 @@ public void testDeleteSome() throws Exception {
assertEquals(deleleResultList.get(i), result.get(i));
}
}

@Test
public void testLoadFromString() throws Exception {
expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO);
replay(storage);
Blob loadedBlob = Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name());
assertEquals(BLOB_INFO, loadedBlob.info());
}

@Test
public void testLoadFromId() throws Exception {
expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO);
replay(storage);
Blob loadedBlob = Blob.load(storage, BLOB_INFO.blobId());
assertEquals(BLOB_INFO, loadedBlob.info());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,12 @@ public void testCreate() throws Exception {
Blob blob = bucket.create("n", content);
assertEquals(info, blob.info());
}

@Test
public void testLoad() throws Exception {
expect(storage.get(BUCKET_INFO.name())).andReturn(BUCKET_INFO);
replay(storage);
Bucket loadedBucket = Bucket.load(storage, BUCKET_INFO.name());
assertEquals(BUCKET_INFO, loadedBucket.info());
}
}