Skip to content

Commit

Permalink
Blob and Bucket extended with more functionalities, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Sep 22, 2015
1 parent d8fa7bb commit 44cefb0
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
Expand Down Expand Up @@ -31,7 +30,6 @@
import java.net.URL;
import java.util.Objects;


/**
* A Google cloud storage object.
*/
Expand Down Expand Up @@ -89,38 +87,68 @@ static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption...
}
}

/**
* Construct a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used
* to issue requests.
*
* @param storage the storage service used for issuing requests
* @param info blob's info
*/
public Blob(Storage storage, BlobInfo info) {
this.storage = checkNotNull(storage);
this.info = checkNotNull(info);
}

/**
* Construct a {@code Blob} object for the provided bucket and blob names. The storage service is
* used to issue requests.
*
* @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.of(bucket, blob);
}

/**
* Get the blobs's information.
*
* @return a {@code BlobInfo} object for this blob
*/
public BlobInfo info() {
return info;
}

/**
* Returns true if this blob exists.
* Check if this blob exists.
*
* @return true if this blob exists, false otherwise
* @throws StorageException upon failure
*/
public boolean exists() {
return storage.get(info.bucket(), info.name()) != null;
}

/**
* Returns the blob's content.
* Return this blob's content.
*
* @param options blob read options
* @return the blob's content
* @throws StorageException upon failure
*/
public byte[] content(Storage.BlobSourceOption... options) {
return storage.readAllBytes(info.bucket(), info.name(), options);
}

/**
* Updates the blob's information. Bucket or blob's name cannot be changed by this method. If you
* Update the blob's information. Bucket or blob's name cannot be changed by this method. If you
* want to rename the blob or move it to a different bucket use the {@link #copyTo} and
* {@link #delete} operations.
*
* @param blobInfo new blob's information. Bucket and blob names must match the current ones
* @param options update options
* @throws StorageException upon failure
*/
public void update(BlobInfo blobInfo, BlobTargetOption... options) {
Expand All @@ -130,29 +158,35 @@ public void update(BlobInfo blobInfo, BlobTargetOption... options) {
}

/**
* Deletes this blob.
* Delete this blob.
*
* @return true if bucket was deleted
* @param options blob delete options
* @return true if blob was deleted
* @throws StorageException upon failure
*/
public boolean delete(BlobSourceOption... options) {
return storage.delete(info.bucket(), info.name(), convert(info, options));
}

/**
* Send a copy request.
* Copy this blob.
*
* @return the copied blob.
* @param target target blob
* @param options source blob options
* @return the copied blob
* @throws StorageException upon failure
*/
public Blob copyTo(BlobInfo target, BlobSourceOption... options) {
return copyTo(target, ImmutableList.copyOf(options), ImmutableList.<BlobTargetOption>of());
}

/**
* Send a copy request.
* Copy this blob.
*
* @return the copied blob.
* @param target target blob
* @param sourceOptions source blob options
* @param targetOptions target blob options
* @return the copied blob
* @throws StorageException upon failure
*/
public Blob copyTo(BlobInfo target, Iterable<BlobSourceOption> sourceOptions,
Expand All @@ -165,36 +199,47 @@ public Blob copyTo(BlobInfo target, Iterable<BlobSourceOption> sourceOptions,
}

/**
* Returns a channel for reading this blob's content.
* Return a channel for reading this blob's content.
*
* @param options blob read options
* @return a {@code BlobReadChannel} object to read this blob
* @throws StorageException upon failure
*/
public BlobReadChannel reader(BlobSourceOption... options) {
return storage.reader(info.bucket(), info.name(), convert(info, options));
}

/**
* Returns a channel for writing to this blob.
* Return a channel for writing to this blob.
*
* @param options target blob options
* @return a {@code BlobWriteChannel} object for writing to this blob
* @throws StorageException upon failure
*/
public BlobWriteChannel writer(BlobTargetOption... options) {
return storage.writer(info, options);
}

/**
* Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time
* Generate a signed URL for this blob. If you want to allow access to for a fixed amount of time
* for this blob, you can use this method to generate a URL that is only valid within a certain
* time period. This is particularly useful if you don't want publicly accessible blobs, but don't
* want to require users to explicitly log in.
*
* @param expirationTimeInSeconds the signed URL expiration (using epoch time)
* @param options signed url options
* @return a signed URL for this bucket and the specified options
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a>
*/
public URL signUrl(long expirationTimeInSeconds, SignUrlOption... options) {
return storage.signUrl(info, expirationTimeInSeconds, options);
}

/**
* Get this blobs's {@code Storage} object.
*
* @return the storage service used by this blob to issue requests
*/
public Storage storage() {
return storage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.gcloud.storage.Storage.BlobSourceOption;
import com.google.gcloud.storage.Storage.BlobTargetOption;
import com.google.gcloud.storage.Storage.BucketSourceOption;
import com.google.gcloud.storage.Storage.BucketTargetOption;
import java.util.LinkedList;

import java.util.List;
import java.util.Objects;
Expand All @@ -34,14 +36,43 @@ public final class Bucket {
private final Storage storage;
private BucketInfo info;

/**
* Construct a {@code Bucket} object for the provided {@code BucketInfo}. The storage service is
* used to issue requests.
*
* @param storage the storage service used for issuing requests
* @param info bucket's info
*/
public Bucket(Storage storage, BucketInfo info) {
this.storage = checkNotNull(storage);
this.info = checkNotNull(info);
}

/**
* Returns true if this bucket exists.
* Construct a {@code Bucket} object for the provided name. The storage service is used to issue
* requests.
*
* @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(bucket);
}

/**
* Get the bucket's information.
*
* @return a {@code BucketInfo} object for this bucket
*/
public BucketInfo info() {
return info;
}

/**
* Check if this bucket exists.
*
* @return true if this bucket exists, false otherwise
* @throws StorageException upon failure
*/
public boolean exists() {
Expand All @@ -51,6 +82,8 @@ public boolean exists() {
/**
* Update the bucket's information. Bucket's name cannot be changed.
*
* @param bucketInfo new bucket's information. Name must match the one of the current bucket
* @param options update options
* @throws StorageException upon failure
*/
public void update(BucketInfo bucketInfo, BucketTargetOption... options) {
Expand All @@ -61,33 +94,77 @@ public void update(BucketInfo bucketInfo, BucketTargetOption... options) {
/**
* Delete this bucket.
*
* @param options bucket delete options
* @return true if bucket was deleted
* @throws StorageException upon failure
*/
public boolean delete(BucketSourceOption... options) {
return storage.delete(info.name(), options);
}

public ListResult<BlobInfo> list(Storage.BlobListOption... options) {
return storage.list(info.name(), options);
/**
* List blobs in this bucket.
*
* @param options options for listing blobs
* @return the paginated list of {@code Blob} objects in this bucket
* @throws StorageException upon failure
*/
public ListResult<Blob> list(Storage.BlobListOption... options) {
return new BlobListResult(storage, storage.list(info.name(), options));
}

public BlobInfo get(String blob, BlobSourceOption... options) {
return null;
/**
* Return the requested blob in this bucket or {@code null} if not found.
*
* @param blob name of the requested blob
* @param options blob search options
* @return the requested blob in this bucket or {@code null} if not found
* @throws StorageException upon failure
*/
public Blob get(String blob, BlobSourceOption... options) {
return new Blob(storage, storage.get(info.name(), blob, options));
}

public List<Blob> get(String... blob) {
// todo
return null;
/**
* Return a list of requested blobs in this bucket. Blobs that do not exist are null.
*
* @param blobNames names of the requested blobs
* @return a list containing the requested blobs in this bucket
* @throws StorageException upon failure
*/
public List<Blob> getAll(String... blobNames) {
BatchRequest.Builder batch = BatchRequest.builder();
for (String blobName : blobNames) {
batch.get(info.name(), blobName);
}
List<Blob> blobs = new LinkedList<>();
BatchResponse response = storage.apply(batch.build());
for (BatchResponse.Result<BlobInfo> result : response.gets()) {
BlobInfo blobInfo = result.get();
blobs.add(blobInfo != null ? new Blob(storage, blobInfo) : null);
}
return blobs;
}

/*
* BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options) {
*
* }
/**
* Create a new blob in this bucket.
*
* @param blob a blob name
* @param content the blob content
* @param options options for blob creation
* @return a complete blob information.
* @throws StorageException upon failure
*/
Blob create(String blob, byte[] content, BlobTargetOption... options) {
BlobInfo blobInfo = BlobInfo.of(info.name(), blob);
return new Blob(storage, storage.create(blobInfo, content, options));
}


/**
* Get this bucket's {@code Storage} object.
*
* @return the storage service used by this bucket to issue requests
*/
public Storage storage() {
return storage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class BlobTest {

private Storage storage;
private Blob blob;
private BlobInfo blobInfo = BlobInfo.of("b", "n");
private final BlobInfo blobInfo = BlobInfo.of("b", "n");

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -107,6 +107,9 @@ public void testCopyTo() throws Exception {
replay(storage);
Blob targetBlob = blob.copyTo(target);
assertEquals(target, targetBlob.info());
assertEquals(capturedCopyRequest.getValue().sourceBlob(), blob.info().name());
assertEquals(capturedCopyRequest.getValue().sourceBucket(), blob.info().bucket());
assertEquals(capturedCopyRequest.getValue().target(), target);
assertSame(storage, targetBlob.storage());
}

Expand Down
Loading

0 comments on commit 44cefb0

Please sign in to comment.