Both users are uploading unique images but their camera roll has the same image name.
Should I use a uuid as a file name instead?
CodePudding user response:
You can't prevent overwrites in S3 through the API.
For avoidance, there are multiple strategies that all come down to namespacing or UUIDs. What you want to use depends on what you plan to do with the data.
Approach 1
s3://<bucket>/<userId>/<filename>
This way, you avoid users overwriting each other's files, but a user could still overwrite their own files. You could, with relative ease, list the uploads a specific user has made (but it could get expensive).
Approach 2
s3://<bucket>/<userId>/<uuid>.jpg
You still avoid users being able to overwrite each other's data and make it exceedingly unlikely that a user overwrites their own images - but you lose the information about the original file name.
Approach 3
s3://<bucket>/<userId>/<uuid>/<filename>
This key schema retains the benefits of the first two approaches and also allows you to retain the original filename, but it will be more annoying if you want to look at the data in the console because there will be more "directory" levels.
Approach 4
s3://<bucket>/<uuid>.jpg
This way, you don't namespace anything and just rely on UUIDs to avoid overwriting data. You lose the information about the original file name and which user the object belongs to unless you have a secondary data structure (e.g. an index of you data in DynamoDB).
All of these options (and more) are completely valid, personally, I'd pick something that at least namespaces my data by the user id because that makes it easier to delete specific users if necessary and also allows me to write IAM policies to allow or deny access to specific users.