Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ public Blob copyToStrings(String bucketName, String blobName) {
return copiedBlob;
}

/**
* Example of moving a blob to a different bucket with a different name.
*/
// [TARGET copyTo(String, String, BlobSourceOption...)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "move_blob_name"]
public Blob moveTo(String destBucket, String destBlob) {
// [START storageMoveFile]
CopyWriter copyWriter = blob.copyTo(destBucket, destBlob);
Blob copiedBlob = copyWriter.getResult();
boolean deleted = blob.delete();
// [END storageMoveFile]
if (deleted) {
return copiedBlob;
} else {
return null;
}
}

/**
* Example of reading the blob's content through a reader.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,21 @@ public Acl updateBlobAcl(String bucketName, String blobName, long blobGeneration
return acl;
}

/**
* Example of updating a blob to be public-read.
*/
// [TARGET createAcl(BlobId, Acl)]
// [VARIABLE "my_unique_bucket"]
// [VARIABLE "my_blob_name"]
// [VARIABLE 42]
public Acl blobToPublicRead(String bucketName, String blobName, long blobGeneration) {
// [START storageMakePublic]
BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
// [END storageMakePublic]
return acl;
}

/**
* Example of listing the ACL entries for a blob.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobListOption;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -46,6 +49,7 @@
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand All @@ -71,7 +75,6 @@ public static void beforeClass() {
RemoteStorageHelper helper = RemoteStorageHelper.create();
storage = helper.getOptions().getService();
storage.create(BucketInfo.of(BUCKET));
blob = storage.create(BlobInfo.newBuilder(BUCKET, BLOB).build());
}

@AfterClass
Expand All @@ -84,6 +87,18 @@ public static void afterClass() throws ExecutionException, InterruptedException
}
}

@Before
public void before() {
blob = storage.create(BlobInfo.newBuilder(BUCKET, BLOB).build());
}

@After
public void after() {
for (BlobInfo info : storage.list(BUCKET, BlobListOption.versions(true)).getValues()) {
storage.delete(info.getBlobId());
}
}

@Test
public void testBlob() throws IOException {
BlobSnippets blobSnippets = new BlobSnippets(blob);
Expand Down Expand Up @@ -137,4 +152,21 @@ public void testBlob() throws IOException {
assertNull(blobSnippets.getAcl());
storage.delete(BlobId.of(BUCKET, BLOB));
}

@Test
public void testMoveBlob() throws IOException {
BlobSnippets blobSnippets = new BlobSnippets(blob);

Blob movedBlob = blobSnippets.moveTo(BUCKET, "moveBlob");
assertNotNull(movedBlob);

// Assert that the destination blob exists
Iterator<Blob> blobs = storage.list(BUCKET).iterateAll();
Blob moveBlob = blobs.next();
assertEquals(BUCKET, moveBlob.getBucket());
assertEquals("moveBlob", moveBlob.getName());

// Assert that the old blob doesn't exist
assertFalse(blobs.hasNext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.google.cloud.Page;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.User;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
Expand Down Expand Up @@ -319,6 +320,14 @@ public void testBlobAcl() {
Set<Acl> acls = Sets.newHashSet(
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(acls.contains(updatedAcl));

updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
assertEquals(Acl.Role.READER, updatedAcl.getRole());
assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
acls = Sets.newHashSet(
storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(acls.contains(updatedAcl));

assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
// test non-existing blob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,15 @@ public CopyWriter copyTo(String targetBucket, BlobSourceOption... options) {
* Blob copiedBlob = copyWriter.getResult();
* }</pre>
*
* <p>Example of moving a blob to a different bucket with a different name.
* <pre> {@code
* String destBucket = "my_unique_bucket";
* String destBlob = "move_blob_name";
* CopyWriter copyWriter = blob.copyTo(destBucket, destBlob);
* Blob copiedBlob = copyWriter.getResult();
* boolean deleted = blob.delete();
* }</pre>
*
* @param targetBucket target bucket's name
* @param targetBlob target blob's name
* @param options source blob options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,15 @@ public static Builder newBuilder() {
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllAuthenticatedUsers(), Role.READER));
* }</pre>
*
* <p>Example of updating a blob to be public-read.
* <pre> {@code
* String bucketName = "my_unique_bucket";
* String blobName = "my_blob_name";
* long blobGeneration = 42;
* BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
* Acl acl = storage.createAcl(blobId, Acl.of(User.ofAllUsers(), Role.READER));
* }</pre>
*
* @throws StorageException upon failure
*/
Acl createAcl(BlobId blob, Acl acl);
Expand Down