3
3
import logging
4
4
import os
5
5
import json
6
- from s3path import S3Path
6
+ import re
7
7
8
8
logger = logging .getLogger (__name__ )
9
9
AWS_S3_AUTH_PATH = '.configs/aws_s3_auth.json'
10
10
11
11
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
-
22
12
def parse_s3_uri (s3_uri ):
23
13
"""Get s3_path for S3 Object URL
24
14
25
15
:param s3_url: url of s3 object
26
16
:return: s3_bucket, s3_client
27
17
"""
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
31
25
32
26
33
27
def get_s3_client ():
@@ -55,13 +49,15 @@ def upload_file_to_s3(
55
49
file_name ,
56
50
bucket ,
57
51
object_name = None ,
52
+ content_type = "binary/octet-stream" ,
58
53
):
59
54
"""Upload a file to an object in an S3 bucket
60
55
61
56
:param s3_client: a boto3 S3 client
62
57
:param file_name: File to upload
63
58
:param bucket: Bucket to upload to
64
59
: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"
65
61
:return: True if file was uploaded, else False
66
62
"""
67
63
@@ -75,6 +71,7 @@ def upload_file_to_s3(
75
71
file_name ,
76
72
bucket ,
77
73
object_name ,
74
+ ExtraArgs = {'ContentType' : content_type }
78
75
)
79
76
logger .info (response )
80
77
except ClientError as client_error :
0 commit comments