Created
November 18, 2025 01:51
-
-
Save ilhamsj/90bffb1ece44fd13ac138b49886d8124 to your computer and use it in GitHub Desktop.
Payload S3 Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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