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

Combine info and functional objects #561

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -34,8 +34,8 @@
import com.google.gcloud.Page;
import com.google.gcloud.bigquery.BigQuery.DatasetOption;
import com.google.gcloud.bigquery.testing.RemoteBigQueryHelper;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.BucketInfo;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.Bucket;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.testing.RemoteGcsHelper;

Expand Down Expand Up @@ -145,10 +145,16 @@ public static void beforeClass() throws IOException, InterruptedException {
RemoteGcsHelper gcsHelper = RemoteGcsHelper.create();
bigquery = bigqueryHelper.options().service();
storage = gcsHelper.options().service();
storage.create(BucketInfo.of(BUCKET));
storage.create(BlobInfo.builder(BUCKET, LOAD_FILE).contentType("text/plain").build(),
Bucket.of(storage, BUCKET).create();
Blob.builder(storage, BUCKET, LOAD_FILE)
.contentType("text/plain")
.build()
.create(
CSV_CONTENT.getBytes(StandardCharsets.UTF_8));
storage.create(BlobInfo.builder(BUCKET, JSON_LOAD_FILE).contentType("application/json").build(),
Blob.builder(storage, BUCKET, JSON_LOAD_FILE)
.contentType("application/json")
.build()
.create(
JSON_CONTENT.getBytes(StandardCharsets.UTF_8));
DatasetInfo info = DatasetInfo.builder(DATASET).description(DESCRIPTION).build();
bigquery.create(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.gcloud.examples;

import com.google.common.base.Joiner;
import com.google.gcloud.resourcemanager.ProjectInfo;
import com.google.gcloud.resourcemanager.Project;
import com.google.gcloud.resourcemanager.ResourceManager;
import com.google.gcloud.resourcemanager.ResourceManagerOptions;

Expand Down Expand Up @@ -64,8 +64,7 @@ public void run(ResourceManager resourceManager, String... args) {
labels.put(args[i], "");
}
}
ProjectInfo project =
resourceManager.create(ProjectInfo.builder(projectId).labels(labels).build());
Project project = Project.builder(resourceManager, projectId).labels(labels).build().create();
System.out.printf(
"Successfully created project '%s': %s.%n", projectId, projectDetails(project));
}
Expand Down Expand Up @@ -111,7 +110,7 @@ private static class GetAction implements ResourceManagerAction {
@Override
public void run(ResourceManager resourceManager, String... args) {
String projectId = args[0];
ProjectInfo project = resourceManager.get(projectId);
Project project = resourceManager.get(projectId);
if (project != null) {
System.out.printf(
"Successfully got project '%s': %s.%n", projectId, projectDetails(project));
Expand All @@ -135,7 +134,7 @@ private static class ListAction implements ResourceManagerAction {
@Override
public void run(ResourceManager resourceManager, String... args) {
System.out.println("Projects you can view:");
for (ProjectInfo project : resourceManager.list().values()) {
for (Project project : resourceManager.list().values()) {
System.out.println(projectDetails(project));
}
}
Expand All @@ -158,7 +157,7 @@ public String[] getOptionalParams() {
ACTIONS.put("list", new ListAction());
}

private static String projectDetails(ProjectInfo project) {
private static String projectDetails(Project project) {
return new StringBuilder()
.append("{projectId:")
.append(project.projectId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import com.google.gcloud.spi.StorageRpc.Tuple;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.Bucket;
import com.google.gcloud.storage.BucketInfo;
import com.google.gcloud.storage.CopyWriter;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.Storage.ComposeRequest;
Expand Down Expand Up @@ -93,7 +91,7 @@ private abstract static 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 "";
Expand All @@ -103,7 +101,7 @@ protected String params() {
private abstract static class BlobsAction extends StorageAction<BlobId[]> {

@Override
BlobId[] parse(String... args) {
BlobId[] parse(Storage storage, String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -138,33 +136,33 @@ public void run(Storage storage, BlobId... blobIds) {
System.out.println("No such bucket");
return;
}
System.out.println("Bucket info: " + bucket.info());
System.out.println("Bucket info: " + bucket);
} else {
// get Blob
Blob blob = Blob.get(storage, blobIds[0]);
if (blob == null) {
System.out.println("No such object");
return;
}
System.out.println("Blob info: " + blob.info());
System.out.println("Blob info: " + blob);
}
} else {
// use batch to get multiple blobs.
List<Blob> blobs = Blob.get(storage, Arrays.asList(blobIds));
for (Blob blob : blobs) {
if (blob != null) {
System.out.println(blob.info());
System.out.println(blob);
}
}
}
}

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

@Override
Expand Down Expand Up @@ -204,7 +202,7 @@ public void run(Storage storage, BlobId... blobIds) {
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 @@ -218,9 +216,9 @@ String parse(String... args) {
public void run(Storage storage, String bucketName) {
if (bucketName == null) {
// list buckets
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
while (bucketInfoIterator.hasNext()) {
System.out.println(bucketInfoIterator.next());
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
while (bucketIterator.hasNext()) {
System.out.println(bucketIterator.next());
}
} else {
// list a bucket's blobs
Expand All @@ -231,7 +229,7 @@ public void run(Storage storage, String bucketName) {
}
Iterator<Blob> blobIterator = bucket.list().iterateAll();
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next().info());
System.out.println(blobIterator.next());
}
}
}
Expand All @@ -247,17 +245,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 (WriteChannel writer = blob.writer()) {
byte[] buffer = new byte[1024];
try (InputStream input = Files.newInputStream(uploadFrom)) {
Expand All @@ -274,20 +271,20 @@ 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, 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());
return Tuple.of(path, Blob.builder(storage, args[1], blob).contentType(contentType).build());
}

@Override
Expand Down Expand Up @@ -320,7 +317,7 @@ private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOExcep
if (downloadTo != null) {
writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile()));
}
if (blob.info().size() < 1_000_000) {
if (blob.size() < 1_000_000) {
// Blob is small read all its content in one request
byte[] content = blob.content();
writeTo.write(content);
Expand All @@ -344,7 +341,7 @@ private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOExcep
}

@Override
Tuple<BlobId, Path> parse(String... args) {
Tuple<BlobId, Path> parse(Storage storage, String... args) {
if (args.length < 2 || args.length > 3) {
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -379,11 +376,11 @@ 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();
}
return CopyRequest.of(args[0], args[1], BlobId.of(args[2], args[3]));
return CopyRequest.of(storage, args[0], args[1], BlobId.of(args[2], args[3]));
}

@Override
Expand All @@ -400,17 +397,17 @@ public String params() {
private static class ComposeAction extends StorageAction<ComposeRequest> {
@Override
public void run(Storage storage, ComposeRequest request) {
BlobInfo composedBlobInfo = storage.compose(request);
System.out.println("Composed " + composedBlobInfo);
Blob composedBlob = storage.compose(request);
System.out.println("Composed " + composedBlob);
}

@Override
ComposeRequest parse(String... args) {
ComposeRequest parse(Storage storage, String... args) {
if (args.length < 3) {
throw new IllegalArgumentException();
}
ComposeRequest.Builder request = ComposeRequest.builder();
request.target(BlobInfo.builder(args[0], args[args.length - 1]).build());
request.target(Blob.builder(storage, args[0], args[args.length - 1]).build());
for (int i = 1; i < args.length - 1; i++) {
request.addSource(args[i]);
}
Expand Down Expand Up @@ -443,12 +440,12 @@ private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
System.out.println("No such object");
return;
}
Blob updateBlob = blob.update(blob.info().toBuilder().metadata(metadata).build());
System.out.println("Updated " + updateBlob.info());
Blob updateBlob = blob.update(blob.toBuilder().metadata(metadata).build());
System.out.println("Updated " + updateBlob);
}

@Override
Tuple<BlobId, Map<String, String>> parse(String... args) {
Tuple<BlobId, Map<String, String>> parse(Storage storage, String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -478,25 +475,25 @@ 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 {
Blob blob = new Blob(storage, blobInfo);
System.out.println("Signed URL: "
+ blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.serviceAccount(cred)));
}

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

@Override
Expand Down Expand Up @@ -569,7 +566,7 @@ public static void main(String... args) throws Exception {
Storage storage = optionsBuilder.build().service();
Object request;
try {
request = action.parse(args);
request = action.parse(storage, args);
} catch (IllegalArgumentException ex) {
System.out.println("Invalid input for action '" + actionName + "'");
System.out.println("Expected: " + action.params());
Expand Down
Loading