Skip to content

Commit d1ca2e0

Browse files
author
Jerjou Cheng
committed
Add sample for moving blob.
1 parent f7afed1 commit d1ca2e0

File tree

3 files changed

+61
-1
lines changed
  • google-cloud-examples/src
  • google-cloud-storage/src/main/java/com/google/cloud/storage

3 files changed

+61
-1
lines changed

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/BlobSnippets.java

+19
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ public Blob copyToStrings(String bucketName, String blobName) {
172172
return copiedBlob;
173173
}
174174

175+
/**
176+
* Example of moving a blob to a different bucket with a different name.
177+
*/
178+
// [TARGET copyTo(String, String, BlobSourceOption...)]
179+
// [VARIABLE "my_unique_bucket"]
180+
// [VARIABLE "move_blob_name"]
181+
public Blob moveTo(String destBucket, String destBlob) {
182+
// [START storageMoveFile]
183+
CopyWriter copyWriter = blob.copyTo(destBucket, destBlob);
184+
Blob copiedBlob = copyWriter.getResult();
185+
boolean deleted = blob.delete();
186+
// [END storageMoveFile]
187+
if (deleted) {
188+
return copiedBlob;
189+
} else {
190+
return null;
191+
}
192+
}
193+
175194
/**
176195
* Example of reading the blob's content through a reader.
177196
*/

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBlobSnippets.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
import com.google.cloud.storage.BlobInfo;
3232
import com.google.cloud.storage.BucketInfo;
3333
import com.google.cloud.storage.Storage;
34+
import com.google.cloud.storage.Storage.BlobListOption;
3435
import com.google.cloud.storage.StorageException;
3536
import com.google.cloud.storage.testing.RemoteStorageHelper;
3637
import com.google.common.collect.ImmutableMap;
3738
import com.google.common.collect.Sets;
3839

40+
import org.junit.After;
3941
import org.junit.AfterClass;
42+
import org.junit.Before;
4043
import org.junit.BeforeClass;
4144
import org.junit.Rule;
4245
import org.junit.Test;
@@ -46,6 +49,7 @@
4649
import java.io.InputStream;
4750
import java.net.URL;
4851
import java.net.URLConnection;
52+
import java.util.Iterator;
4953
import java.util.Set;
5054
import java.util.concurrent.ExecutionException;
5155
import java.util.concurrent.TimeUnit;
@@ -71,7 +75,6 @@ public static void beforeClass() {
7175
RemoteStorageHelper helper = RemoteStorageHelper.create();
7276
storage = helper.getOptions().getService();
7377
storage.create(BucketInfo.of(BUCKET));
74-
blob = storage.create(BlobInfo.newBuilder(BUCKET, BLOB).build());
7578
}
7679

7780
@AfterClass
@@ -84,6 +87,18 @@ public static void afterClass() throws ExecutionException, InterruptedException
8487
}
8588
}
8689

90+
@Before
91+
public void before() {
92+
blob = storage.create(BlobInfo.newBuilder(BUCKET, BLOB).build());
93+
}
94+
95+
@After
96+
public void after() {
97+
for (BlobInfo info : storage.list(BUCKET, BlobListOption.versions(true)).getValues()) {
98+
storage.delete(info.getBlobId());
99+
}
100+
}
101+
87102
@Test
88103
public void testBlob() throws IOException {
89104
BlobSnippets blobSnippets = new BlobSnippets(blob);
@@ -137,4 +152,21 @@ public void testBlob() throws IOException {
137152
assertNull(blobSnippets.getAcl());
138153
storage.delete(BlobId.of(BUCKET, BLOB));
139154
}
155+
156+
@Test
157+
public void testMoveBlob() throws IOException {
158+
BlobSnippets blobSnippets = new BlobSnippets(blob);
159+
160+
Blob movedBlob = blobSnippets.moveTo(BUCKET, "moveBlob");
161+
assertNotNull(movedBlob);
162+
163+
// Assert that the destination blob exists
164+
Iterator<Blob> blobs = storage.list(BUCKET).iterateAll();
165+
Blob moveBlob = blobs.next();
166+
assertEquals(BUCKET, moveBlob.getBucket());
167+
assertEquals("moveBlob", moveBlob.getName());
168+
169+
// Assert that the old blob doesn't exist
170+
assertFalse(blobs.hasNext());
171+
}
140172
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,15 @@ public CopyWriter copyTo(String targetBucket, BlobSourceOption... options) {
591591
* Blob copiedBlob = copyWriter.getResult();
592592
* }</pre>
593593
*
594+
* <p>Example of moving a blob to a different bucket with a different name.
595+
* <pre> {@code
596+
* String destBucket = "my_unique_bucket";
597+
* String destBlob = "move_blob_name";
598+
* CopyWriter copyWriter = blob.copyTo(destBucket, destBlob);
599+
* Blob copiedBlob = copyWriter.getResult();
600+
* boolean deleted = blob.delete();
601+
* }</pre>
602+
*
594603
* @param targetBucket target bucket's name
595604
* @param targetBlob target blob's name
596605
* @param options source blob options

0 commit comments

Comments
 (0)