I’m using AWS S3 and MongoDB in a full-stack react application.
When I add a new recept in front-end it works normally. But once in a while it changes the rndImageName which I create on back-end. Images uploads to S3 fine.
Is there better way to do this?
multer.js
const randomImageName = (bytes = 32) => crypto.randomBytes(bytes).toString('hex')
let rndImageName = randomImageName();
awsRouter.post('/recipeImages', upload.single('file'), async (req, res) => {
const buffer = await sharp(req.file.buffer).resize({ height: 1920, width: 1080, fit: 'contain' }).toBuffer()
const params = {
Bucket: bucketName,
Key: 'recipeImages/' + rndImageName,
Body: buffer,
ContentType: req.file.mimetype
}
const command = new PutObjectCommand(params)
await s3.send(command)
})
recipes.js
const { rndImageName } = require('./multer')
recipesRouter.post('/', async (request, response, next) => {
const body = request.body
const decodedToken = jwt.verify(getTokenFrom(request), process.env.SECRET)
if (!decodedToken.id) {
return response.status(401).json({ error: 'token invalid' })
}
const user = await User.findById(decodedToken.id)
const recipe = new Recipe({
id: body.id,
recipe_name: body.recipe_name,
ingredients: body.ingredients,
instructions: body.instructions,
speed: body.speed,
category: body.category,
main_ingredient: body.main_ingredient,
diet: body.diet,
likes: body.likes || 0,
comments: body.comments || [],
created: new Date(),
imageName: rndImageName,
user: user._id
})
const savedRecipe = await recipe.save()
user.recipes = user.recipes.concat(savedRecipe._id)
await user.save()
})
I want to insert the rndImageName (which I set up for S3 file name) to mongoDB objects so I can create signedUrl for a specific recipe.
Now it seems to use the same randomImageName again. Also changing the previously added recipe’s imageName.