anacronw/multer-s3

error - case 'undefined': throw new Error('bucket is required') - env variable

redhulkrko opened this issue · 2 comments

Hi, has anyone else experienced multer-s3 not accepting env variables?

I keep getting this error - case 'undefined': throw new Error('bucket is required'), I understand thats its not finding the name of my bucket. But I can't figure out why:

`const aws = require("aws-sdk");
const express = require("express");
const multer = require("multer");
const multerS3 = require("multer-s3");
const cors = require("cors");
const app = express();

const dotenv = require("dotenv");
dotenv.config({ path: "../config.env" });

const s3 = new aws.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID, // accessKeyId that is stored in .env file
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, // secretAccessKey is also store in .env file
});

app.use(cors());

const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET_NAME,
shouldTransform: true,
acl: "public-read-write",
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function (req, file, cb) {
cb(null, Date.now().toString());
},
});

const upload = multer({
storage: multerS3Config,
});

module.exports = upload;`

the contents of my config.env, (i've stripped out my credentials):

AWS_ACCESS_KEY_ID = xxxxxxxxxxxxxxxxxxxxxxxxxxx AWS_SECRET_ACCESS_KEY = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx AWS_BUCKET_NAME = xx-xxxxxx

Can you add a console.log(process.env.AWS_BUCKET_NAME) to make sure that the problem is actually with this library?

Can you add a console.log(process.env.AWS_BUCKET_NAME) to make sure that the problem is actually with this library?

Hi, yes it would seem it was an issue with dotenv, issue resolved.