0

I am using Node.js and multer3s to communicate with AWS S3. I have tried a few different setups but still cannot make it right to actually upload a file to my bucket.

For the S3 setup I have followed this tutorial.

In my IAM service I have a user myuser that has this

permission policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::*"
            ]
        }
    ]
}

then I have a bucket that has set Block all public access to off (I have also tried to have it on).

The bucket has:

  • bucket policy
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddCannedAcl",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::228522121793:user/myuser"
            },
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl"
            ],
            "Resource": [
                "arn:aws:s3:::mybucket/*",
                "arn:aws:s3:::mybucket"
            ],
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": "public-read"
                }
            }
        }
    ]
}
  • CORS configuration
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
  • under Access points sections there are no access points created.

in Node.js

aws.config.update({
    accessId: ACCESS_KEY,
    secretAccessKey: SECRET_KEY,
    region: 'eu-west-3'
})

const s3 = new aws.S3()

const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: BUCKET,
        acl: 'public-read',
        key(req, file, cb) {
            cb(null, Date.now() + shortid.generate())
        }
    }),
    limits: {
        files: 1,
        fileSize: 2580 * 1944
    },
    fileFilter(req, file, cb) {
        // filter
        cb(undefined, true)
    }
})

router.post('/test/img', upload.single('img'), async (req, res) => {
    console.log("success")
    console.log(req.file.location)
    res.send()
}, (error, req, res, next) => {
    res.status(400).send({
        error: error.message
    })
})

I would appreciate any suggestions as to what I might be doing wrong. Thanks

eja08
  • 4,600
  • 3
  • 13
  • 19
  • 1
    Can you share the error message you get? – maafk Apr 02 '20 at 21:47
  • @tkwargs the error message is only "Access denied" if you mean the error from the router.post – eja08 Apr 02 '20 at 22:17
  • When the Nodejs SDK is attempting to upload the file to S3, but doesn't have the proper permissions, you should see in your server/lambda logs the error message from AWS stating what S3 action you attempted and that you don't have the permissions. – maafk Apr 02 '20 at 22:24
  • @tkwargs oh, ok. I don't se anything like that. I run nodemon and the only think that gets print out is "Access denied" because of my print statement. And I don't have any image uploaded in the bucket – eja08 Apr 02 '20 at 22:33
  • Are you able to add more verbose logging? – maafk Apr 03 '20 at 09:01

0 Answers0