Skip to content

Commit

Permalink
feat: fix signed url mismatch in BlobWriteChannel (#915)
Browse files Browse the repository at this point in the history
* feat: fix signed url mismatch in BlobWriteChannel

* remove leftover file
  • Loading branch information
JesseLovelace committed Jul 12, 2021
1 parent b0de3d0 commit 8b05867
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Expand Up @@ -956,6 +956,10 @@ public String open(String signedURL) {
HttpHeaders requestHeaders = httpRequest.getHeaders();
requestHeaders.set("X-Upload-Content-Type", "");
requestHeaders.set("x-goog-resumable", "start");
// Using the x-goog-api-client header causes a signature mismatch with signed URLs generated
// outside the Java storage client
requestHeaders.remove("x-goog-api-client");

HttpResponse response = httpRequest.execute();
if (response.getStatusCode() != 201) {
GoogleJsonError error = new GoogleJsonError();
Expand Down
Expand Up @@ -3421,15 +3421,23 @@ public void testUploadUsingSignedURL() throws Exception {
String blobName = "test-signed-url-upload";
BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
assertNotNull(storage.create(blob));
Map<String, String> extensionHeaders = new HashMap<>();
extensionHeaders.put("x-goog-resumable", "start");
for (Storage.SignUrlOption urlStyle :
Arrays.asList(
Storage.SignUrlOption.withPathStyle(),
Storage.SignUrlOption.withVirtualHostedStyle())) {
URL signUrl =
storage.signUrl(
blob, 1, TimeUnit.HOURS, Storage.SignUrlOption.httpMethod(HttpMethod.POST), urlStyle);
blob,
1,
TimeUnit.HOURS,
Storage.SignUrlOption.httpMethod(HttpMethod.POST),
Storage.SignUrlOption.withExtHeaders(extensionHeaders),
urlStyle);
byte[] bytesArrayToUpload = BLOB_STRING_CONTENT.getBytes();
try (WriteChannel writer = storage.writer(signUrl)) {
Storage unauthenticatedStorage = StorageOptions.getUnauthenticatedInstance().getService();
try (WriteChannel writer = unauthenticatedStorage.writer(signUrl)) {
writer.write(ByteBuffer.wrap(bytesArrayToUpload, 0, bytesArrayToUpload.length));
}

Expand Down

0 comments on commit 8b05867

Please sign in to comment.