Skip to content

Commit cf71a20

Browse files
authored
adding RequesterPays snippets (googleapis#2393)
1 parent 0a70b16 commit cf71a20

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@
5959

6060
import java.io.ByteArrayInputStream;
6161
import java.io.FileInputStream;
62+
import java.io.FileOutputStream;
6263
import java.io.IOException;
6364
import java.io.InputStream;
65+
import java.io.PrintStream;
6466
import java.net.URL;
6567
import java.nio.ByteBuffer;
68+
import java.nio.file.Path;
6669
import java.util.HashMap;
6770
import java.util.LinkedList;
6871
import java.util.List;
@@ -1042,4 +1045,100 @@ public Page<Bucket> authListBuckets() {
10421045
return buckets;
10431046
}
10441047

1048+
/**
1049+
* Example of enabling Requester pays on a bucket.
1050+
*/
1051+
public Bucket enableRequesterPays(String bucketName) throws StorageException {
1052+
// [START enable_requester_pays]
1053+
// Instantiate a Google Cloud Storage client
1054+
Storage storage = StorageOptions.getDefaultInstance().getService();
1055+
1056+
// The name of the existing bucket to enable requester-paying for, e.g. "my-bucket"
1057+
// String bucketName = "my-bucket"
1058+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName)
1059+
.setRequesterPays(true)
1060+
.build();
1061+
1062+
// Update the bucket, throws StorageException on failure
1063+
Bucket bucket = storage.update(bucketInfo);
1064+
1065+
System.out.println("Requester pay status for " + bucketName +": " + bucket.requesterPays());
1066+
// [END enable_requester_pays]
1067+
return bucket;
1068+
}
1069+
1070+
/**
1071+
* Example of disabling Requester pays on a bucket.
1072+
*/
1073+
public Bucket disableRequesterPays(String bucketName) {
1074+
// [START disable_requester_pays]
1075+
// Instantiate a Google Cloud Storage client
1076+
Storage storage = StorageOptions.getDefaultInstance().getService();
1077+
1078+
// The name of the bucket to disable requester-paying for, e.g. "my-bucket"
1079+
// String bucketName = "my-bucket"
1080+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName)
1081+
.setRequesterPays(false)
1082+
.build();
1083+
1084+
// Update the bucket, throws StorageException on failure
1085+
Bucket bucket = storage.update(bucketInfo);
1086+
1087+
System.out.println("Requester pays status for " + bucketName +": " + bucket.requesterPays());
1088+
// [END disable_requester_pays]
1089+
return bucket;
1090+
}
1091+
1092+
/**
1093+
* Example of retrieving Requester pays status on a bucket.
1094+
*/
1095+
public Bucket getRequesterPaysStatus(String bucketName) throws StorageException {
1096+
// [START get_requester_pays_status]
1097+
// Instantiate a Google Cloud Storage client
1098+
Storage storage = StorageOptions.getDefaultInstance().getService();
1099+
1100+
// The name of the bucket to retrieve requester-pays status, eg. "my-bucket"
1101+
// String bucketName = "my-bucket"
1102+
// Retrieve the bucket, throws StorageException on failure
1103+
Bucket bucket = storage.get(bucketName);
1104+
1105+
System.out.println("Requester pays status : " + bucket.requesterPays());
1106+
// [END get_requester_pays_status]
1107+
return bucket;
1108+
}
1109+
1110+
/**
1111+
* Example of downloading a file using Requester pay.
1112+
*/
1113+
public void downloadFileUsingRequesterPays(String projectId, String bucketName,
1114+
String srcFilename, Path destFilePath) throws IOException {
1115+
// [START storage_download_file_requester_pays]
1116+
// Instantiate a Google Cloud Storage client
1117+
1118+
// The project ID to bill
1119+
// String projectId = "my-billable-project-id";
1120+
1121+
// The name of the bucket to access
1122+
// String bucketName = "my-bucket";
1123+
1124+
// The name of the remote file to download
1125+
// String srcFilename = "file.txt";
1126+
1127+
// The path to which the file should be downloaded
1128+
// String destFilePath = "/local/path/to/file.txt";
1129+
1130+
// Instantiate a Google Cloud Storage client
1131+
Storage storage = StorageOptions.getDefaultInstance().getService();
1132+
1133+
BlobSourceOption option = BlobSourceOption.userProject(projectId);
1134+
1135+
// read blob in a single pass
1136+
byte[] readBytes = storage.readAllBytes(bucketName, srcFilename, option);
1137+
1138+
// write out to file
1139+
PrintStream out = new PrintStream(new FileOutputStream(destFilePath.toFile()));
1140+
out.write(readBytes);
1141+
out.close();
1142+
// [END storage_download_file_requester_pays]
1143+
}
10451144
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626
import static org.junit.Assert.fail;
2727

2828
import com.google.api.gax.paging.Page;
29+
import com.google.cloud.ServiceOptions;
2930
import com.google.cloud.storage.Acl;
3031
import com.google.cloud.storage.Acl.Role;
3132
import com.google.cloud.storage.Acl.User;
3233
import com.google.cloud.storage.Blob;
3334
import com.google.cloud.storage.BlobId;
3435
import com.google.cloud.storage.BlobInfo;
3536
import com.google.cloud.storage.Bucket;
37+
import com.google.cloud.storage.Bucket.BlobTargetOption;
3638
import com.google.cloud.storage.Storage;
3739
import com.google.cloud.storage.StorageException;
3840
import com.google.cloud.storage.testing.RemoteStorageHelper;
@@ -50,6 +52,8 @@
5052
import java.io.InputStream;
5153
import java.net.URL;
5254
import java.net.URLConnection;
55+
import java.nio.file.Files;
56+
import java.nio.file.Paths;
5357
import java.util.ArrayList;
5458
import java.util.HashSet;
5559
import java.util.Iterator;
@@ -64,6 +68,7 @@ public class ITStorageSnippets {
6468

6569
private static final Logger log = Logger.getLogger(ITStorageSnippets.class.getName());
6670
private static final String BUCKET = RemoteStorageHelper.generateBucketName();
71+
private static final byte[] BLOB_BYTE_CONTENT = {0xD, 0xE, 0xA, 0xD};
6772
private static final String USER_EMAIL = "google-cloud-java-tests@"
6873
+ "java-docs-samples-tests.iam.gserviceaccount.com";
6974

@@ -405,4 +410,23 @@ public void testAuthListBuckets() {
405410
Page<Bucket> bucket = storageSnippets.authListBuckets();
406411
assertNotNull(bucket);
407412
}
413+
414+
@Test
415+
public void testRequesterPays() throws Exception {
416+
Bucket bucket = storageSnippets.enableRequesterPays(BUCKET);
417+
assertTrue(bucket.requesterPays());
418+
bucket = storageSnippets.getRequesterPaysStatus(BUCKET);
419+
assertTrue(bucket.requesterPays());
420+
String projectId = ServiceOptions.getDefaultProjectId();
421+
String blobName = "test-create-empty-blob-requester-pays";
422+
Blob remoteBlob = bucket.create(blobName, BLOB_BYTE_CONTENT,
423+
BlobTargetOption.userProject(projectId));
424+
assertNotNull(remoteBlob);
425+
storageSnippets.downloadFileUsingRequesterPays(projectId, BUCKET, blobName,
426+
Paths.get(blobName));
427+
byte[] readBytes = Files.readAllBytes(Paths.get(blobName));
428+
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
429+
bucket = storageSnippets.disableRequesterPays(BUCKET);
430+
assertFalse(bucket.requesterPays());
431+
}
408432
}

0 commit comments

Comments
 (0)