const {
S3Client,
PutObjectCommand,
GetObjectCommand,
DeleteObjectCommand,
} = require("@aws-sdk/client-s3");
const params = {
Bucket: process.env.AWS_S3_PHOTO_BUCKET,
Key: photo_name.toString(),
Body: resized_img,
ContentType: req.file.mimetype,
IfNoneMatch: "*", // does not work
};
const command = new PutObjectCommand(params);
await AWS_S3.send(command);
I have added the IfNoneMatch: "*"
, but it has no effect. It is completely ignored.
What am I doing wrong? I need the send
command to fail/throw error if an existing object with the same key already exists, resulting in an overwrite if the operation is successful.
Update your @aws-sdk/client-s3 library to the latest version.
Older versions of the library, from a time before this feature was implemented, did not support this option and so will ignore the IfNoneMatch option indicated in the parameters passed to PutObjectCommand.
When the library is updated, the PutObject should fail with:
httpStatusCode: 412
Code: PreconditionFailed
Condition: If-None-Match
I tested this and was able to reproduce the behavior with an older library version. Upgrading to the latest fixed the problem.