@@ -2,6 +2,7 @@ use aws_lambda_events::event::s3::{S3Entity, S3Event};
2
2
use aws_lambda_events:: sqs:: SqsEvent ;
3
3
use aws_sdk_s3:: Client as S3Client ;
4
4
use lambda_runtime:: { run, service_fn, Error , LambdaEvent } ;
5
+ use regex:: Regex ;
5
6
use routefinder:: Router ;
6
7
use tracing:: log:: * ;
7
8
@@ -61,6 +62,8 @@ async fn function_handler(
61
62
) -> Result < ( ) , Error > {
62
63
let input_pattern =
63
64
std:: env:: var ( "INPUT_PATTERN" ) . expect ( "You must define INPUT_PATTERN in the environment" ) ;
65
+ let exclude_regex: Option < Regex > = std:: env:: var ( "EXCLUDE_REGEX" )
66
+ . map ( |ex| Regex :: new ( ex. as_ref ( ) ) . expect ( "Failed to compile EXCLUDE_REGEX" ) ) . ok ( ) ;
64
67
let output_template = std:: env:: var ( "OUTPUT_TEMPLATE" )
65
68
. expect ( "You must define OUTPUT_TEMPLATE in the environment" ) ;
66
69
@@ -80,6 +83,10 @@ async fn function_handler(
80
83
debug ! ( "Processing {entity:?}" ) ;
81
84
82
85
if let Some ( source_key) = entity. object . key {
86
+ if should_exclude ( exclude_regex. as_ref ( ) , & source_key) {
87
+ continue ;
88
+ }
89
+
83
90
let parameters = add_builtin_parameters ( captured_parameters ( & router, & source_key) ?) ;
84
91
let output_key = template. render ( & parameters) ?;
85
92
info ! ( "Copying {source_key:?} to {output_key:?}" ) ;
@@ -110,7 +117,7 @@ async fn main() -> Result<(), Error> {
110
117
. without_time ( )
111
118
. init ( ) ;
112
119
113
- let shared_config = aws_config:: load_from_env ( ) . await ;
120
+ let shared_config = aws_config:: from_env ( ) . load ( ) . await ;
114
121
let client = S3Client :: new ( & shared_config) ;
115
122
let client_ref = & client;
116
123
@@ -146,6 +153,15 @@ fn captured_parameters<Handler>(
146
153
Ok ( data)
147
154
}
148
155
156
+ /// Return true if the given key matches the pattern and should be excluded from consideration
157
+ fn should_exclude ( pattern : Option < & Regex > , key : & str ) -> bool {
158
+ match pattern {
159
+ Some ( re) => re. is_match ( key) ,
160
+ None => false ,
161
+ }
162
+ }
163
+
164
+ /// Introduce the necessary built-in parameters to the `data` for rendering a Handlebars template
149
165
fn add_builtin_parameters ( mut data : HashMap < String , String > ) -> HashMap < String , String > {
150
166
use chrono:: Datelike ;
151
167
let now = chrono:: Utc :: now ( ) ;
@@ -246,7 +262,7 @@ mod tests {
246
262
]
247
263
}"# ;
248
264
249
- let event: S3Event = serde_json:: from_str ( & raw_buf) ?;
265
+ let event: S3Event = serde_json:: from_str ( raw_buf) ?;
250
266
Ok ( event)
251
267
}
252
268
@@ -272,4 +288,19 @@ mod tests {
272
288
"databases/oltp/a_table/ds=2023-09-05/some.parquet"
273
289
) ;
274
290
}
291
+
292
+ #[ test]
293
+ fn test_exclude_regex ( ) {
294
+ let exclude = Some ( Regex :: new ( r#"^path\/to\/table.*"# ) . expect ( "Failed to compile regular expression" ) ) ;
295
+ let keys = vec ! [
296
+ "path/to/alpha" ,
297
+ "path/to/bravo/foo.parquet" ,
298
+ "path/to/table" ,
299
+ "path/to/table/foo.parquet" ,
300
+ ] ;
301
+
302
+ let filtered: Vec < _ > = keys. iter ( ) . filter ( |k| !should_exclude ( exclude. as_ref ( ) , k) ) . map ( |k| k. clone ( ) ) . collect ( ) ;
303
+ assert_ne ! ( filtered, keys) ;
304
+ }
275
305
}
306
+
0 commit comments