Auto rename files into s3

Can we make uppy auto rename on upload if files with the same name already exist in the s3 bucket? We are self hosting companion.

Thank you in advance for any assistance.

That should be possible! I’m going to work under the assumption that you’re attempting to catch file upload error events on upload if there’s a name conflict.

You can hook into the upload-error event in order to get the File Object that Uppy failed to upload. In that case you can then modify that File Object within Uppy to have a new name and attempt to re-upload the file(s) with the .retryUpload(fileID) function.

Roughly what you need to do is the following:

  1. Create an event handler for the upload-error event.
  2. Create a function to uniquely rename your File Objects
  3. Tell Uppy to then retry the file Upload for each File Object that is caught in #1

Thanks for your prompt reply! Will give it a try although I’m a bit unsure how the error is thrown and it still uploads.

I don’t think that will work - my understanding is that S3 does not throw an error if a duplicate file is uploaded - it just overwrites the original (see too here).

In order to avoid this issue, in our instance we make each upload filename unique by prepending a date stamp to the filename (if you want, you can always strip that part of it out on download so only the original filename shows to the user).

Another (much more cumbersome) approach would be to make a call to S3 via your server to see if the file already exists, triggered via Uppy’s file-added event:

uppy.on('file-added', (file) => {
  // fetch a response from your server to see if the file exists in S3
  //, and if so, rename it here
})

But this seems like a much worse solution than just making each filename unique, since you wish to keep duplicate files anyway (but you could use something like this if you wanted to reject a duplicate, since S3 won’t).

Here’s how we append the date stamp to our filename:

Key: `${Date.now().toString()}-${filename}`,

Edit: a typo.