Skip to content

Commit

Permalink
Update StorageExample actions to use functional Blob and Bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Oct 6, 2015
1 parent abc9a00 commit 167197f
Showing 1 changed file with 63 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ private static abstract class StorageAction<T> {

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

abstract T parse(String... args) throws Exception;
abstract T parse(Storage storage, String... args) throws Exception;

protected String params() {
return "";
}
}

private static abstract class BlobAction extends StorageAction<BlobInfo> {
private static abstract class BlobAction extends StorageAction<Blob> {

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

@Override
Expand All @@ -111,18 +111,18 @@ public String params() {
}
}

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

@Override
BlobInfo[] parse(String... args) {
Blob[] parse(Storage storage, String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
BlobInfo[] blobInfos = new BlobInfo[args.length - 1];
Blob[] blobs = new Blob[args.length - 1];
for (int i = 1; i < args.length; i++) {
blobInfos[i - 1] = BlobInfo.of(args[0], args[i]);
blobs[i - 1] = new Blob(storage, args[0], args[i]);
}
return blobInfos;
return blobs;
}

@Override
Expand All @@ -140,22 +140,21 @@ public String params() {
*/
private static class InfoAction extends BlobsAction {
@Override
public void run(Storage storage, BlobInfo... blobInfos) {
if (blobInfos.length == 1) {
if (blobInfos[0].name().isEmpty()) {
public void run(Storage storage, Blob... blobs) {
if (blobs.length == 1) {
if (blobs[0].info().name().isEmpty()) {
// get Bucket
Bucket bucket = new Bucket(storage, blobInfos[0].bucket());
Bucket bucket = new Bucket(storage, blobs[0].info().bucket());
System.out.println("Bucket info: " + bucket.reload().info());
} else {
// get Blob
Blob blob = new Blob(storage, blobInfos[0]);
System.out.println("Blob info: " + blob.reload().info());
System.out.println("Blob info: " + blobs[0].reload().info());
}
} else {
// use batch to get multiple blobs.
BatchRequest.Builder batch = BatchRequest.builder();
for (BlobInfo blobInfo : blobInfos) {
batch.get(blobInfo.bucket(), blobInfo.name());
for (Blob blob : blobs) {
batch.get(blob.info().bucket(), blob.info().name());
}
BatchResponse response = storage.apply(batch.build());
for (BatchResponse.Result<BlobInfo> result : response.gets()) {
Expand All @@ -165,11 +164,11 @@ public void run(Storage storage, BlobInfo... blobInfos) {
}

@Override
BlobInfo[] parse(String... args) {
Blob[] parse(Storage storage, String... args) {
if (args.length < 2) {
return new BlobInfo[] {BlobInfo.of(args[0], "")};
return new Blob[] {new Blob(storage, args[0], "")};
}
return super.parse(args);
return super.parse(storage, args);
}

@Override
Expand All @@ -187,24 +186,24 @@ public String params() {
*/
private static class DeleteAction extends BlobsAction {
@Override
public void run(Storage storage, BlobInfo... blobInfos) {
if (blobInfos.length == 1) {
boolean wasDeleted = new Blob(storage, blobInfos[0]).delete();
public void run(Storage storage, Blob... blobs) {
if (blobs.length == 1) {
boolean wasDeleted = blobs[0].delete();
if (wasDeleted) {
System.out.println("Blob " + blobInfos[0] + " was deleted");
System.out.println("Blob " + blobs[0].info() + " was deleted");
}
} else {
// use batch operation
BatchRequest.Builder batch = BatchRequest.builder();
for (BlobInfo blobInfo : blobInfos) {
batch.delete(blobInfo.bucket(), blobInfo.name());
for (Blob blob : blobs) {
batch.delete(blob.info().bucket(), blob.info().name());
}
int index = 0;
BatchResponse response = storage.apply(batch.build());
for (BatchResponse.Result<Boolean> result : response.deletes()) {
if (result.get()) {
// request order is maintained
System.out.println("Blob " + blobInfos[index] + " was deleted");
System.out.println("Blob " + blobs[index].info() + " was deleted");
}
index++;
}
Expand All @@ -220,7 +219,7 @@ public void run(Storage storage, BlobInfo... blobInfos) {
private static class ListAction extends StorageAction<String> {

@Override
String parse(String... args) {
String parse(Storage storage, String... args) {
if (args.length == 0) {
return null;
}
Expand All @@ -231,16 +230,16 @@ String parse(String... args) {
}

@Override
public void run(Storage storage, String bucket) {
if (bucket == null) {
public void run(Storage storage, String bucketName) {
if (bucketName == null) {
// list buckets
for (BucketInfo b : storage.list()) {
System.out.println(b);
}
} else {
// list a bucket's blobs
Bucket functionalBucket = new Bucket(storage, bucket);
for (Blob b : functionalBucket.list()) {
Bucket bucket = new Bucket(storage, bucketName);
for (Blob b : bucket.list()) {
System.out.println(b.info());
}
}
Expand All @@ -257,17 +256,16 @@ public String params() {
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/insert">Objects: insert</a>
*/
private static class UploadAction extends StorageAction<Tuple<Path, BlobInfo>> {
private static class UploadAction extends StorageAction<Tuple<Path, Blob>> {
@Override
public void run(Storage storage, Tuple<Path, BlobInfo> tuple) throws Exception {
public void run(Storage storage, Tuple<Path, Blob> tuple) throws Exception {
run(storage, tuple.x(), tuple.y());
}

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

@Override
Tuple<Path, BlobInfo> parse(String... args) throws IOException {
Tuple<Path, Blob> parse(Storage storage, String... args) throws IOException {
if (args.length < 2 || args.length > 3) {
throw new IllegalArgumentException();
}
Path path = Paths.get(args[0]);
String contentType = Files.probeContentType(path);
String blob = args.length < 3 ? path.getFileName().toString() : args[2];
return Tuple.of(path, BlobInfo.builder(args[1], blob).contentType(contentType).build());
BlobInfo info = BlobInfo.builder(args[1], blob).contentType(contentType).build();
return Tuple.of(path, new Blob(storage, info));
}

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

@Override
public void run(Storage storage, Tuple<BlobInfo, Path> tuple) throws IOException {
run(storage, tuple.x().bucket(), tuple.x().name(), tuple.y());
public void run(Storage storage, Tuple<Blob, Path> tuple) throws IOException {
run(storage, tuple.x(), tuple.y());
}

private void run(Storage storage, String bucket, String blobName, Path downloadTo)
throws IOException {
Blob blob = new Blob(storage, bucket, blobName);
BlobInfo blobInfo = storage.get(bucket, blobName);
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 All @@ -332,7 +329,7 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT
if (downloadTo != null) {
writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile()));
}
if (blobInfo.size() < 1_000_000) {
if (blob.info().size() < 1_000_000) {
// Blob is small read all its content in one request
byte[] content = blob.content();
writeTo.write(content);
Expand All @@ -356,7 +353,7 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT
}

@Override
Tuple<BlobInfo, Path> parse(String... args) {
Tuple<Blob, Path> parse(Storage storage, String... args) {
if (args.length < 2 || args.length > 3) {
throw new IllegalArgumentException();
}
Expand All @@ -369,7 +366,7 @@ Tuple<BlobInfo, Path> parse(String... args) {
} else {
path = null;
}
return Tuple.of(BlobInfo.of(args[0], args[1]), path);
return Tuple.of(new Blob(storage, args[0], args[1]), path);
}

@Override
Expand All @@ -391,7 +388,7 @@ public void run(Storage storage, CopyRequest request) {
}

@Override
CopyRequest parse(String... args) {
CopyRequest parse(Storage storage, String... args) {
if (args.length != 4) {
throw new IllegalArgumentException();
}
Expand All @@ -417,7 +414,7 @@ public void run(Storage storage, ComposeRequest request) {
}

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

@Override
public void run(Storage storage, Tuple<BlobInfo, Map<String, String>> tuple)
public void run(Storage storage, Tuple<Blob, Map<String, String>> tuple)
throws IOException {
run(storage, tuple.x().bucket(), tuple.x().name(), tuple.y());
run(storage, tuple.x(), tuple.y());
}

private void run(Storage storage, String bucket, String blobName,
Map<String, String> metadata) {
Blob blob = new Blob(storage, bucket, blobName).reload();
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 @@ -461,11 +457,11 @@ private void run(Storage storage, String bucket, String blobName,
}

@Override
Tuple<BlobInfo, Map<String, String>> parse(String... args) {
Tuple<Blob, Map<String, String>> parse(Storage storage, String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
BlobInfo blobInfo = BlobInfo.of(args[0], args[1]);
Blob blob = new Blob(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 All @@ -475,7 +471,7 @@ Tuple<BlobInfo, Map<String, String>> parse(String... args) {
metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1));
}
}
return Tuple.of(blobInfo, metadata);
return Tuple.of(blob, metadata);
}

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

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

@Override
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo> tuple)
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, Blob> tuple)
throws Exception {
run(storage, tuple.x(), tuple.y());
}

private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo)
private void run(Storage storage, ServiceAccountAuthCredentials cred, Blob blob)
throws IOException {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
long expiration = cal.getTimeInMillis() / 1000;
System.out.println("Signed URL: " +
new Blob(storage, blobInfo).signUrl(expiration, SignUrlOption.serviceAccount(cred)));
blob.signUrl(expiration, SignUrlOption.serviceAccount(cred)));
}

@Override
Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(String... args)
Tuple<ServiceAccountAuthCredentials, Blob> parse(Storage storage, String... args)
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException,
UnrecoverableKeyException {
if (args.length != 4) {
Expand All @@ -521,7 +517,7 @@ Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(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, BlobInfo.of(args[2], args[3]));
return Tuple.of(cred, new Blob(storage, args[2], args[3]));
}

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

0 comments on commit 167197f

Please sign in to comment.