0

I'm using multer for image upload while uploading an image it's showing the error. I searched so much but can't able to find solution. Please suggest me some blog or documentation so that I can follow them to know about this stuff

  var aws = require("aws-sdk"),
      multerS3 = require("multer-s3");
    s3 = new aws.S3({
      accessKeyId: process.env.ACCESS_KEY_ID,
      accessSecretKey: process.env.ACCESS_SECRET_KEY,
      region:process.env.REGION,
      Bucket: process.env.AWS_BUCKET_NAME,
    });
    var upload = multer({
      storage: multerS3({
        
        s3: s3,
        bucket: "******",
        metadata: function (req, file, cb) {
          cb(null, { fieldName: file.fieldname });
        },
        key: function (req, file, cb) {
          const uniqueSuffix =
            "myfile" + Date.now() + "-" + Math.round(Math.random() * 1e9);
          cb(null, uniqueSuffix + file.originalname);
        },
      }),
    });

My bucket policy

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

    <?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>
James Z
  • 12,209
  • 10
  • 24
  • 44
Abhay Jain
  • 13
  • 4
  • I assume that you have all the `process.env` variable defined in your shell? can you print `env` and see that all the 4 parameters are defined properly? If you defined them in `~/.aws/config` you shouldn't try to pass them from the environment!) – Nir Alfasi Oct 25 '20 at 09:50
  • Variables are defined properly but the same problem arises even I hard code them – Abhay Jain Oct 25 '20 at 09:59
  • Still facing the same problem trying to get credentail from JSON file but its showing EC2 Metadata roleName request returned error – Abhay Jain Oct 25 '20 at 18:15

0 Answers0