1- #!/usr/bin/env python
2-
3- # Copyright 2016 Google Inc. All rights reserved.
1+ # Copyright 2023 Google LLC
42#
5- # Licensed under the Apache License, Version 2.0 (the ' License' );
3+ # Licensed under the Apache License, Version 2.0 (the " License" );
64# you may not use this file except in compliance with the License.
75# You may obtain a copy of the License at
86#
9- # http://www.apache.org/licenses/LICENSE-2.0
7+ # http://www.apache.org/licenses/LICENSE-2.0
108#
119# Unless required by applicable law or agreed to in writing, software
12- # distributed under the License is distributed on an ' AS IS' BASIS,
10+ # distributed under the License is distributed on an " AS IS" BASIS,
1311# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1412# See the License for the specific language governing permissions and
1513# limitations under the License.
14+
1615"""Command-line sample app demonstrating customer-supplied encryption keys.
1716
1817This sample demonstrates uploading an object while supplying an encryption key,
5251ANOTHER_KEY_HASH = '/gd0N3k3MK0SEDxnUiaswl0FFv6+5PHpo+5KD5SBCeA='
5352
5453
55- def create_service ():
54+ def create_service () -> googleapiclient . discovery . Resource :
5655 """Creates the service object for calling the Cloud Storage API."""
5756 # Construct the service object for interacting with the Cloud Storage API -
5857 # the 'storage' service, at version 'v1'.
@@ -61,8 +60,23 @@ def create_service():
6160 return googleapiclient .discovery .build ('storage' , 'v1' )
6261
6362
64- def upload_object (bucket , filename , encryption_key , key_hash ):
65- """Uploads an object, specifying a custom encryption key."""
63+ def upload_object (
64+ bucket : str ,
65+ filename : str ,
66+ encryption_key : str ,
67+ key_hash : str ,
68+ ) -> googleapiclient .http .HttpRequest :
69+ """Uploads an object, specifying a custom encryption key.
70+
71+ Args:
72+ bucket: The name of the bucket to upload to.
73+ filename: The name of the file to upload.
74+ encryption_key: The encryption key to use for the upload.
75+ key_hash: The hash of the encryption key
76+
77+ Returns:
78+ The http request object.
79+ """
6680 service = create_service ()
6781
6882 with open (filename , 'rb' ) as f :
@@ -82,8 +96,25 @@ def upload_object(bucket, filename, encryption_key, key_hash):
8296 return resp
8397
8498
85- def download_object (bucket , obj , out_file , encryption_key , key_hash ):
86- """Downloads an object protected by a custom encryption key."""
99+ def download_object (
100+ bucket : str ,
101+ obj : str ,
102+ out_file : str ,
103+ encryption_key : str ,
104+ key_hash : str ,
105+ ) -> googleapiclient .http .HttpRequest :
106+ """Downloads an object protected by a custom encryption key.
107+
108+ Args:
109+ bucket: The name of the bucket to download from.
110+ obj: The name of the object to download.
111+ filename: The name of the file to download to.
112+ encryption_key: The encryption key to use for the download
113+ key_hash: The hash of the encryption key
114+
115+ Returns:
116+ The http request object.
117+ """
87118 service = create_service ()
88119
89120 request = service .objects ().get_media (bucket = bucket , object = obj )
@@ -97,9 +128,27 @@ def download_object(bucket, obj, out_file, encryption_key, key_hash):
97128 out_file .write (request .execute ())
98129
99130
100- def rotate_key (bucket , obj , current_encryption_key , current_key_hash ,
101- new_encryption_key , new_key_hash ):
102- """Changes the encryption key used to store an existing object."""
131+ def rotate_key (
132+ bucket : str ,
133+ obj : str ,
134+ current_encryption_key : str ,
135+ current_key_hash : str ,
136+ new_encryption_key : str ,
137+ new_key_hash : str ,
138+ ) -> googleapiclient .http .HttpRequest :
139+ """Changes the encryption key used to store an existing object.
140+
141+ Args:
142+ bucket: The name of the bucket to upload to.
143+ obj: The name of the object to upload.
144+ current_encryption_key: The current encryption key to use for the upload.
145+ current_key_hash: The current hash of the encryption key
146+ new_encryption_key: The new encryption key to use for the upload.
147+ new_key_hash: The new hash of the encryption key
148+
149+ Returns:
150+ The http request object.
151+ """
103152 service = create_service ()
104153
105154 request = service .objects ().rewrite (
@@ -131,7 +180,19 @@ def rotate_key(bucket, obj, current_encryption_key, current_key_hash,
131180 rewrite_response .execute ()
132181
133182
134- def main (bucket , filename ):
183+ def main (
184+ bucket : str ,
185+ filename : str ,
186+ ) -> None :
187+ """Main method to upload and download objects.
188+
189+ Args:
190+ bucket: The name of the bucket to upload to.
191+ filename: The name of the file to upload.
192+
193+ Returns:
194+ None.
195+ """
135196 print (f'Uploading object gs://{ bucket } /{ filename } ' )
136197 upload_object (bucket , filename , ENCRYPTION_KEY , KEY_HASH )
137198 print ('Downloading it back' )
0 commit comments