#Allow Prefix Parameter in Payload API for S3 Media File Disambiguation

6 messages · Page 1 of 1 (latest)

pearl fog
#

In the current implementation of Payload, the default behaviour for Media/Uploads enforces that file names must be globally unique. My team and I have invested significant effort in integrating the S3 storage plugin, enabling folder structures, and adjusting indexes to deliver a more intuitive media management experience. Specifically, we have aligned the uniqueness constraint to require image names to be unique only within their respective folders, utilizing a composite index of file name and prefix to enforce this.

Functionality works as expected for media organization and storage. However, we have encountered a limitation when retrieving images via the API endpoint: http://localhost:3000/api/media/file/blocks.jpg

At present, the API solely uses the image name to perform a lookup for the S3 prefix. When multiple images with the same name exist in different folders, the API’s response (using docs[0]) does not guarantee that the correct file is returned.

Proposed Enhancement: I'd like to propose adding an optional query parameter to the API to allow passing the prefix when it is known, for example: http://localhost:3000/api/media/file/blocks.jpg?prefix=UUID

This would provide a straightforward solution to precisely identify and fetch the correct file, without the need to develop a custom endpoint or duplicate existing logic for secure file streaming from S3.

I believe this enhancement would greatly improve the developer and user experience for managing media in Payload. I’d be happy to further discuss the implementation or contribute a pull request if there is interest.

floral zodiacBOT
wicked lodge
#

I've actually been working on a solution to this myself! the current path that I started down involved creating a custom API endpoint + request handler for (using NextJS dynamic route syntax here)http://localhost:3000/api/files/[...slug], where slug is ${prefix}/${filename}

#

I would then do a simple auth-checked local API .find() to look for the file

#

that all being said: I do agree with your proposal here. I'd really love to see the default GET endpoint for upload-enabled collections to have enhanced configurability

pearl fog
#

I also went down that route and turned back, this felt better and I'd be willing to make the contribution.