Search code examples
node.jsamazon-web-servicesamazon-s3aws-lambda

Image from S3 bucket does not display on server


I'm facing a weird issue on S3 bucket, I'm using S3 bucket to store images for the application and lambda to handle API functions, so the upload works correctly on local and also using lambda, the issue is when I want to show the image on the app, on localhost works great but on server the image does not show at all:

Here is on local: https://www.screencast.com/t/GA9R2ik3vX

But on server looks like this: https://www.screencast.com/t/oSY2AuE5NF


Solution

  • I'm thinking it could be a problem with your policy settings for the S3 bucket access. It may be that when setting up the bucket AWS obtained your local IP and added it to the allowed IPs list (I'm pretty sure AWS does this automatically sometimes) and that's why it works on your localhost but not on your server.

    Make sure your block public access looks like this: enter image description here

    And that your bucket policy is public:

    enter image description here

    It should look something like this:

    {
        "Version": "2012-10-17",
        "Id": "Policy123",
        "Statement": [
            {
                "Sid": "Stmt123",
                "Effect": "Allow",
                "Principal": "*",
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::your-bucket-name/*"
            }
        ]
    }
    

    Note that this will make images in your bucket public and anyone with a link will have access to it.