Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to serve images assets from AWS S3 via Cloudflare CDN with Rails and Shrine Gem?

Tommy asked in General

I use the Shrine gem to upload images to AWS S3 from my Rails app.

Now I’m trying to load the asset trough Cloudflare CDN, but then my images fail to load.

Here’s my attempt to implement this

  1. Created an AWS S3 bucket with name cdn.myrealdomain.com
  2. Created a CNAME record in Cloudflare set to: Name: cdn and Host: cdn.myrealdomain.com.s3.eu-north-1.amazonaws.com



Now I can access the XLM for my bucket both trough
**s3.eu-north-1.amazonaws.com/cdn.myrealdomain.com
* and trough
cdn.myrealdomain.com/ *

3. Then I added this line to my shrine.rb to set the URL HOST to my cdn.myrealdomain.com



plugin :url_options, store: { host: "https://cdn.myrealdomain.com" }


4) Now. when I start my rail server, all image assets fail to load.
If I copy image address and paste it into my browser it shows error
SignatureDoesNotMatch

**Here's the URL generated by the image_tag helper in rails (that is NOT working)**



https://cdn.myrealdomain.com/store/48a9048cb8e5f31306417e2a1a3c7bf3.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJ5I6Z37AW6G7Q5PQ%2F20201113%2Feu-north-1%2Fs3%2Faws4_request&X-Amz-Date=20201113T121155Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=1c82f8c15fcfbfe59d24a74a560a5837696a10ac49cdadeaa971b6565700faa8




However if I delete the trailing characters after
https://cdn.myrealdomain.com/store/48a9048cb8e5f31306417e2a1a3c7bf3.png
The image load properly.



Any suggestions to resolve this would be very much appreciated!

Reply
Join the discussion
Create an account Log in

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

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

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