Hi all. Does anyone know if there are any AWS S3 e...
# sst
i
Hi all. Does anyone know if there are any AWS S3 events/subscriptions so we can subscribe on it and notify users? I am using
aws-amplify
Storage
module. So when someone creates a file,
front-end
app must be aware of it. Has anyone faced this case?
c
Copy code
lambda.addEventSource(new aws_lambda_event_sources.S3EventSource(s3Bucket, {
       events: [s3.EventType.OBJECT_CREATED],
       filters: [{ prefix: 'bucket-key/' }]
     }));
You can use the above event source to trigger a lambda to run when a new file is created
i
@Chad (cysense) where are these pieces of code are imported from:
lambda
,
aws_lambda_event_sources
,
s3.EventType.OBJECT_CREATED
Are they executed on the front-end or back-end side?
c
aws_lambda_event_sources
and
aws_s3
are imported from
aws-cdk-lib
Lambda is a lambda function object you need to create
i
on the back-end side, right?
c
Yeah
i
But how to notify front-end?
c
Oh I see what you're asking. I dono of an easy way to notify the frontend. I would create an API using the above function and then you can subscribe to it with graphql in amplify
Or if you're not using
sst/cdk
at all and only using amplify you can still do what I said but set it up via amplify: https://docs.amplify.aws/cli/usage/lambda-triggers/#s3-lambda-triggers
i
@Chad (cysense) I use
SST
+
amplify
. I saw this page https://docs.amplify.aws/lib/storage/autotrack/q/platform/js/ but I don’t understand how to use it. Is there a method name or listener on
Storage
instance?
l
We have a separate table in db to store user file inserts (S3 event listener - part of SST S3 construct) that are periodically pulled via Cron and verified (important as users may have deleted the file in the meantime) then sent to interested parties
You could extend it using pub/sub for a fast info loop if that's part of your requirement
There's a tutorial about using iot notifiers (that's what happens in Amplify pub/sub under the hood if I remember correctly)
i
@Lukasz K https://docs.amplify.aws/lib/pubsub/getting-started/q/platform/js/#step-3-allow-the-amazon-cognito-authenticated-role-to-access-iot-services - this page only shows how to attach policy using
aws cli
. Do you know how to attach policy using
aws-amplify
(npm package) &
SST
?
I guess you want to use aws-amplify frontend component to easily connect to the pub/sub integration
Sadly I moved away from those quite a while ago (was annoyed with constant makeovers and breaking changes) so I'd have to build a demo of my own before I can assist you..
i
@Lukasz K Yeah. This step is working perfectly. But I am not clear how to connect back-end with front-end like in web socket way. The docs of
aws-amplify
(npm package) doesn't cover a step of how to attach policies to it
l
Yeah, that's cause all of amplify relies on the fact you're also using amplify Cli for provisioning
And all the policies are pre-attached to cognito authRole
i
@Lukasz K Do you know how
aws cli
attaches policies under the hood? Maybe we don't need to do this if we use
aws-amplify
?
l
As said above, I'd need to dust off one of my old projects and check how it's defined post-provisioning and in the amplify fe configuration file
i
@Lukasz K Ok. I will post this question to the channel. Maybe someone faced with this...