Skip to content

Commit

Permalink
Make functional objects subclasses of metadata objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Feb 3, 2016
1 parent d3abf25 commit 58b60e5
Show file tree
Hide file tree
Showing 18 changed files with 1,203 additions and 908 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Here is a code snippet showing a simple usage example from within Compute/App En
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
Expand All @@ -257,7 +258,7 @@ import java.nio.channels.WritableByteChannel;

Storage storage = StorageOptions.defaultInstance().service();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = Blob.get(storage, blobId);
Blob blob = storage.get(storage, blobId);
if (blob == null) {
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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 @@ -138,22 +137,22 @@ 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]);
Blob blob = storage.get(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));
List<Blob> blobs = storage.get(blobIds);
for (Blob blob : blobs) {
if (blob != null) {
System.out.println(blob.info());
System.out.println(blob);
}
}
}
Expand Down Expand Up @@ -184,7 +183,7 @@ private static class DeleteAction extends BlobsAction {
@Override
public void run(Storage storage, BlobId... blobIds) {
// use batch operation
List<Boolean> deleteResults = Blob.delete(storage, blobIds);
List<Boolean> deleteResults = storage.delete(blobIds);
int index = 0;
for (Boolean deleted : deleteResults) {
if (deleted) {
Expand Down Expand Up @@ -218,9 +217,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 +230,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 @@ -257,8 +256,7 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
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()) {
try (WriteChannel writer = storage.writer(blobInfo)) {
byte[] buffer = new byte[1024];
try (InputStream input = Files.newInputStream(uploadFrom)) {
int limit;
Expand Down Expand Up @@ -311,7 +309,7 @@ public void run(Storage storage, Tuple<BlobId, Path> tuple) throws IOException {
}

private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOException {
Blob blob = Blob.get(storage, blobId);
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("No such object");
return;
Expand All @@ -320,7 +318,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 Down Expand Up @@ -438,13 +436,13 @@ public void run(Storage storage, Tuple<BlobId, Map<String, String>> tuple)
}

private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
Blob blob = Blob.get(storage, blobId);
Blob blob = storage.get(blobId);
if (blob == null) {
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.toBuilder().metadata(metadata).build().update();
System.out.println("Updated " + updateBlob);
}

@Override
Expand Down Expand Up @@ -488,9 +486,8 @@ public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo>
run(storage, tuple.x(), tuple.y());
}

private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo)
throws IOException {
Blob blob = new Blob(storage, blobInfo);
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo) {
Blob blob = storage.get(blobInfo.blobId());
System.out.println("Signed URL: "
+ blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.serviceAccount(cred)));
}
Expand Down
30 changes: 17 additions & 13 deletions gcloud-java-storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,17 @@ Storage storage = StorageOptions.defaultInstance().service();
For other authentication options, see the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page.

#### Storing data
Stored objects are called "blobs" in `gcloud-java` and are organized into containers called "buckets". In this code snippet, we will create a new bucket and upload a blob to that bucket.
Stored objects are called "blobs" in `gcloud-java` and are organized into containers called "buckets". `Blob`, a subclass of `BlobInfo`, adds a layer of service-related functionality over `BlobInfo`. Similarly, `Bucket` adds a layer of service-related functionality over `BucketInfo`. In this code snippet, we will create a new bucket and upload a blob to that bucket.

Add the following imports at the top of your file:

```java
import static java.nio.charset.StandardCharsets.UTF_8;

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;
```

Expand All @@ -96,11 +98,11 @@ Then add the following code to create a bucket and upload a simple blob.
```java
// Create a bucket
String bucketName = "my_unique_bucket"; // Change this to something unique
BucketInfo bucketInfo = storage.create(BucketInfo.of(bucketName));
Bucket bucket = storage.create(BucketInfo.of(bucketName));

// Upload a blob to the newly created bucket
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
BlobInfo blobInfo = storage.create(
Blob blob = storage.create(
BlobInfo.builder(blobId).contentType("text/plain").build(),
"a simple blob".getBytes(UTF_8));
```
Expand All @@ -125,14 +127,14 @@ Then add the following code to list all your buckets and all the blobs inside yo

```java
// List all your buckets
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
System.out.println("My buckets:");
while (bucketInfoIterator.hasNext()) {
System.out.println(bucketInfoIterator.next());
while (bucketIterator.hasNext()) {
System.out.println(bucketIterator.next());
}

// List the blobs in a particular bucket
Iterator<BlobInfo> blobIterator = storage.list(bucketName).iterateAll();
Iterator<Blob> blobIterator = storage.list(bucketName).iterateAll();
System.out.println("My blobs:");
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next());
Expand All @@ -146,8 +148,10 @@ Here we put together all the code shown above into one program. This program as
```java
import static java.nio.charset.StandardCharsets.UTF_8;

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.Storage;
import com.google.gcloud.storage.StorageOptions;
Expand All @@ -163,26 +167,26 @@ public class GcloudStorageExample {

// Create a bucket
String bucketName = "my_unique_bucket"; // Change this to something unique
BucketInfo bucketInfo = storage.create(BucketInfo.of(bucketName));
Bucket bucket = storage.create(BucketInfo.of(bucketName));

// Upload a blob to the newly created bucket
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
BlobInfo blobInfo = storage.create(
Blob blob = storage.create(
BlobInfo.builder(blobId).contentType("text/plain").build(),
"a simple blob".getBytes(UTF_8));

// Retrieve a blob from the server
String blobContent = new String(storage.readAllBytes(blobId), UTF_8);

// List all your buckets
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
System.out.println("My buckets:");
while (bucketInfoIterator.hasNext()) {
System.out.println(bucketInfoIterator.next());
while (bucketIterator.hasNext()) {
System.out.println(bucketIterator.next());
}

// List the blobs in a particular bucket
Iterator<BlobInfo> blobIterator = storage.list(bucketName).iterateAll();
Iterator<Blob> blobIterator = storage.list(bucketName).iterateAll();
System.out.println("My blobs:");
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public final class BatchResponse implements Serializable {
private static final long serialVersionUID = 1057416839397037706L;

private final List<Result<Boolean>> deleteResult;
private final List<Result<BlobInfo>> updateResult;
private final List<Result<BlobInfo>> getResult;
private final List<Result<Blob>> updateResult;
private final List<Result<Blob>> getResult;

public static class Result<T extends Serializable> implements Serializable {

Expand Down Expand Up @@ -113,8 +113,8 @@ static <T extends Serializable> Result<T> empty() {
}
}

BatchResponse(List<Result<Boolean>> deleteResult, List<Result<BlobInfo>> updateResult,
List<Result<BlobInfo>> getResult) {
BatchResponse(List<Result<Boolean>> deleteResult, List<Result<Blob>> updateResult,
List<Result<Blob>> getResult) {
this.deleteResult = ImmutableList.copyOf(deleteResult);
this.updateResult = ImmutableList.copyOf(updateResult);
this.getResult = ImmutableList.copyOf(getResult);
Expand Down Expand Up @@ -146,14 +146,14 @@ public List<Result<Boolean>> deletes() {
/**
* Returns the results for the update operations using the request order.
*/
public List<Result<BlobInfo>> updates() {
public List<Result<Blob>> updates() {
return updateResult;
}

/**
* Returns the results for the get operations using the request order.
*/
public List<Result<BlobInfo>> gets() {
public List<Result<Blob>> gets() {
return getResult;
}
}
Loading

0 comments on commit 58b60e5

Please sign in to comment.