All threads / Active Storage, AWS S3 and Cloudfront

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Active Storage, AWS S3 and Cloudfront

Alex Deering asked in Rails

Im trying to setup cloudfront for my aws s3 storage of user uploaded images. Not sure what settings to change in the storage.yml file to point to the cloudfront and still work with everything else. Would it be just changing the endpoint variable to the cloudfront url?

Hey Alex,
Cloudfront would actually be setup in the console or cli, terraform etc.. which would then cache content from your s3 bucket at edge locations. As far as your storage.yml, you just use s3 like normal. Any files uploaded through the app would then get picked up by cloudfront automatically (although new assets won't get cached until the edge location gets a request for that asset).

As far as linking, use your cloudfront distribution url that you got when you set it up and AWS will take care of routing to the closest edge location for you.

Hope that helps.

Ok, ill give it a shot. So shouldnt need to change anything in my code and just set up the cloudfront in AWS

Not for storage.yml. If you want to link to any of those assets though, you'll need to use the cf distribution domain in the link.

So how do I change the activestorage url to use the CF url?

Sorry Alex, I think I initially misunderstood what you were trying to do but now I think I understand. I did some quick research and it doesn't look like there is any "out of the box" support for cloudfront in active storage atm. I did find a gist that might help you though, assuming you haven't tried it already. I'll do some further looking as well and see if there might not be a better solution. Gist url is https://gist.github.com/timm-oh/10c4f06effa536ff32c5d038e0dd57e1

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.