/
s3.ts
50 lines (42 loc) · 1.54 KB
/
s3.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
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as lambda from '../../aws-lambda';
import * as s3 from '../../aws-s3';
import * as notifs from '../../aws-s3-notifications';
export interface S3EventSourceProps {
/**
* The s3 event types that will trigger the notification.
*/
readonly events: s3.EventType[];
/**
* S3 object key filter rules to determine which objects trigger this event.
* Each filter must include a `prefix` and/or `suffix` that will be matched
* against the s3 object key. Refer to the S3 Developer Guide for details
* about allowed filter rules.
*/
readonly filters?: s3.NotificationKeyFilter[];
}
/**
* Use S3 bucket notifications as an event source for AWS Lambda.
*/
export class S3EventSource implements lambda.IEventSource {
constructor(readonly bucket: s3.Bucket, private readonly props: S3EventSourceProps) {
}
public bind(target: lambda.IFunction) {
const filters = this.props.filters || [];
for (const event of this.props.events) {
this.bucket.addEventNotification(event, new notifs.LambdaDestination(target), ...filters);
}
}
}
/**
* S3EventSourceV2 Use S3 bucket notifications as an event source for AWS Lambda.
*/
export class S3EventSourceV2 implements lambda.IEventSource {
constructor(private readonly bucket: s3.IBucket, private readonly props: S3EventSourceProps) {
}
public bind(target: lambda.IFunction) {
const filters = this.props.filters || [];
for (const event of this.props.events) {
this.bucket.addEventNotification(event, new notifs.LambdaDestination(target), ...filters);
}
}
}