1- import { S3 } from '@aws-sdk/client-s3' ;
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 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,10 +48,12 @@ export const handler = async (event): Promise<unknown> => {
4248
4349 const rootKey = getRootKey ( srcKey ) ;
4450
45- const inputObject = await s3 . getObject ( {
46- Bucket : srcBucket ,
47- Key : srcKey ,
48- } ) ;
51+ const inputObject = await s3 . send (
52+ new GetObjectCommand ( {
53+ Bucket : srcBucket ,
54+ Key : srcKey ,
55+ } )
56+ ) ;
4957
5058 if ( ! inputObject . Body ) {
5159 throw new Error ( 'Source object not found' ) ;
@@ -83,13 +91,15 @@ export const handler = async (event): Promise<unknown> => {
8391 } )
8492 . toBuffer ( )
8593 . then ( ( outputBuffer ) =>
86- s3 . putObject ( {
87- Body : outputBuffer ,
88- Bucket : srcBucket ,
89- Key : makeFilename ( FILENAMES . jpeg , rootKey ) ,
90- ACL : 'public-read' ,
91- ContentType : 'image/jpeg' ,
92- } )
94+ s3 . send (
95+ new PutObjectCommand ( {
96+ Body : outputBuffer ,
97+ Bucket : srcBucket ,
98+ Key : makeFilename ( FILENAMES . jpeg , rootKey ) ,
99+ ACL : 'public-read' ,
100+ ContentType : 'image/jpeg' ,
101+ } )
102+ )
93103 ) ,
94104
95105 sharp ( inputBuffer )
@@ -100,13 +110,15 @@ export const handler = async (event): Promise<unknown> => {
100110 } )
101111 . toBuffer ( )
102112 . then ( ( outputBuffer ) =>
103- s3 . putObject ( {
104- Body : outputBuffer ,
105- Bucket : srcBucket ,
106- Key : makeFilename ( FILENAMES . jpeg720 , rootKey ) ,
107- ACL : 'public-read' ,
108- ContentType : 'image/jpeg' ,
109- } )
113+ s3 . send (
114+ new PutObjectCommand ( {
115+ Body : outputBuffer ,
116+ Bucket : srcBucket ,
117+ Key : makeFilename ( FILENAMES . jpeg720 , rootKey ) ,
118+ ACL : 'public-read' ,
119+ ContentType : 'image/jpeg' ,
120+ } )
121+ )
110122 ) ,
111123
112124 sharp ( inputBuffer )
@@ -116,18 +128,20 @@ export const handler = async (event): Promise<unknown> => {
116128 } )
117129 . toBuffer ( )
118130 . then ( ( outputBuffer ) =>
119- s3 . putObject ( {
120- Body : outputBuffer ,
121- Bucket : srcBucket ,
122- Key : makeFilename ( FILENAMES . jpeg420 , rootKey ) ,
123- ACL : 'public-read' ,
124- ContentType : 'image/jpeg' ,
125- } )
131+ s3 . send (
132+ new PutObjectCommand ( {
133+ Body : outputBuffer ,
134+ Bucket : srcBucket ,
135+ Key : makeFilename ( FILENAMES . jpeg420 , rootKey ) ,
136+ ACL : 'public-read' ,
137+ ContentType : 'image/jpeg' ,
138+ } )
139+ )
126140 ) ,
127141 ] ) ;
128142} ;
129143
130- export const deletionHandler = async ( event ) : Promise < unknown > => {
144+ export const deletionHandler = async ( event : S3Event ) : Promise < unknown > => {
131145 const srcBucket = event . Records [ 0 ] . s3 . bucket . name ;
132146 const srcKey = event . Records [ 0 ] . s3 . object . key ;
133147
@@ -138,14 +152,16 @@ export const deletionHandler = async (event): Promise<unknown> => {
138152
139153 const rootKey = getRootKey ( srcKey ) ;
140154
141- return s3 . deleteObjects ( {
142- Bucket : srcBucket ,
143- Delete : {
144- Objects : Object . values ( FILENAMES )
145- . map ( ( template ) => makeFilename ( template , rootKey ) )
146- . map ( ( key ) => ( {
147- Key : key ,
148- } ) ) ,
149- } ,
150- } ) ;
155+ return s3 . send (
156+ new DeleteObjectsCommand ( {
157+ Bucket : srcBucket ,
158+ Delete : {
159+ Objects : Object . values ( FILENAMES )
160+ . map ( ( template ) => makeFilename ( template , rootKey ) )
161+ . map ( ( key ) => ( {
162+ Key : key ,
163+ } ) ) ,
164+ } ,
165+ } )
166+ ) ;
151167} ;
0 commit comments