File tree Expand file tree Collapse file tree
main/java/com/google/gcloud/storage
test/java/com/google/gcloud/storage Expand file tree Collapse file tree Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments