@@ -115,15 +115,22 @@ def _upload_string_to_s3(self, string, key):
115115 self .s3 .put_object (Bucket = self .bucket , Key = key , Body = string )
116116
117117 def _read_bytes_from_s3 (self , key , allow_missing = False , ext_bucket = None ):
118+ bucket = self .bucket
119+ if ext_bucket is not None :
120+ bucket = ext_bucket
121+
118122 try :
119- bucket = self .bucket
120- if ext_bucket is not None :
121- bucket = ext_bucket
122- byte_array = self .s3 .get_object (Bucket = bucket , Key = key )["Body" ].read ()
123- except self .s3 .exceptions .NoSuchKey as e :
124- if allow_missing :
125- return None
126- raise CortexException ("bucket " + self .bucket , "key " + key ) from e
123+ try :
124+ byte_array = self .s3 .get_object (Bucket = bucket , Key = key )["Body" ].read ()
125+ except self .s3 .exceptions .NoSuchKey as e :
126+ if allow_missing :
127+ return None
128+ raise e
129+ except Exception as e :
130+ raise CortexException (
131+ 'key "{}" in bucket "{}" could not be accessed; ' .format (key , bucket )
132+ + "it may not exist, or you may not have suffienct permissions"
133+ ) from e
127134
128135 return byte_array .strip ()
129136
@@ -161,12 +168,15 @@ def upload_file(self, local_path, key):
161168 self .s3 .upload_file (local_path , self .bucket , key )
162169
163170 def download_file (self , key , local_path ):
171+ util .mkdir_p (os .path .dirname (local_path ))
164172 try :
165- util .mkdir_p (os .path .dirname (local_path ))
166173 self .s3 .download_file (self .bucket , key , local_path )
167174 return local_path
168175 except Exception as e :
169- raise CortexException ("bucket " + self .bucket , "key " + key ) from e
176+ raise CortexException (
177+ 'key "{}" in bucket "{}" could not be accessed; ' .format (key , bucket )
178+ + "it may not exist, or you may not have suffienct permissions"
179+ ) from e
170180
171181 def zip_and_upload (self , local_path , key ):
172182 util .zip_dir (local_path , "temp.zip" )
@@ -186,20 +196,20 @@ def download_and_unzip_external(self, s3_path, local_dir):
186196 util .extract_zip (local_zip , delete_zip_file = True )
187197
188198 def download_file_external (self , s3_path , local_path ):
199+ util .mkdir_p (os .path .dirname (local_path ))
200+ bucket , key = self .deconstruct_s3_path (s3_path )
189201 try :
190- util .mkdir_p (os .path .dirname (local_path ))
191- bucket , key = self .deconstruct_s3_path (s3_path )
192202 self .s3 .download_file (bucket , key , local_path )
193203 return local_path
194204 except Exception as e :
195- raise CortexException ("bucket " + bucket , "key " + key ) from e
205+ raise CortexException (
206+ 'key "{}" in bucket "{}" could not be accessed; ' .format (key , bucket )
207+ + "it may not exist, or you may not have suffienct permissions"
208+ ) from e
196209
197210 def get_json_external (self , s3_path ):
198- try :
199- bucket , key = self .deconstruct_s3_path (s3_path )
200- obj = self ._read_bytes_from_s3 (key , ext_bucket = bucket )
201- if obj is None :
202- return None
203- return json .loads (obj .decode ("utf-8" ))
204- except Exception as e :
205- raise CortexException ("bucket " + bucket , "key " + key ) from e
211+ bucket , key = self .deconstruct_s3_path (s3_path )
212+ obj = self ._read_bytes_from_s3 (key , ext_bucket = bucket )
213+ if obj is None :
214+ return None
215+ return json .loads (obj .decode ("utf-8" ))
0 commit comments