'How to upload a file with the same name to Amazon S3 and overwrite existing file?

s3.putObject({
        Bucket: bucketName,
        Key: fileName,
        Body: file,
        ACL: 'bucket-owner-full-control'
    }, function(err, data) {
        if (err) {
            console.log(err);
        }
        console.log(data)
    }
);

I use this code to upload image to my Amazon S3 cloud storage. But I can't upload a file with the same name (this name exist on the server S3 already).

How can I upload a file with the same name and overwrite the already existing one in S3?

Thanks for any help :)



Solution 1:[1]

By default, when you upload the file with same name. It will overwrite the existing file. In case you want to have the previous file available, you need to enable versioning in the bucket.

Solution 2:[2]

I guess you've encountered the default caching mechanism which is 24 hours, this results in not receiving the latest stored object. To override this, add parameter to putObject():

CacheControl: "no-cache" or Expires: new Date()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco
Solution 2 m.e