2323import com .google .api .services .storage .model .StorageObject ;
2424import com .google .auth .ServiceAccountSigner ;
2525import com .google .auth .ServiceAccountSigner .SigningException ;
26+ import com .google .cloud .GcpLaunchStage ;
2627import com .google .cloud .ReadChannel ;
2728import com .google .cloud .Tuple ;
2829import com .google .cloud .WriteChannel ;
@@ -104,6 +105,8 @@ private Storage.BlobSourceOption toSourceOptions(BlobInfo blobInfo) {
104105 return Storage .BlobSourceOption .metagenerationNotMatch (blobInfo .getMetageneration ());
105106 case CUSTOMER_SUPPLIED_KEY :
106107 return Storage .BlobSourceOption .decryptionKey ((String ) getValue ());
108+ case USER_PROJECT :
109+ return Storage .BlobSourceOption .userProject ((String ) getValue ());
107110 default :
108111 throw new AssertionError ("Unexpected enum value" );
109112 }
@@ -119,6 +122,8 @@ private Storage.BlobGetOption toGetOption(BlobInfo blobInfo) {
119122 return Storage .BlobGetOption .metagenerationMatch (blobInfo .getMetageneration ());
120123 case IF_METAGENERATION_NOT_MATCH :
121124 return Storage .BlobGetOption .metagenerationNotMatch (blobInfo .getMetageneration ());
125+ case USER_PROJECT :
126+ return Storage .BlobGetOption .userProject ((String ) getValue ());
122127 default :
123128 throw new AssertionError ("Unexpected enum value" );
124129 }
@@ -175,6 +180,15 @@ public static BlobSourceOption decryptionKey(String key) {
175180 return new BlobSourceOption (StorageRpc .Option .CUSTOMER_SUPPLIED_KEY , key );
176181 }
177182
183+ /**
184+ * Returns an option for blob's billing user project. This option is used only if the blob's
185+ * bucket has requester_pays flag enabled.
186+ */
187+ @ GcpLaunchStage .Alpha
188+ public static BlobSourceOption userProject (String userProject ) {
189+ return new BlobSourceOption (StorageRpc .Option .USER_PROJECT , userProject );
190+ }
191+
178192 static Storage .BlobSourceOption [] toSourceOptions (BlobInfo blobInfo ,
179193 BlobSourceOption ... options ) {
180194 Storage .BlobSourceOption [] convertedOptions = new Storage .BlobSourceOption [options .length ];
@@ -196,15 +210,16 @@ static Storage.BlobGetOption[] toGetOptions(BlobInfo blobInfo, BlobSourceOption.
196210 }
197211
198212 /**
199- * Downloads this blob to the given file path.
213+ * Downloads this blob to the given file path using specified blob read options .
200214 *
201215 * @param path destination
216+ * @param options blob read options
202217 * @throws StorageException upon failure
203218 */
204- public void downloadTo (Path path ) throws StorageException {
219+ public void downloadTo (Path path , BlobSourceOption ... options ) {
205220 try (
206221 OutputStream outputStream = Files .newOutputStream (path );
207- ReadChannel reader = reader ()
222+ ReadChannel reader = reader (options )
208223 ) {
209224 WritableByteChannel channel = Channels .newChannel (outputStream );
210225 ByteBuffer bytes = ByteBuffer .allocate (DEFAULT_CHUNK_SIZE );
@@ -218,6 +233,19 @@ public void downloadTo(Path path) throws StorageException {
218233 }
219234 }
220235
236+ /**
237+ * Downloads this blob to the given file path.
238+ *
239+ * This method is replaced with {@link #downloadTo(Path, BlobSourceOption...)}, but is kept here
240+ * for binary compatibility with the older versions of the client library.
241+ *
242+ * @param path destination
243+ * @throws StorageException upon failure
244+ */
245+ public void downloadTo (Path path ) {
246+ downloadTo (path , new BlobSourceOption [0 ]);
247+ }
248+
221249 /**
222250 * Builder for {@code Blob}.
223251 */
0 commit comments