Skip to content

Commit ddcaaab

Browse files
authored
Merge pull request #2466 from frankyn/update-requester-samples
[Storage] Update download requester pays sample
2 parents 97d50c2 + 3519c7c commit ddcaaab

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.google.cloud.storage.Storage.BlobSourceOption;
4646
import com.google.cloud.storage.Storage.BlobTargetOption;
4747
import com.google.cloud.storage.Storage.BlobWriteOption;
48+
import com.google.cloud.storage.Storage.BucketField;
4849
import com.google.cloud.storage.Storage.BucketGetOption;
4950
import com.google.cloud.storage.Storage.BucketListOption;
5051
import com.google.cloud.storage.Storage.BucketSourceOption;
@@ -1100,7 +1101,8 @@ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException
11001101
// The name of the bucket to retrieve requester-pays status, eg. "my-bucket"
11011102
// String bucketName = "my-bucket"
11021103
// Retrieve the bucket, throws StorageException on failure
1103-
Bucket bucket = storage.get(bucketName);
1104+
Bucket bucket = storage.get(bucketName,
1105+
Storage.BucketGetOption.fields(BucketField.BILLING));
11041106

11051107
System.out.println("Requester pays status : " + bucket.requesterPays());
11061108
// [END get_requester_pays_status]
@@ -1113,8 +1115,6 @@ public Bucket getRequesterPaysStatus(String bucketName) throws StorageException
11131115
public void downloadFileUsingRequesterPays(String projectId, String bucketName,
11141116
String srcFilename, Path destFilePath) throws IOException {
11151117
// [START storage_download_file_requester_pays]
1116-
// Instantiate a Google Cloud Storage client
1117-
11181118
// The project ID to bill
11191119
// String projectId = "my-billable-project-id";
11201120

@@ -1125,20 +1125,16 @@ public void downloadFileUsingRequesterPays(String projectId, String bucketName,
11251125
// String srcFilename = "file.txt";
11261126

11271127
// The path to which the file should be downloaded
1128-
// String destFilePath = "/local/path/to/file.txt";
1128+
// Path destFilePath = Paths.get("/local/path/to/file.txt");
11291129

11301130
// Instantiate a Google Cloud Storage client
11311131
Storage storage = StorageOptions.getDefaultInstance().getService();
11321132

1133-
BlobSourceOption option = BlobSourceOption.userProject(projectId);
1134-
1135-
// read blob in a single pass
1136-
byte[] readBytes = storage.readAllBytes(bucketName, srcFilename, option);
1133+
// Get specific file from specified bucket
1134+
Blob blob = storage.get(BlobId.of(bucketName, srcFilename));
11371135

1138-
// write out to file
1139-
PrintStream out = new PrintStream(new FileOutputStream(destFilePath.toFile()));
1140-
out.write(readBytes);
1141-
out.close();
1136+
// Download file to specified path
1137+
blob.downloadTo(destFilePath, Blob.BlobSourceOption.userProject(projectId));
11421138
// [END storage_download_file_requester_pays]
11431139
}
11441140
}

0 commit comments

Comments
 (0)