I've setup an s3 bucket for my media files and I'm using the cloud storage plugin. It's working but I've noticed the images are very slow to load now. So I've tried to setup CloudFront to use with the bucket.
My S3 bucket is setup to block public access, but when I created my CloudFront distribution it generated a policy for me to add to my S3 bucket policy, which I've added. Now I have a distribution domain name.
So what do I do next to get the cloud storage plugin working with my CloudFront CDN?
Thanks
@.kilosierra So in this case you'd need to override the image URL to have the new distro domain name, correct?
There is a
generateFileURL
function that can be defined in your plugin options:
https://github.com/payloadcms/plugin-cloud-storage#plugin-optionsThis function would need logic to properly prefix the image with your new distro domain name.
Here's the function def:
https://github.com/payloadcms/plugin-cloud-storage/blob/master/src/types.ts#L56My S3 bucket doesn't have public access so how would I authenticate via my access/secret key? My understanding is having a bucket with public access is not recommended. Changing the url to point to the bucket via the CDN would throw a "Access Denied" error. I originally wanted to avoid the complexities of hosting myself on AWS which is why I decided to use railway. However, it seems like using a service like railway is redundant if I have to setup separate cloud storage for media and configure payload to work with a seperate CDN.
Can you explain the generateFileURL is some more detail please. I'll be honest, it's starting to get beyond my skill level now. Do I have to make the bucket public and then create a function which returns "www.cdn-domain.com/media/media-file-name.jpg? Is there not a more simple way to not lose all my media files every time I redeploy?
Is there not a more simple way to not lose all my media files every time I redeploy?
This is due to the fact that your provider doesn't have persistent disk storage, which makes it so you need to use cloud storage of some sort.
Others have used the
generateFileURL
function to accommodate CDN distros. This is an example of how it can be used
media: {
adapter,
generateFileURL: ({ filename, prefix }) => {
return ['https://my-cloudfront-distro.com', prefix, filename].filter(Boolean).join('/')
},
},
Worth noting that this is one of the complexities that Payload Cloud solves, if you wanted to check it out.
@.kilosierra how did you set up your .env? I can't wire up my Cloudfront distro with this plugin...
ok figured this out ... just ignore me
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.