R2 storage from env + filename normalization

This commit is contained in:
2026-08-27 15:41:47 +02:00
parent 9359f26788
commit 0dee178ded
4 changed files with 94 additions and 8 deletions
+16
View File
@@ -38,9 +38,25 @@ import { s3Storage } from '@payloadcms/storage-s3';
for (const slug of collections){
collectionsConfig[slug] = true;
}
// Public URL for served media. R2 is private by default; with a custom domain
// (media.klient.pl → bucket) set R2_PUBLIC_URL so Payload generates URLs
// pointing there instead of the private S3 endpoint (which 403s on the front).
// Without it, uploads work but images don't display publicly. See docs/storage.md.
const publicUrl = process.env.R2_PUBLIC_URL?.replace(/\/$/, '') // strip trailing slash
;
return s3Storage({
bucket,
collections: collectionsConfig,
...publicUrl ? {
// generateFileURL overrides the stored/returned URL to the CDN domain.
// Params come from Payload's storage plugin; type them explicitly since
// the callback shape isn't inferred here (would be implicit any).
generateFileURL: ({ filename, prefix })=>[
publicUrl,
prefix,
filename
].filter(Boolean).join('/')
} : {},
config: {
credentials: {
accessKeyId,