Search code examples
c#amazon-web-servicesamazon-s3amazon-cloudfrontacl

How to restrict read access to file in S3 file


I'm new in AWS S3. My project needs to upload images to a 'private' S3 bucket (disable all public access setting in S3) and allows only certain users to access these images who have valid JWT token (mostly stored in browser, maybe localStorage / cookie)

So how can I setup S3 service and implement in C# to archive this goal: open URL with valid token then user can view image, and show unauthorize access page if user don't valid token. Sample content may be like below

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>ResourceNotFound</Code>
<Message>The specified resource does not exist. RequestId:4087a246-001e-0002-3481-37acce000000 Time:2024-11-15T17:13:52.7976811Z</Message>
</Error>

These 2 examples are tested with same signed url

I found something like cloudfront but I don't know where I need to start or is that my expected stuff

Thanks


Solution

  • We can use CloudFront distributions to secure the S3 object

    Here's step by step guide

    The term I mentioned above about the valid token can be checked by trigger a lambda function when accessing to a signed url

    https://github.com/aws-samples/amazon-cloudfront-signed-urls-using-lambda-secretsmanager