1
+ /*
2
+ * Copyright 2016 Google Inc. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
1
17
import com .google .api .client .googleapis .json .GoogleJsonResponseException ;
2
18
import com .google .api .client .http .HttpHeaders ;
3
19
import com .google .api .client .http .InputStreamContent ;
@@ -45,18 +61,19 @@ class CustomerSuppliedEncryptionKeysSamples {
45
61
* @param storage A Storage object, ready for use
46
62
* @param bucketName The name of the destination bucket
47
63
* @param objectName The name of the destination object
48
- * @param base64CSEKey An AES256 key, encoded as a base64 string.
49
- * @param base64CSEKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
50
- * @throws IOException if there was some error download from GCS.
64
+ * @param base64CseKey An AES256 key, encoded as a base64 string.
65
+ * @param base64CseKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
51
66
*
52
67
* @return An InputStream that contains the decrypted contents of the object.
68
+ *
69
+ * @throws IOException if there was some error download from GCS.
53
70
*/
54
71
public static InputStream downloadObject (
55
72
Storage storage ,
56
73
String bucketName ,
57
74
String objectName ,
58
- String base64CSEKey ,
59
- String base64CSEKeyHash )
75
+ String base64CseKey ,
76
+ String base64CseKeyHash )
60
77
throws Exception {
61
78
Storage .Objects .Get getObject = storage .objects ().get (bucketName , objectName );
62
79
@@ -66,8 +83,8 @@ public static InputStream downloadObject(
66
83
// Now set the CSEK headers
67
84
final HttpHeaders httpHeaders = new HttpHeaders ();
68
85
httpHeaders .set ("x-goog-encryption-algorithm" , "AES256" );
69
- httpHeaders .set ("x-goog-encryption-key" , base64CSEKey );
70
- httpHeaders .set ("x-goog-encryption-key-sha256" , base64CSEKeyHash );
86
+ httpHeaders .set ("x-goog-encryption-key" , base64CseKey );
87
+ httpHeaders .set ("x-goog-encryption-key-sha256" , base64CseKeyHash );
71
88
72
89
getObject .setRequestHeaders (httpHeaders );
73
90
@@ -89,17 +106,17 @@ public static InputStream downloadObject(
89
106
* @param bucketName The name of the destination bucket
90
107
* @param objectName The name of the destination object
91
108
* @param data An InputStream containing the contents of the object to upload
92
- * @param base64CSEKey An AES256 key, encoded as a base64 string.
93
- * @param base64CSEKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
109
+ * @param base64CseKey An AES256 key, encoded as a base64 string.
110
+ * @param base64CseKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
94
111
* @throws IOException if there was some error uploading to GCS.
95
112
*/
96
113
public static void uploadObject (
97
114
Storage storage ,
98
115
String bucketName ,
99
116
String objectName ,
100
117
InputStream data ,
101
- String base64CSEKey ,
102
- String base64CSEKeyHash )
118
+ String base64CseKey ,
119
+ String base64CseKeyHash )
103
120
throws IOException {
104
121
InputStreamContent mediaContent = new InputStreamContent ("text/plain" , data );
105
122
Storage .Objects .Insert insertObject =
@@ -112,8 +129,8 @@ public static void uploadObject(
112
129
// Now set the CSEK headers
113
130
final HttpHeaders httpHeaders = new HttpHeaders ();
114
131
httpHeaders .set ("x-goog-encryption-algorithm" , "AES256" );
115
- httpHeaders .set ("x-goog-encryption-key" , base64CSEKey );
116
- httpHeaders .set ("x-goog-encryption-key-sha256" , base64CSEKeyHash );
132
+ httpHeaders .set ("x-goog-encryption-key" , base64CseKey );
133
+ httpHeaders .set ("x-goog-encryption-key-sha256" , base64CseKeyHash );
117
134
118
135
insertObject .setRequestHeaders (httpHeaders );
119
136
@@ -192,18 +209,18 @@ public static void main(String[] args) throws Exception {
192
209
System .exit (1 );
193
210
}
194
211
String bucketName = args [0 ];
195
-
212
+
196
213
Storage storage = StorageFactory .getService ();
197
214
InputStream dataToUpload = new StorageUtils .ArbitrarilyLargeInputStream (10000000 );
198
215
199
216
System .out .format ("Uploading object gs://%s/%s using CSEK.\n " , bucketName , OBJECT_NAME );
200
217
uploadObject (storage , bucketName , OBJECT_NAME , dataToUpload , CSEK_KEY , CSEK_KEY_HASH );
201
-
218
+
202
219
System .out .format ("Downloading object gs://%s/%s using CSEK.\n " , bucketName , OBJECT_NAME );
203
220
InputStream objectData =
204
221
downloadObject (storage , bucketName , OBJECT_NAME , CSEK_KEY , CSEK_KEY_HASH );
205
222
StorageUtils .readStream (objectData );
206
-
223
+
207
224
System .out .println ("Rotating object to use a different CSEK." );
208
225
rotateKey (storage , bucketName , OBJECT_NAME , CSEK_KEY , CSEK_KEY_HASH ,
209
226
ANOTHER_CESK_KEY , ANOTHER_CSEK_KEY_HASH );
0 commit comments