@@ -888,38 +888,30 @@ public String open(StorageObject object, Map<Option, ?> options) {
888888 if (kmsKeyName != null && kmsKeyName .contains ("cryptoKeyVersions" )) {
889889 object .setKmsKeyName ("" );
890890 }
891- Insert req = storage .objects ().insert (object .getBucket (), object );
892- GenericUrl url = req .buildHttpRequest ().getUrl ();
893- String scheme = url .getScheme ();
894- String host = url .getHost ();
895- int port = url .getPort ();
896- port = port > 0 ? port : url .toURL ().getDefaultPort ();
897- String path = "/upload" + url .getRawPath ();
898- url = new GenericUrl (scheme + "://" + host + ":" + port + path );
891+ Insert req =
892+ storage
893+ .objects ()
894+ .insert (object .getBucket (), object )
895+ .setName (object .getName ())
896+ .setProjection (Option .PROJECTION .getString (options ))
897+ .setPredefinedAcl (Option .PREDEFINED_ACL .getString (options ))
898+ .setIfMetagenerationMatch (Option .IF_METAGENERATION_MATCH .getLong (options ))
899+ .setIfMetagenerationNotMatch (Option .IF_METAGENERATION_NOT_MATCH .getLong (options ))
900+ .setIfGenerationMatch (Option .IF_GENERATION_MATCH .getLong (options ))
901+ .setIfGenerationNotMatch (Option .IF_GENERATION_NOT_MATCH .getLong (options ))
902+ .setUserProject (Option .USER_PROJECT .getString (options ))
903+ .setKmsKeyName (Option .KMS_KEY_NAME .getString (options ));
904+ GenericUrl url = req .buildHttpRequestUrl ();
905+ url .setRawPath ("/upload" + url .getRawPath ());
899906 url .set ("uploadType" , "resumable" );
900- url .set ("name" , object .getName ());
901- for (Option option : options .keySet ()) {
902- Object content = option .get (options );
903- if (content != null ) {
904- url .set (option .value (), content .toString ());
905- }
906- }
907+
907908 JsonFactory jsonFactory = storage .getJsonFactory ();
908909 HttpRequestFactory requestFactory = storage .getRequestFactory ();
909910 HttpRequest httpRequest =
910911 requestFactory .buildPostRequest (url , new JsonHttpContent (jsonFactory , object ));
911912 HttpHeaders requestHeaders = httpRequest .getHeaders ();
912913 requestHeaders .set ("X-Upload-Content-Type" , detectContentType (object , options ));
913- String key = Option .CUSTOMER_SUPPLIED_KEY .getString (options );
914- if (key != null ) {
915- BaseEncoding base64 = BaseEncoding .base64 ();
916- HashFunction hashFunction = Hashing .sha256 ();
917- requestHeaders .set ("x-goog-encryption-algorithm" , "AES256" );
918- requestHeaders .set ("x-goog-encryption-key" , key );
919- requestHeaders .set (
920- "x-goog-encryption-key-sha256" ,
921- base64 .encode (hashFunction .hashBytes (base64 .decode (key )).asBytes ()));
922- }
914+ setEncryptionHeaders (requestHeaders , "x-goog-encryption-" , options );
923915 HttpResponse response = httpRequest .execute ();
924916 if (response .getStatusCode () != 200 ) {
925917 GoogleJsonError error = new GoogleJsonError ();
0 commit comments