Skip to content

Commit 57738da

Browse files
jerjoumziccard
authored andcommitted
Add snippets for moving a blob and making a blob public (#1384)
1 parent 8ec721e commit 57738da

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-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/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

+15
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,21 @@ public Acl updateBlobAcl(String bucketName, String blobName, long blobGeneration
908908
return acl;
909909
}
910910

911+
/**
912+
* Example of updating a blob to be public-read.
913+
*/
914+
// [TARGET createAcl(BlobId, Acl)]
915+
// [VARIABLE "my_unique_bucket"]
916+
// [VARIABLE "my_blob_name"]
917+
// [VARIABLE 42]
918+
public Acl blobToPublicRead(String bucketName, String blobName, long blobGeneration) {
919+
// [START storageMakePublic]
920+
BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
921+
Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
922+
// [END storageMakePublic]
923+
return acl;
924+
}
925+
911926
/**
912927
* Example of listing the ACL entries for a blob.
913928
*/

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-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.google.cloud.Page;
2929
import com.google.cloud.storage.Acl;
30+
import com.google.cloud.storage.Acl.User;
3031
import com.google.cloud.storage.Blob;
3132
import com.google.cloud.storage.BlobId;
3233
import com.google.cloud.storage.BlobInfo;
@@ -319,6 +320,14 @@ public void testBlobAcl() {
319320
Set<Acl> acls = Sets.newHashSet(
320321
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
321322
assertTrue(acls.contains(updatedAcl));
323+
324+
updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
325+
assertEquals(Acl.Role.READER, updatedAcl.getRole());
326+
assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
327+
acls = Sets.newHashSet(
328+
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
329+
assertTrue(acls.contains(updatedAcl));
330+
322331
assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
323332
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
324333
// test non-existing blob

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

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

+9
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,15 @@ public static Builder newBuilder() {
24562456
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER));
24572457
* }</pre>
24582458
*
2459+
* <p>Example of updating a blob to be public-read.
2460+
* <pre> {@code
2461+
* String bucketName = "my_unique_bucket";
2462+
* String blobName = "my_blob_name";
2463+
* long blobGeneration = 42;
2464+
* BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
2465+
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
2466+
* }</pre>
2467+
*
24592468
* @throws StorageException upon failure
24602469
*/
24612470
Acl createAcl(BlobId blob, Acl acl);

0 commit comments

Comments
 (0)