Skip to content

Commit 8f77925

Browse files
authored
Fix requester pays issues (#2464)
Fixes #2392, #2391 and #2439
1 parent 7864376 commit 8f77925

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.api.services.storage.model.StorageObject;
2424
import com.google.auth.ServiceAccountSigner;
2525
import com.google.auth.ServiceAccountSigner.SigningException;
26+
import com.google.cloud.GcpLaunchStage;
2627
import com.google.cloud.ReadChannel;
2728
import com.google.cloud.Tuple;
2829
import 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
*/

google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ enum BucketField implements FieldSelector {
9292
VERSIONING("versioning"),
9393
CORS("cors"),
9494
STORAGE_CLASS("storageClass"),
95-
ETAG("etag");
95+
ETAG("etag"),
96+
@GcpLaunchStage.Alpha
97+
BILLING("billing");
9698

9799
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(NAME);
98100

google-cloud-storage/src/test/java/com/google/cloud/storage/BucketTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ public void testBuilder() {
693693
assertEquals(STORAGE_CLASS, bucket.getStorageClass());
694694
assertEquals(VERSIONING_ENABLED, bucket.versioningEnabled());
695695
assertEquals(BUCKET_LABELS, bucket.getLabels());
696-
assertEquals(VERSIONING_ENABLED, bucket.requesterPays());
696+
assertEquals(REQUESTER_PAYS, bucket.requesterPays());
697697
assertEquals(storage.getOptions(), bucket.getStorage().getOptions());
698698
}
699699
}

0 commit comments

Comments
 (0)