1
1
use aws_lambda_events:: event:: s3:: { S3Entity , S3Event } ;
2
+ use aws_lambda_events:: sqs:: SqsEvent ;
2
3
use aws_sdk_s3:: Client as S3Client ;
3
4
use lambda_runtime:: { run, service_fn, Error , LambdaEvent } ;
4
5
use routefinder:: Router ;
5
6
use tracing:: log:: * ;
6
- use aws_lambda_events:: sqs:: SqsEvent ;
7
- // use deltalake::{DeltaResult};
8
7
9
8
use std:: collections:: HashMap ;
10
9
11
-
12
10
/// A simple structure to make deserializing test events for identification easier
13
11
///
14
12
/// See <fhttps://github.com/buoyant-data/oxbow/issues/8>
@@ -18,15 +16,14 @@ struct TestEvent {
18
16
event : String ,
19
17
}
20
18
21
-
22
19
/// Convert the given [aws_lambda_events::sqs::SqsEvent] to a collection of
23
20
/// [aws_lambda_events::s3::S3EventRecord] entities. This is mostly useful for handling S3 Bucket
24
21
/// Notifications which have been passed into SQS
25
22
///
26
23
/// In the case where the [aws_lambda_events::sqs::SqsEvent] contains an `s3:TestEvent` which is
27
24
/// fired when S3 Bucket Notifications are first enabled, the event will be ignored to avoid
28
25
/// errorsin the processing pipeline
29
- async fn s3_from_sqs ( event : SqsEvent ) -> Result < S3Event , anyhow:: Error > {
26
+ fn s3_from_sqs ( event : SqsEvent ) -> Result < S3Event , anyhow:: Error > {
30
27
let mut records = vec ! [ ] ;
31
28
for record in event. records . iter ( ) {
32
29
/* each record is an SqsMessage */
@@ -55,12 +52,13 @@ async fn s3_from_sqs(event: SqsEvent) -> Result<S3Event,anyhow::Error> {
55
52
} ;
56
53
}
57
54
}
58
- Ok ( aws_lambda_events:: s3:: S3Event { records : records } )
55
+ Ok ( aws_lambda_events:: s3:: S3Event { records } )
59
56
}
60
57
61
-
62
-
63
- async fn function_handler ( event : LambdaEvent < SqsEvent > , client : & S3Client ) -> Result < ( ) , Error > {
58
+ async fn function_handler (
59
+ event : LambdaEvent < serde_json:: Value > ,
60
+ client : & S3Client ,
61
+ ) -> Result < ( ) , Error > {
64
62
let input_pattern =
65
63
std:: env:: var ( "INPUT_PATTERN" ) . expect ( "You must define INPUT_PATTERN in the environment" ) ;
66
64
let output_template = std:: env:: var ( "OUTPUT_TEMPLATE" )
@@ -72,9 +70,13 @@ async fn function_handler(event: LambdaEvent<SqsEvent>, client: &S3Client) -> Re
72
70
. parse ( & output_template) ?;
73
71
74
72
router. add ( input_pattern, 1 ) ?;
75
- let records = s3_from_sqs ( event. payload ) ;
76
73
77
- for entity in entities_from ( records. await ?) ? {
74
+ let records = match serde_json:: from_value :: < SqsEvent > ( event. payload . clone ( ) ) {
75
+ Ok ( sqs_event) => s3_from_sqs ( sqs_event) ?,
76
+ Err ( _) => serde_json:: from_value ( event. payload ) ?,
77
+ } ;
78
+
79
+ for entity in entities_from ( records) ? {
78
80
debug ! ( "Processing {entity:?}" ) ;
79
81
80
82
if let Some ( source_key) = entity. object . key {
@@ -116,13 +118,10 @@ async fn main() -> Result<(), Error> {
116
118
run ( func) . await
117
119
}
118
120
119
- /**
120
- * Return the deserialized and useful objects from the event payload
121
- *
122
- * This function will apply a filter to make sure that it is only return objects which have been
123
- * put in this invocation
124
- */
125
-
121
+ /// Return the deserialized and useful objects from the event payload
122
+ ///
123
+ /// This function will apply a filter to make sure that it is only return objects which have been
124
+ /// put in this invocation
126
125
fn entities_from ( event : S3Event ) -> Result < Vec < S3Entity > , anyhow:: Error > {
127
126
Ok ( event
128
127
. records
@@ -133,10 +132,8 @@ fn entities_from(event: S3Event) -> Result<Vec<S3Entity>, anyhow::Error> {
133
132
. collect ( ) )
134
133
}
135
134
136
- /**
137
- * Take the source key and the already configured router in order to access a collection of
138
- * captured parameters in a HashMap format
139
- */
135
+ /// Take the source key and the already configured router in order to access a collection of
136
+ /// captured parameters in a HashMap format
140
137
fn captured_parameters < Handler > (
141
138
router : & Router < Handler > ,
142
139
source_key : & str ,
@@ -275,4 +272,4 @@ mod tests {
275
272
"databases/oltp/a_table/ds=2023-09-05/some.parquet"
276
273
) ;
277
274
}
278
- }
275
+ }
0 commit comments