Skip to content

Commit

Permalink
Add copyTo(BlobId) method to Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Oct 14, 2015
1 parent 8b89ba3 commit e32f51e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
return new Blob(storage, storage.update(blobInfo, options));
}

/**
* Copies this blob to the specified target. Possibly copying also some of the metadata
* (e.g. content-type).
*
* @param target target blob's id
* @param options source blob options
* @return the copied blob
* @throws StorageException upon failure
*/
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();
return new Blob(storage, storage.copy(copyRequest));
}

/**
* Deletes this blob.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ public void testCopyTo() throws Exception {
assertSame(storage, targetBlob.storage());
}

@Test
public void testCopyToBlobId() throws Exception {
BlobId targetId = BlobId.of("bt", "nt");
BlobInfo target = BLOB_INFO.toBuilder().blobId(targetId).build();
Capture<CopyRequest> capturedCopyRequest = Capture.newInstance();
expect(storage.copy(capture(capturedCopyRequest))).andReturn(target);
replay(storage);
Blob targetBlob = blob.copyTo(targetId);
assertEquals(target, targetBlob.info());
assertEquals(capturedCopyRequest.getValue().source(), blob.id());
assertEquals(capturedCopyRequest.getValue().target(), target);
assertSame(storage, targetBlob.storage());
}

@Test
public void testReader() throws Exception {
BlobReadChannel channel = createMock(BlobReadChannel.class);
Expand Down

0 comments on commit e32f51e

Please sign in to comment.