I want to get the image file from S3 in node.js, but I get a Forbidden 403 error in s3.getObject.

Asked 1 years ago, Updated 1 years ago, 79 views

I can upload it to S3, but the server code I wrote in node.js is gettingObject and I'm going to get the file from S3, but I get a 403 Forbidden error.

What I want to do is to read the photo I uploaded to S3 and display it on the screen, but I can't get the important image from S3.I would like someone to teach me.
I look forward to your kind cooperation.

getImage:function(req,res){
    var fileName = req.param('filename');
    sails.log.info('fileName='+fileName);
    vars3 = new AWS.S3({
        region: 'ap-northeast-1'
    });

    vars3Params = {
        bucket —The bucket name obtained from the constant,
        Key—fileName
    };
    s3.getObject(s3Params, function(err,data){
        if(err===null){
            sails.log.info('ok!');
            res.send(data.Body);
        } else{
            sails.log.error('err='+err.message);
            res.status(500).send(err);
        }
    });
},

The bucket policy for S3 is specified as follows:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowReading",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::xxxxxxxxxxx:user/myusername"
            },
            "Action"Action"]
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3::bucketName",
                "arn:aws:s3::bucketName/*"
            ]
        }
    ]
}

javascript node.js aws amazon-s3

2022-09-30 19:00

1 Answers

403 Forbidden means you don't have permission, so I think S3 needs to disclose the image file settings.
I think you should check the settings in S3.

https://ja.wikipedia.org/wiki/HTTP_403


2022-09-30 19:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.