This lambda handler * listen to file creation events * downloads the created file * creates a thumbnail from it * uploads the thumbnail to bucket "[original bucket name]-thumbs". Make sure that * the created png file has no strange characters in the name * there is another bucket with "-thumbs" suffix in the name * this lambda only gets event from png file creation
(
event: LambdaEvent<S3Event>,
size: u32,
client: &T,
)
| 19 | * this lambda has permission to put file into the "-thumbs" bucket |
| 20 | */ |
| 21 | pub(crate) async fn function_handler<T: PutFile + GetFile>( |
| 22 | event: LambdaEvent<S3Event>, |
| 23 | size: u32, |
| 24 | client: &T, |
| 25 | ) -> Result<(), Error> { |
| 26 | let records = event.payload.records; |
| 27 | |
| 28 | for record in records.into_iter() { |
| 29 | let (bucket, key) = match get_file_props(record) { |
| 30 | Ok(touple) => touple, |
| 31 | Err(msg) => { |
| 32 | tracing::info!("Record skipped with reason: {}", msg); |
| 33 | continue; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | let image = match client.get_file(&bucket, &key).await { |
| 38 | Ok(vec) => vec, |
| 39 | Err(msg) => { |
| 40 | tracing::info!("Can not get file from S3: {}", msg); |
| 41 | continue; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | let thumbnail = match get_thumbnail(image, size) { |
| 46 | Ok(vec) => vec, |
| 47 | Err(msg) => { |
| 48 | tracing::info!("Can not create thumbnail: {}", msg); |
| 49 | continue; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | let mut thumbs_bucket = bucket.to_owned(); |
| 54 | thumbs_bucket.push_str("-thumbs"); |
| 55 | |
| 56 | // It uploads the thumbnail into a bucket name suffixed with "-thumbs" |
| 57 | // So it needs file creation permission into that bucket |
| 58 | |
| 59 | match client.put_file(&thumbs_bucket, &key, thumbnail).await { |
| 60 | Ok(msg) => tracing::info!(msg), |
| 61 | Err(msg) => tracing::info!("Can not upload thumbnail: {}", msg), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Ok(()) |
| 66 | } |
| 67 | |
| 68 | fn get_file_props(record: S3EventRecord) -> Result<(String, String), String> { |
| 69 | record |
no test coverage detected