Skip to content

Commit 8b05867

Browse files
feat: fix signed url mismatch in BlobWriteChannel (#915)
* feat: fix signed url mismatch in BlobWriteChannel * remove leftover file
1 parent b0de3d0 commit 8b05867

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java

+4
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,10 @@ public String open(String signedURL) {
956956
HttpHeaders requestHeaders = httpRequest.getHeaders();
957957
requestHeaders.set("X-Upload-Content-Type", "");
958958
requestHeaders.set("x-goog-resumable", "start");
959+
// Using the x-goog-api-client header causes a signature mismatch with signed URLs generated
960+
// outside the Java storage client
961+
requestHeaders.remove("x-goog-api-client");
962+
959963
HttpResponse response = httpRequest.execute();
960964
if (response.getStatusCode() != 201) {
961965
GoogleJsonError error = new GoogleJsonError();

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -3421,15 +3421,23 @@ public void testUploadUsingSignedURL() throws Exception {
34213421
String blobName = "test-signed-url-upload";
34223422
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
34233423
assertNotNull(storage.create(blob));
3424+
Map<String, String> extensionHeaders = new HashMap<>();
3425+
extensionHeaders.put("x-goog-resumable", "start");
34243426
for (Storage.SignUrlOption urlStyle :
34253427
Arrays.asList(
34263428
Storage.SignUrlOption.withPathStyle(),
34273429
Storage.SignUrlOption.withVirtualHostedStyle())) {
34283430
URL signUrl =
34293431
storage.signUrl(
3430-
blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST), urlStyle);
3432+
blob,
3433+
1,
3434+
TimeUnit.HOURS,
3435+
Storage.SignUrlOption.httpMethod(HttpMethod.POST),
3436+
Storage.SignUrlOption.withExtHeaders(extensionHeaders),
3437+
urlStyle);
34313438
byte[] bytesArrayToUpload = BLOB_STRING_CONTENT.getBytes();
3432-
try (WriteChannel writer = storage.writer(signUrl)) {
3439+
Storage unauthenticatedStorage = StorageOptions.getUnauthenticatedInstance().getService();
3440+
try (WriteChannel writer = unauthenticatedStorage.writer(signUrl)) {
34333441
writer.write(ByteBuffer.wrap(bytesArrayToUpload, 0, bytesArrayToUpload.length));
34343442
}
34353443

0 commit comments

Comments
 (0)