Priya
Priya

Reputation: 11

Amazon S3 with node rest API

I am trying to upload my data using postman. I have integrated amazon s3 bucket with it. But after I try to POST particular data, the postman throws an error: "Access Denied". Here is my code:

    exports.vehicles_create_vehicle = (req, res, next) => {


    const vehicle = new Vehicle({
      _id: new mongoose.Types.ObjectId(),
      name: req.body.name,
      category: req.body.category,
      price: req.body.price,
      vehicleImage: req.files[0].location,
      baseFare: req.body.baseFare,

    });
    vehicle
      .save()
      .then(result => {
        console.log(result);
        res.status(201).json({
          message: "Created product successfully",
          createdProduct: {
            name: result.name,
            price: result.price,
            category: result.category,
            _id: result._id,
            request: {
              type: "GET",
              url: "http://localhost:3000/vehicles/" + result._id
            }
          }
        });
      })
      .catch(err => {
        console.log(err);
        res.status(500).json({
          error: err
        });
      });
  };

And here is my bucket coding:

var aws = require('aws-sdk')
var multerS3 = require('multer-s3')


aws.config.update({
  signatureVersion: 'v4',
  secretAccessKey:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  accessKeyId: 'xxxxxxxxxxxxxxxxx',
  region: 'us-east-2'
})

var s3 = new aws.S3()

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'one-way-travel',
    key: function (req, file, cb) {
      cb(null, Date.now()+file.originalname.replace(/\s+/g, '-'));
    }
  })
})

I could not understand the reason for the error. Please help to figure it out.

Upvotes: 0

Views: 436

Answers (1)

jbarros
jbarros

Reputation: 638

To troubleshoot HTTP 403: Access Denied errors from Amazon S3, check the following:

  • Permissions for bucket and object owners across AWS accounts
  • Issues in bucket policy or AWS Identity and Access Management (IAM) user policies
  • User credentials to access Amazon S3
  • VPC endpoint policy
  • Missing object
  • Object encryption by AWS Key Management Service (AWS KMS)
  • Requester Pays enabled on bucket
  • AWS Organizations service control policy

Source: Amazon Knowledge Center.

Upvotes: 2

Related Questions