You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Duplicate S3 uploads when clientUploads=true and modifying prefix in beforeOperationHook #13299
In my current setup I would like to organize my S3 and separate it by projects, here is an example:
my-s3-bucket/
├── projects/
│ └── {projectID}/
│ ├── textures/
│ ├── meshes/
│ └── images/
└── textures/ (global textures, so i can share with other projects)
To achieve this I enabled clientUploads (deploying on Vercel) and I added a hook on my "textures" collection that has already a default prefix in the payload config called "textures".
Please add a reproduction in order for us to be able to investigate.
Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.
Why was this issue marked with the invalid-reproduction label?
To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with create-payload-app@latest -t blank or a forked/branched version of this repository with tests added (more info in the reproduction-guide).
To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.
Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.
I added a link, why was it still marked?
Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.
This behavior is due to the timing of client uploads vs hooks. When clientUploads is enabled, the file is uploaded to S3 immediately using the adapter’s static prefix, before any hooks run. When your beforeOperation hook later sets a different prefix, Payload treats that as a new location and ends up saving the file again under the updated path.
If you want to continue with this approach, you can clean up the original upload by deleting it in a hook that runs after the operation completes, for example:
import{S3Client,DeleteObjectCommand}from'@aws-sdk/client-s3'exportconstafterOperation=async({ operation, result })=>{if(operation!=='create')returnawaitnewS3Client({region: process.env.AWS_REGION}).send(newDeleteObjectCommand({Bucket: process.env.S3_BUCKET!,Key: `textures/${result.filename}`,}))}
This works, but it’s not ideal and adds extra work. There’s currently no supported way to have dynamic per-document upload paths with clientUploads enabled. To do that properly, the adapter would need to generate the signed URL using a dynamic prefix so the file uploads to the right location from the start.
Converting this to a feature request since it requires new functionality rather than a bug fix.
Activity
github-actions commentedon Jul 28, 2025
Please add a reproduction in order for us to be able to investigate.
Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.
Why was this issue marked with the
invalid-reproductionlabel?To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with
create-payload-app@latest -t blankor a forked/branched version of this repository with tests added (more info in the reproduction-guide).To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.
Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.
I added a link, why was it still marked?
Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.
Useful Resources
ysaied631 commentedon Sep 10, 2025
I am also running into this issue.
Here are possible files where the issue lies:
https://github.com/payloadcms/payload/blob/main/packages/storage-s3/src/client/S3ClientUploadHandler.ts
payload/packages/storage-s3/src/index.ts
Line 112 in 15e462b
https://github.com/payloadcms/payload/blob/main/packages/plugin-cloud-storage/src/utilities/initClientUploads.ts
JessRynkar commentedon Feb 5, 2026
Hi @cstamati, sorry for the delay here!
This behavior is due to the timing of client uploads vs hooks. When
clientUploadsis enabled, the file is uploaded to S3 immediately using the adapter’s static prefix, before any hooks run. When yourbeforeOperationhook later sets a different prefix, Payload treats that as a new location and ends up saving the file again under the updated path.If you want to continue with this approach, you can clean up the original upload by deleting it in a hook that runs after the operation completes, for example:
This works, but it’s not ideal and adds extra work. There’s currently no supported way to have dynamic per-document upload paths with
clientUploadsenabled. To do that properly, the adapter would need to generate the signed URL using a dynamic prefix so the file uploads to the right location from the start.Converting this to a feature request since it requires new functionality rather than a bug fix.