Skip to content

Commit 411d0dd

Browse files
authored
Fix issue #185: Add event rules support (#199)
1 parent fb4a8a5 commit 411d0dd

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

packages/serverless-offline-s3/src/s3-event-definition.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class S3EventDefinition {
22
constructor(S3Event) {
33
this.event = S3Event.event;
44
this.bucket = S3Event.bucket;
5+
this.rules = S3Event.rules;
56
}
67
}
78

packages/serverless-offline-s3/src/s3.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ class S3 {
5757
}
5858

5959
async _s3Event(functionKey, s3Event) {
60-
const {event, bucket} = s3Event;
60+
const {event, bucket, rules} = s3Event;
6161
await this._waitFor(bucket);
6262

63-
const listener = this.client.listenBucketNotification(bucket, '*', '*', [event]);
63+
const eventRules = rules || [];
64+
const prefix = (eventRules.find(rule => rule.prefix) || {prefix: '*'}).prefix;
65+
const suffix = (eventRules.find(rule => rule.suffix) || {suffix: '*'}).suffix;
66+
67+
const listener = this.client.listenBucketNotification(bucket, prefix, suffix, [event]);
6468

6569
listener.on('notification', async record => {
6670
if (record) {

scripts/create-buckets.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ trap "exit 1" INT
33

44
AWS_ENDPOINT_URL=${AWS_ENDPOINT_URL:-http://localhost:9000}
55

6-
BUCKETS="documents pictures files"
6+
BUCKETS="documents pictures files others"
77
for BUCKET in $BUCKETS
88
do
99
until aws --endpoint-url ${AWS_ENDPOINT_URL} s3 ls s3://${BUCKET} > /dev/null 2> /dev/null

tests/serverless-plugins-integration/serverless.s3.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ functions:
2222
- s3:
2323
bucket: pictures
2424
event: s3:ObjectCreated:Put
25+
- s3:
26+
bucket: others
27+
event: s3:ObjectCreated:Put
28+
rules:
29+
- prefix: correct/
30+
- suffix: .csv
2531

2632
myCallbackHandler:
2733
handler: lambda/handler.callback

tests/serverless-plugins-integration/test-s3.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ const uploadFiles = async () => {
2424
client.fPutObject('files', 'first.txt', path),
2525
client.fPutObject('documents', 'second.txt', path),
2626
client.fPutObject('pictures', 'second.txt', path),
27-
client.fPutObject('files', 'second.txt', path)
27+
client.fPutObject('files', 'second.txt', path),
28+
client.fPutObject('others', 'correct/test.txt', path),
29+
client.fPutObject('others', 'wrong/test.csv', path),
30+
client.fPutObject('others', 'correct/test.csv', path),
31+
client.fPutObject('others', 'wrong/test.txt', path)
2832
]);
2933
};
30-
const EXPECED_LAMBDA_CALL = 8; // pictures files are consumed twice, by myPromiseHandler and myPythonHandler
34+
const EXPECTED_LAMBDA_CALL = 9; // pictures files are consumed twice, by myPromiseHandler and myPythonHandler
3135

3236
const serverless = spawn('serverless', ['--config', 'serverless.s3.yml', 'offline', 'start'], {
3337
stdio: ['pipe', 'pipe', 'pipe'],
@@ -51,7 +55,7 @@ pump(
5155
/offline: \(λ: .*\) RequestId: .* Duration: .* ms {2}Billed Duration: .* ms/g
5256
) || []
5357
).length;
54-
if (this.count === EXPECED_LAMBDA_CALL) serverless.kill();
58+
if (this.count === EXPECTED_LAMBDA_CALL) serverless.kill();
5559
cb();
5660
}
5761
})

0 commit comments

Comments
 (0)