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 :)

2

2 Answers

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.

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()

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like