1- import AWS from 'aws-sdk' ;
1+ import {
2+ DeleteObjectsCommand ,
3+ GetObjectCommand ,
4+ PutObjectCommand ,
5+ S3Client ,
6+ } from '@aws-sdk/client-s3' ;
7+ import { S3Event } from 'aws-lambda' ;
28import sharp from 'sharp' ;
39
410import * as ImageAdjustUtils from './src/image-processing/ImageAdjustUtils' ;
511import * as LaserdiscUtils from './src/image-processing/LaserdiscUtils' ;
612
7- const s3 = new AWS . S3 ( ) ;
13+ const s3 = new S3Client ( ) ;
814
915type Template = { prefix : string ; suffix : string } ;
1016
@@ -31,7 +37,7 @@ const INPUT_PREFIX = 'originals/';
3137const getRootKey = ( srcKey : string ) : string =>
3238 srcKey . substring ( INPUT_PREFIX . length ) . replace ( / \. \w + $ / , '' ) ;
3339
34- export const handler = async ( event ) : Promise < unknown > => {
40+ export const handler = async ( event : S3Event ) : Promise < unknown > => {
3541 const srcBucket = event . Records [ 0 ] . s3 . bucket . name ;
3642 const srcKey = event . Records [ 0 ] . s3 . object . key ;
3743
@@ -42,14 +48,18 @@ export const handler = async (event): Promise<unknown> => {
4248
4349 const rootKey = getRootKey ( srcKey ) ;
4450
45- const inputObject = await s3
46- . getObject ( {
51+ const inputObject = await s3 . send (
52+ new GetObjectCommand ( {
4753 Bucket : srcBucket ,
4854 Key : srcKey ,
4955 } )
50- . promise ( ) ;
56+ ) ;
5157
52- let inputBuffer = inputObject . Body as Buffer ;
58+ if ( ! inputObject . Body ) {
59+ throw new Error ( 'Source object not found' ) ;
60+ }
61+
62+ let inputBuffer = Buffer . from ( await inputObject . Body . transformToByteArray ( ) ) ;
5363
5464 // Crop laserdisc video frames to eliminate borders and superimposed banner
5565 if ( await LaserdiscUtils . isLaserdiscVideoFrame ( inputBuffer ) ) {
@@ -81,15 +91,15 @@ export const handler = async (event): Promise<unknown> => {
8191 } )
8292 . toBuffer ( )
8393 . then ( ( outputBuffer ) =>
84- s3
85- . putObject ( {
94+ s3 . send (
95+ new PutObjectCommand ( {
8696 Body : outputBuffer ,
8797 Bucket : srcBucket ,
8898 Key : makeFilename ( FILENAMES . jpeg , rootKey ) ,
8999 ACL : 'public-read' ,
90100 ContentType : 'image/jpeg' ,
91101 } )
92- . promise ( )
102+ )
93103 ) ,
94104
95105 sharp ( inputBuffer )
@@ -100,15 +110,15 @@ export const handler = async (event): Promise<unknown> => {
100110 } )
101111 . toBuffer ( )
102112 . then ( ( outputBuffer ) =>
103- s3
104- . putObject ( {
113+ s3 . send (
114+ new PutObjectCommand ( {
105115 Body : outputBuffer ,
106116 Bucket : srcBucket ,
107117 Key : makeFilename ( FILENAMES . jpeg720 , rootKey ) ,
108118 ACL : 'public-read' ,
109119 ContentType : 'image/jpeg' ,
110120 } )
111- . promise ( )
121+ )
112122 ) ,
113123
114124 sharp ( inputBuffer )
@@ -118,20 +128,20 @@ export const handler = async (event): Promise<unknown> => {
118128 } )
119129 . toBuffer ( )
120130 . then ( ( outputBuffer ) =>
121- s3
122- . putObject ( {
131+ s3 . send (
132+ new PutObjectCommand ( {
123133 Body : outputBuffer ,
124134 Bucket : srcBucket ,
125135 Key : makeFilename ( FILENAMES . jpeg420 , rootKey ) ,
126136 ACL : 'public-read' ,
127137 ContentType : 'image/jpeg' ,
128138 } )
129- . promise ( )
139+ )
130140 ) ,
131141 ] ) ;
132142} ;
133143
134- export const deletionHandler = async ( event ) : Promise < unknown > => {
144+ export const deletionHandler = async ( event : S3Event ) : Promise < unknown > => {
135145 const srcBucket = event . Records [ 0 ] . s3 . bucket . name ;
136146 const srcKey = event . Records [ 0 ] . s3 . object . key ;
137147
@@ -142,8 +152,8 @@ export const deletionHandler = async (event): Promise<unknown> => {
142152
143153 const rootKey = getRootKey ( srcKey ) ;
144154
145- return s3
146- . deleteObjects ( {
155+ return s3 . send (
156+ new DeleteObjectsCommand ( {
147157 Bucket : srcBucket ,
148158 Delete : {
149159 Objects : Object . values ( FILENAMES )
@@ -153,5 +163,5 @@ export const deletionHandler = async (event): Promise<unknown> => {
153163 } ) ) ,
154164 } ,
155165 } )
156- . promise ( ) ;
166+ ) ;
157167} ;
0 commit comments