|
59 | 59 |
|
60 | 60 | import java.io.ByteArrayInputStream; |
61 | 61 | import java.io.FileInputStream; |
| 62 | +import java.io.FileOutputStream; |
62 | 63 | import java.io.IOException; |
63 | 64 | import java.io.InputStream; |
| 65 | +import java.io.PrintStream; |
64 | 66 | import java.net.URL; |
65 | 67 | import java.nio.ByteBuffer; |
| 68 | +import java.nio.file.Path; |
66 | 69 | import java.util.HashMap; |
67 | 70 | import java.util.LinkedList; |
68 | 71 | import java.util.List; |
@@ -1042,4 +1045,100 @@ public Page<Bucket> authListBuckets() { |
1042 | 1045 | return buckets; |
1043 | 1046 | } |
1044 | 1047 |
|
| 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 | + } |
1045 | 1144 | } |
0 commit comments