From acf28608d42e0992091dedb130bcd5bee897d274 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 12 Jan 2021 11:19:36 -0800 Subject: [PATCH 1/2] Fix unnecessary options in resumable upload URL --- .../cloud/storage/spi/v1/HttpStorageRpc.java | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index abdfa33ca6..fb4483f28d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -888,7 +888,16 @@ public String open(StorageObject object, Map options) { if (kmsKeyName != null && kmsKeyName.contains("cryptoKeyVersions")) { object.setKmsKeyName(""); } - Insert req = storage.objects().insert(object.getBucket(), object); + Insert req = storage.objects().insert(object.getBucket(), object) + .setName(object.getName()) + .setProjection(Option.PROJECTION.getString(options)) + .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) + .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)) + .setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)) + .setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)) + .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) + .setUserProject(Option.USER_PROJECT.getString(options)) + .setKmsKeyName(Option.KMS_KEY_NAME.getString(options)); GenericUrl url = req.buildHttpRequest().getUrl(); String scheme = url.getScheme(); String host = url.getHost(); @@ -897,29 +906,14 @@ public String open(StorageObject object, Map options) { String path = "/upload" + url.getRawPath(); url = new GenericUrl(scheme + "://" + host + ":" + port + path); url.set("uploadType", "resumable"); - url.set("name", object.getName()); - for (Option option : options.keySet()) { - Object content = option.get(options); - if (content != null) { - url.set(option.value(), content.toString()); - } - } + JsonFactory jsonFactory = storage.getJsonFactory(); HttpRequestFactory requestFactory = storage.getRequestFactory(); HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object)); HttpHeaders requestHeaders = httpRequest.getHeaders(); requestHeaders.set("X-Upload-Content-Type", detectContentType(object, options)); - String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options); - if (key != null) { - BaseEncoding base64 = BaseEncoding.base64(); - HashFunction hashFunction = Hashing.sha256(); - requestHeaders.set("x-goog-encryption-algorithm", "AES256"); - requestHeaders.set("x-goog-encryption-key", key); - requestHeaders.set( - "x-goog-encryption-key-sha256", - base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes())); - } + setEncryptionHeaders(requestHeaders, "x-goog-encryption-", options); HttpResponse response = httpRequest.execute(); if (response.getStatusCode() != 200) { GoogleJsonError error = new GoogleJsonError(); From 5c163640a46b2de26b3e1587f7a2addce11117c5 Mon Sep 17 00:00:00 2001 From: Jesse Lovelace Date: Tue, 12 Jan 2021 13:21:52 -0800 Subject: [PATCH 2/2] Fix format and streamline --- .../cloud/storage/spi/v1/HttpStorageRpc.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java index fb4483f28d..ffcf3d2ff8 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/HttpStorageRpc.java @@ -888,7 +888,10 @@ public String open(StorageObject object, Map options) { if (kmsKeyName != null && kmsKeyName.contains("cryptoKeyVersions")) { object.setKmsKeyName(""); } - Insert req = storage.objects().insert(object.getBucket(), object) + Insert req = + storage + .objects() + .insert(object.getBucket(), object) .setName(object.getName()) .setProjection(Option.PROJECTION.getString(options)) .setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)) @@ -898,13 +901,8 @@ public String open(StorageObject object, Map options) { .setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)) .setUserProject(Option.USER_PROJECT.getString(options)) .setKmsKeyName(Option.KMS_KEY_NAME.getString(options)); - GenericUrl url = req.buildHttpRequest().getUrl(); - String scheme = url.getScheme(); - String host = url.getHost(); - int port = url.getPort(); - port = port > 0 ? port : url.toURL().getDefaultPort(); - String path = "/upload" + url.getRawPath(); - url = new GenericUrl(scheme + "://" + host + ":" + port + path); + GenericUrl url = req.buildHttpRequestUrl(); + url.setRawPath("/upload" + url.getRawPath()); url.set("uploadType", "resumable"); JsonFactory jsonFactory = storage.getJsonFactory();