4545import com .google .cloud .storage .Storage .BlobSourceOption ;
4646import com .google .cloud .storage .Storage .BlobTargetOption ;
4747import com .google .cloud .storage .Storage .BlobWriteOption ;
48+ import com .google .cloud .storage .Storage .BucketField ;
4849import com .google .cloud .storage .Storage .BucketGetOption ;
4950import com .google .cloud .storage .Storage .BucketListOption ;
5051import 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