33import logging
44import os
55import json
6- from s3path import S3Path
6+ import re
77
88logger = logging .getLogger (__name__ )
99AWS_S3_AUTH_PATH = '.configs/aws_s3_auth.json'
1010
1111
12- def parse_s3_path (s3_url ):
13- """Get s3_path for S3 Object URL
14-
15- :param s3_url: url of s3 object
16- :return: bucket and key name
17- """
18- path = S3Path .from_uri (s3_url )
19- return path .bucket , path .key
20-
21-
2212def parse_s3_uri (s3_uri ):
2313 """Get s3_path for S3 Object URL
2414
2515 :param s3_url: url of s3 object
2616 :return: s3_bucket, s3_client
2717 """
28- path = S3Path .from_uri (s3_uri )
29- uri_splt = path .key .split ('/' )
30- return path .bucket , get_s3_client (uri_splt [1 ], uri_splt [2 ], uri_splt [0 ]), uri_splt [3 ] if len (uri_splt ) > 3 else ''
18+
19+ matches = re .match ("s3://(.*?)/(.*)/(.*)/" , s3_uri )
20+ if matches :
21+ bucket , _ , key_name = matches .groups ()
22+ else :
23+ raise ValueError (f'Cannot interpret { s3_uri } ' )
24+ return bucket , get_s3_client (), key_name
3125
3226
3327def get_s3_client ():
@@ -55,13 +49,15 @@ def upload_file_to_s3(
5549 file_name ,
5650 bucket ,
5751 object_name = None ,
52+ content_type = "binary/octet-stream" ,
5853):
5954 """Upload a file to an object in an S3 bucket
6055
6156 :param s3_client: a boto3 S3 client
6257 :param file_name: File to upload
6358 :param bucket: Bucket to upload to
6459 :param object_name: S3 object name. If not specified, file_name is used
60+ :param content_type: AWS S3 Content Type, default to "binary/octet-stream"
6561 :return: True if file was uploaded, else False
6662 """
6763
@@ -75,6 +71,7 @@ def upload_file_to_s3(
7571 file_name ,
7672 bucket ,
7773 object_name ,
74+ ExtraArgs = {'ContentType' : content_type }
7875 )
7976 logger .info (response )
8077 except ClientError as client_error :
0 commit comments