Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a wildcard on S3 Event Notification prefix

Tags:

amazon-s3

I have a Lambda function that creates a thumbnail image for every image that gets uploaded to my bucket, it then places the Thumbnail inside another bucket. When I upload a user image (profile pic) I use the users ID and name as part of the key:

System-images/users/250/john_doe.jpg

Is there a way to use a wildcard in the prefix path? This is what I have so far but it doesn't work

S3 bucket properties

like image 879
Mark Kenny Avatar asked Oct 13 '15 16:10

Mark Kenny


People also ask

What is S3 key prefix?

A key prefix is a string of characters that can be the complete path in front of the object name (including the bucket name). For example, if an object (123. txt) is stored as BucketName/Project/WordFiles/123. txt, the prefix might be “BucketName/Project/WordFiles/123.

Is S3 event notification asynchronous?

Amazon S3 invokes your function asynchronously with an event that contains details about the object.

How do I enable and configure event notifications for an S3 bucket?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to enable events for. Choose Properties. Navigate to the Event Notifications section and choose Create event notification.


2 Answers

No, you can't -- it's a literal prefix.

In your example, you could use either of these prefixes, depending on what else is in the bucket (if there are things sharing the common prefix that you don't want to match):

System-images/
System-images/users/
like image 66
Michael - sqlbot Avatar answered Oct 07 '22 19:10

Michael - sqlbot


Wildcards in prefix/suffix filters of Lambda are not supported and will never be since the asterisk (*) is a valid character that can be used in S3 object key names. However, you could somehow fix this problem by adding a filter in your Lambda function. For example:

First, get the source key:

var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));

Then, check if it is inside the users folder:

if (srcKey.indexOf('/users/') === -1) {
    callback('Not inside users folder!');
    return;
}
like image 26
hatef Avatar answered Oct 07 '22 17:10

hatef