@@ -2,6 +2,7 @@ use aws_lambda_events::event::s3::{S3Entity, S3Event};
22use aws_lambda_events:: sqs:: SqsEvent ;
33use aws_sdk_s3:: Client as S3Client ;
44use lambda_runtime:: { run, service_fn, Error , LambdaEvent } ;
5+ use regex:: Regex ;
56use routefinder:: Router ;
67use tracing:: log:: * ;
78
@@ -61,6 +62,8 @@ async fn function_handler(
6162) -> Result < ( ) , Error > {
6263 let input_pattern =
6364 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 ( ) ;
6467 let output_template = std:: env:: var ( "OUTPUT_TEMPLATE" )
6568 . expect ( "You must define OUTPUT_TEMPLATE in the environment" ) ;
6669
@@ -80,6 +83,10 @@ async fn function_handler(
8083 debug ! ( "Processing {entity:?}" ) ;
8184
8285 if let Some ( source_key) = entity. object . key {
86+ if should_exclude ( exclude_regex. as_ref ( ) , & source_key) {
87+ continue ;
88+ }
89+
8390 let parameters = add_builtin_parameters ( captured_parameters ( & router, & source_key) ?) ;
8491 let output_key = template. render ( & parameters) ?;
8592 info ! ( "Copying {source_key:?} to {output_key:?}" ) ;
@@ -110,7 +117,7 @@ async fn main() -> Result<(), Error> {
110117 . without_time ( )
111118 . init ( ) ;
112119
113- let shared_config = aws_config:: load_from_env ( ) . await ;
120+ let shared_config = aws_config:: from_env ( ) . load ( ) . await ;
114121 let client = S3Client :: new ( & shared_config) ;
115122 let client_ref = & client;
116123
@@ -146,6 +153,15 @@ fn captured_parameters<Handler>(
146153 Ok ( data)
147154}
148155
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
149165fn add_builtin_parameters ( mut data : HashMap < String , String > ) -> HashMap < String , String > {
150166 use chrono:: Datelike ;
151167 let now = chrono:: Utc :: now ( ) ;
@@ -246,7 +262,7 @@ mod tests {
246262 ]
247263}"# ;
248264
249- let event: S3Event = serde_json:: from_str ( & raw_buf) ?;
265+ let event: S3Event = serde_json:: from_str ( raw_buf) ?;
250266 Ok ( event)
251267 }
252268
@@ -272,4 +288,19 @@ mod tests {
272288 "databases/oltp/a_table/ds=2023-09-05/some.parquet"
273289 ) ;
274290 }
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+ }
275305}
306+
0 commit comments