-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_manifest.py
167 lines (135 loc) · 4.71 KB
/
prepare_manifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import json
from dataclasses import dataclass
from typing import List, Optional, Pattern
import boto3
import botocore
from common import get_static_config_instance, EtlConfig
@dataclass
class S3ManifestEntry:
url: str
mandatory: bool
@dataclass
class S3Manifest:
entries: List[S3ManifestEntry]
def build_s3_manifest(object_summaries: List) -> S3Manifest:
"""
Build `S3Manifest` object from S3 object summaries
Parameters:
object_summaries: list of object summary containing `bucket_name: str` and `key: str`
Returns:
s3_manifest: S3Manifest object
"""
return S3Manifest(entries=[
S3ManifestEntry(url=f's3://{object_summary.bucket_name}/{object_summary.key}', mandatory=True)
for
object_summary
in
object_summaries
])
def get_object_summaries(
bucket,
prefix: str,
regex_pattern: Pattern[str]
) -> List:
"""
Get S3 object summaries from S3 bucket with specific prefix and regular expression.
Parameters:
bucket: S3 bucket resource
prefix: prefix used as S3 object filter
regex_pattern: regular expression used as additional S3 object filter after using `prefix`
Returns:
object_summaries: list of object summaries
"""
object_summaries = []
for object_summary in bucket.objects.filter(Prefix=prefix):
if regex_pattern.match(object_summary.key):
object_summaries = [*object_summaries, object_summary]
return object_summaries
def upload_object_summaries_as_manifest(
s3,
object_summaries: List,
manifest_bucket_name: str,
manifest_key: str
) -> None:
"""
Upload Redshift manifest file created from S3 object summaries to particular S3 bucket
Parameters:
s3: S3 resource
object_summaries: S3 object summaries
manifest_bucket_name: S3 bucket name used to store Redshift manifest file
manifest_key: S3 bucket key used to store Redshift manifest file
Returns:
None
"""
s3_manifest: S3Manifest = build_s3_manifest(object_summaries)
serialized_manifest = json.dumps({
'entries': [
{
'url': entry.url,
'mandatory': entry.mandatory
}
for entry
in s3_manifest.entries
]
})
manifest_bytes = bytes(serialized_manifest, 'utf-8')
obj = s3.Object(manifest_bucket_name, manifest_key)
obj.put(Body=manifest_bytes)
def build_and_upload_manifest_file(
s3,
etl_config: EtlConfig
) -> None:
"""
Build and upload manifest file to S3 bucket according to predefined ETL configuration
Parameters:
s3: S3 resource
etl_config: ETL application configuration
Returns:
None
"""
manifest_bucket_validation_error: Optional = None
try:
s3.meta.client.head_bucket(Bucket=etl_config.manifest.bucket_name)
except botocore.exceptions.ClientError as e:
manifest_bucket_validation_error = e
if manifest_bucket_validation_error:
error_code = manifest_bucket_validation_error.response['Error']['Code']
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/migrations3.html#accessing-a-bucket
if error_code == '404':
s3.create_bucket(
Bucket=etl_config.manifest.bucket_name,
CreateBucketConfiguration={
'LocationConstraint': etl_config.region_name
}
)
else:
raise manifest_bucket_validation_error
dataset_bucket = s3.Bucket(etl_config.data_set.bucket_name)
song_data_object_summaries = get_object_summaries(
bucket=dataset_bucket,
prefix=etl_config.data_set.song_data_prefix,
regex_pattern=etl_config.data_set.song_data_regex_pattern
)
log_data_object_summaries = get_object_summaries(
bucket=dataset_bucket,
prefix=etl_config.data_set.log_data_prefix,
regex_pattern=etl_config.data_set.log_data_regex_pattern
)
upload_object_summaries_as_manifest(
s3,
song_data_object_summaries,
manifest_bucket_name=etl_config.manifest.bucket_name,
manifest_key=etl_config.manifest.song_data_key
)
upload_object_summaries_as_manifest(
s3,
log_data_object_summaries,
manifest_bucket_name=etl_config.manifest.bucket_name,
manifest_key=etl_config.manifest.event_data_key
)
def main() -> None:
s3 = boto3.resource('s3')
etl_config = get_static_config_instance()
build_and_upload_manifest_file(s3, etl_config)
if __name__ == "__main__":
main()