Search code examples
javascriptnode.jsamazon-s3environment-variablesmulter

Process.env not reading? Amazon S3 Bucket


I'm trying to set my Amazon AWS access key and secret by using multer:

var upload = multer({
secretAccessKey: process.env.AWS_ACCESS_SECRET,
accessKeyId: process.env.AWS_ACCESS_KEY,
})

In my zshrc file I've done

export AWS_ACCESS_SECRET="mysecret"
export AWS_ACCESS_KEY="mykey"

however on running node, I get the error

 if (!opts.secretAccessKey) throw new Error('secretAccessKey is required')

However hardcoding the key and secret makes it work, but obviously that's not the safest way to go.

I have done source ~/.zshrc but it still is showing the error.


Solution

  • Multer out of the box doesn't support s3. The way you're creating a new multer object is incorrect. The only available options when creating a new multer object are dest/storage, fileFilter and limits.

    If you want to use Multer with S3 directly, you can use multer-s3. With that you can pass in a new option storage that will take your secretAccessKey and your accessKeyId.

    If you don't use multer-s3 you can use multer with aws-sdk's S3 Client.

    var multer = require('multer');
    var AWS = require('aws-sdk');
    
    var accessKeyId = process.env.AWS_ACCESS_KEY;
    var secretAccessKey = process.env.AWS_ACCESS_SECRET;
    
    var upload = multer({dest: '/temp'});       
    var s3 = new AWS.S3({
      accessKeyId: accessKeyId,
      secretAccessKey: secretAccessKey
    });