/
UploadProcessor.ts
37 lines (29 loc) · 957 Bytes
/
UploadProcessor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import * as AWSLambda from 'aws-lambda'
import { S3 } from '@aws-sdk/client-s3'
import { streamToBuffer } from './s3util'
const bucketName = process.env.BUCKET!
const s3 = new S3({ region: 'us-east-1' })
export const handler: AWSLambda.S3Handler = async (event) => {
await Promise.all(
event.Records.map(async (record) => {
const id = record.s3.object.key.split('/')[1]
const newKey = `files/${id}/content`
console.log(`Processing upload: ${id}`)
await s3.copyObject({
Bucket: bucketName,
CopySource: `${record.s3.bucket.name}/${record.s3.object.key}`,
Key: newKey,
})
await s3.deleteObject({
Bucket: record.s3.bucket.name,
Key: record.s3.object.key,
})
// Process the contents of the file
const obj = await s3.getObject({
Bucket: bucketName,
Key: newKey,
})
const contents = await streamToBuffer(obj.Body)
}),
)
}