Skip to content

Instantly share code, notes, and snippets.

@ilhamsj
Created November 18, 2025 01:51
Show Gist options
  • Select an option

  • Save ilhamsj/90bffb1ece44fd13ac138b49886d8124 to your computer and use it in GitHub Desktop.

Select an option

Save ilhamsj/90bffb1ece44fd13ac138b49886d8124 to your computer and use it in GitHub Desktop.
Payload S3 Plugin
import { s3Storage } from '@payloadcms/storage-s3'
// Generate date-based prefix function (folder/collection/YYYY/MM format)
// Returns a getter so date is evaluated at upload time, not config time
const createMediaPrefix = (folder: string, collection: string) => ({
get prefix() {
const now = new Date()
return [folder, collection, now.getFullYear(), String(now.getMonth() + 1).padStart(2, '0')]
.filter(Boolean)
.join('/')
},
})
export const configStorage = s3Storage({
bucket: process.env.S3_BUCKET!,
collections: {
media: createMediaPrefix('payload', 'media'),
},
config: {
...(process.env.S3_ACCESS_KEY_ID &&
process.env.S3_SECRET_ACCESS_KEY && {
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
},
}),
...(process.env.S3_ENDPOINT && { endpoint: process.env.S3_ENDPOINT }),
...(process.env.S3_REGION && { region: process.env.S3_REGION }),
},
disableLocalStorage: true,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment