Skip to content

Duplicate S3 uploads when clientUploads=true and modifying prefix in beforeOperationHook #13299

Closed

Description

@cstamati

Describe the Bug

Hello,

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".

export const beforeOperation: CollectionBeforeOperationHook = ({
	args,
	operation,
}) => {
	if (operation === "create") {
		if (args.data) {
			args.data.prefix = `projects/${args.data.project}/textures`;
			console.log("beforeOperation :: ", args.data.prefix);
		}
	}
	return args;
};

When I upload a texture in my S3 the file is uploaded 2 times in:

my-s3-bucket/
├── projects/
│   └── {projectID}/
│       ├── textures/**my-file.png**
└── textures/**my-file.png**

How can I achieve this?

Link to the code that reproduces this issue

(cannot provide repository since it needs S3 to reproduce)

Reproduction Steps

  • create blank project
  • add the s3 adapter and configure it
  • enable clientUploads
  • add the beforeOperationHook (see description)
  • try to upload from admin

Which area(s) are affected? (Select all that apply)

plugin: other

Environment Info

Binaries:
  Node: 22.14.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.49.0
  next: 15.3.5
  @payloadcms/db-mongodb: 3.49.0
  @payloadcms/email-nodemailer: 3.49.0
  @payloadcms/graphql: 3.49.0
  @payloadcms/next/utilities: 3.49.0
  @payloadcms/payload-cloud: 3.49.0
  @payloadcms/plugin-cloud-storage: 3.49.0
  @payloadcms/richtext-lexical: 3.49.0
  @payloadcms/storage-s3: 3.49.0
  @payloadcms/translations: 3.49.0
  @payloadcms/ui/shared: 3.49.0
  react: 19.1.0
  react-dom: 19.1.0
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32536
  Available CPU cores: 16

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Jul 28, 2025
  2. github-actions commented on Jul 28, 2025

    @github-actions
    Contributor

    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.

    Useful Resources

  3. removed
    validate-reproduction
    on Jul 28, 2025
  4. removed
    status: needs-triage
    on Jan 20, 2026
  5. JessRynkar commented on Feb 5, 2026

    @JessRynkar
    Member

    Hi @cstamati, sorry for the delay here!

    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'
    
    export const afterOperation = async ({ operation, result }) => {
      if (operation !== 'create') return
    
      await new S3Client({ region: process.env.AWS_REGION }).send(
        new DeleteObjectCommand({
          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.

  6. locked and limited conversation to collaborators on Feb 5, 2026
  7. converted this issue into a discussion #15532 on Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions