Skip to content

Commit e32f51e

Browse files
committed
Add copyTo(BlobId) method to Blob
1 parent 8b89ba3 commit e32f51e

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

  • gcloud-java-storage/src

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
197197
return new Blob(storage, storage.update(blobInfo, options));
198198
}
199199

200+
/**
201+
* Copies this blob to the specified target. Possibly copying also some of the metadata
202+
* (e.g. content-type).
203+
*
204+
* @param target target blob's id
205+
* @param options source blob options
206+
* @return the copied blob
207+
* @throws StorageException upon failure
208+
*/
209+
public Blob copyTo(BlobId targetBlob, BlobSourceOption... options) {
210+
BlobInfo updatedInfo = info.toBuilder().blobId(targetBlob).build();
211+
CopyRequest copyRequest =
212+
CopyRequest.builder().source(info.bucket(), info.name())
213+
.sourceOptions(convert(info, options)).target(updatedInfo).build();
214+
return new Blob(storage, storage.copy(copyRequest));
215+
}
216+
200217
/**
201218
* Deletes this blob.
202219
*

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ public void testCopyTo() throws Exception {
143143
assertSame(storage, targetBlob.storage());
144144
}
145145

146+
@Test
147+
public void testCopyToBlobId() throws Exception {
148+
BlobId targetId = BlobId.of("bt", "nt");
149+
BlobInfo target = BLOB_INFO.toBuilder().blobId(targetId).build();
150+
Capture<CopyRequest> capturedCopyRequest = Capture.newInstance();
151+
expect(storage.copy(capture(capturedCopyRequest))).andReturn(target);
152+
replay(storage);
153+
Blob targetBlob = blob.copyTo(targetId);
154+
assertEquals(target, targetBlob.info());
155+
assertEquals(capturedCopyRequest.getValue().source(), blob.id());
156+
assertEquals(capturedCopyRequest.getValue().target(), target);
157+
assertSame(storage, targetBlob.storage());
158+
}
159+
146160
@Test
147161
public void testReader() throws Exception {
148162
BlobReadChannel channel = createMock(BlobReadChannel.class);

0 commit comments

Comments
 (0)