Currently, my APP writes one object to s3 at a time via PutObjectCommand.
like so:
const params = {
Bucket: process.env.AWS_S3_PHOTO_BUCKET,
Key: photo_name.toString(),
Body: resized_img,
ContentType: req.file.mimetype,
};
const command = new PutObjectCommand(params);
Now, what if I have an array of objects that I want to write to s3. How can I do this in one command?
so S3 doesn't let you put many objects in one go. You have do them one by one.
async function upload(photo) {
const params = {
Bucket: process.env.AWS_S3_PHOTO_BUCKET,
Key: photo.photo_name.toString(),
Body: photo.resized_img,
ContentType: photo.mimetype,
};
await s3Client.send(new PutObjectCommand(params));
};
function uploadAll(photos) {
Promise.all(photos.map(p => upload(p)))
.then(() => console.log("Uploaded!"))
.catch(err => console.log("Error:", err));
};
let photosArray = [];
uploadAll(photosArray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With