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?

Reply

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.

Reply

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

Reply

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.

Reply

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

Reply

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

Reply

I'm looking to add CloudFront for Uploads of new files only to help speed up uploads to S3. AWS docs say to use Amazon CloudFront's PUT/POST to do this for files under 1GB in size. I use CloudFLARE for my CDN etc on my app.

So I'm trying to figure out how I would configure CloudFront for uploads only, and set up my Rails app using Active storage for that.

Any thoughts on that?

Reply

Here's what worked for me:
I chose to abandon ActiveStorage completely (don't worry, this makes things easier). Once the upload button is clicked on the client, a GET /presigned_url is sent to the server and I use the aws gem to get an s3 presigned URL and send it back in the response. The client picks up this URL and uploads directly to it using XHR. Upon successful upload, S3 responds with the permanent location which the client finally POSTS back to rails to create the resource with a picture_url attribute. I also have a cdn_url method in my model which uses some regex to extract the filename and append it to my CloudFront domain.

This approach delegates the hard work to the client which has some speed benefits (only uploads to one location and doesn't keep the backend blocked while doing uploads). Let me know if you'd like to see the repo where I did this.

Reply
Join the discussion
Create an account Log in

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

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

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