Use CDN and HTTPS for Spree::Images - ruby-on-rails

Is it possible to use a CDN like Amazon Cloudfront with spree? I know I can set config.action_controller.asset_host in production.rb, but this doesn't affect the Spree::Image, or any spree helper functions like product_image().
Also, the /admin/image_settings/edit url with setting for s3_protocol, which seems to have no effect, even setting it to blank. I would like to be protocol agnostic, and have the URLs formed like //foo.cloudfront.com

Spree's image uploader is provided the Paperclip gem. There's a handy guide for Using Cloudfront with Paperclip. Paperclip will not use asset_host.
The first step would be to get your S3 image hosting working the way you want, and then get it to work through Cloudfront.
s3_protocol being '' should use protocol relative URL's as shown in this pull request.

Related

Enabling Rails CloudFront on Heroku

I followed this article on Heroku to enable CloudFront on my Rails 5 app hosted on Heroku. However, it doesn't seem to be working for my images (which are hosted on S3).
I have tried setting the Origin Domain Name in AWS to both by S3 bucket (selected from the dropdown), as well as my app's domain name.
However, when I look at the source code, the images are still using the S3 URL instead of the being prefaced with the CloudFront URL which leads me to believe that something is not set-up right.
When I look at the source, I can see the some things are using the preface, such as my favicon https://mycloudfront.cloudfront.net/assets/favicon-28fde7db6babda6b94460b806d567abe4521f8dd77bc3741debadbf30eeecb19.ico.
I saw this post
Rails Cloudfront assets not served, but I'm using image_tag on the images showing the S3 URL in the source code.
in production.rb
config.action_controller.asset_host = "https://mycloudfront.cloudfront.net/"

Rails: Refile gem (attachment_image_tag prints wrong URL)

I'm using refile gem for file uploading and I'm uploading files directly to my amazon s3 public bucket. The files are being uploaded just fine to following URL:
https://s3.amazonaws.com/{bucket-name}/store/6309035935359dca1ef549170e7facac9ab3553ad53946cea0bcb8335475
I'm using amazon cloudfront to serve these assets from s3, so the cloudfront url, for the asset given above, is something like this:
https://{distribution-name}.cloudfront.net/store/6309035935359dca1ef549170e7facac9ab3553ad53946cea0bcb8335475
The both urls when accessed directly through browser works fine. To display this asset in my app view file I'm using refile gem following helper function:
attachment_image_tag(#post, :profile_img, :fill, 350, 350)
Now the issue is, this helper method prints a URL something like this:
https://{distribution-name}.cloudfront.net/attachments/5845d0a97dc77a57b5b05742a1eb26d629d7cdaa/store/fill/350/350/6309035935359dca1ef549170e7facac9ab3553ad53946cea0bcb8335475/file
As this URL is different from above cloudfront URL, it is not displaying the image. How do I force attachment_image_tag function to print the correct cloudfront URL?
The issue was resolved. The issue was pointed out by another guy on github: https://github.com/refile/refile/issues/500#issuecomment-239784247
It sounds like you may have set up your Cloudfront distribution with
your S3 bucket as its origin? Refile, however, does not generate URLs
directly to files on S3 (see README section 3. Rack Application).
You'll therefore want to set your Cloudfront distribution's origin to
be the URL of your application. That way, files will be streamed from
your application on the first request, and cached by Cloudfront for
future requests.

What header settings do I need to use with Cloudfront so that the bootstrap glyphicons render in my rails app?

I have Cloudfront setup as my cdn (first time I have tried using it). The site renders perfectly without Cloudfront but with it the glyphicons disappear. To see what I mean look here. The square boxes are supposed to be arrows.
I read in another SO post that I need to set my cors headers in Cloudfront but I cannot find any information on what the specifics of doing this are (I have already installed and setup rack-cors gem). Currently the cdn 'Forward headers' are set to none. Which one/s do I need to turn on to get the glyphicons to render or is their another solution to this?

Configuring a Heroku Rails app for CKEditor to work on Cloudfront

I'm using this gem to integrate CKEditor into my Rails app, with paperclip to handle image uploading.
I no longer want Heroku to serve my CKEditor-uploaded images anymore, so I'm switching to Cloudfront and found this tutorial.
As I understood (from the tutorial), I can use Cloudfront without using an S3 bucket, as Cloudfront will automatically fetch my precompiled static assets stored on Heroku. But I'm so confused:
Will image uploaded via CKEditor be included in the asset pipeline? So that when precompiled, will be served by Cloudfront?
The tutorial said I need to change all image links to <%= image_tag('...') %> so that it'll point to Cloudfront and work. But this is only possible when I hard-coded the image, not the case when a user uploaded one in his text and stored in the database. Am I wrong and how to solve it?
Will this method (not using S3 bucket) work for other "dynamic" images such as User's avatars, post's cover images, etc...?
I don't want to use S3 bucket as it will involve the use of asset_sync. Any help is appreciated, thanks!

Adding Additional Headers to Carrierwave for Amazon s3 Encryption

In short
In short I want to know if I can send additional headers through a carrierwave and fog connection to Amazon s3?
In depth
I recently found that amazon supports Client and Server side encryption of files. more info ยป http://docs.amazonwebservices.com/AmazonS3/latest/dev/SSEUsingRESTAPI.html
I'm currently using carrierwave in a rails app to upload files to amazon s3.
For server side encryption amazon asks for a header of x-amz-server-side-encryption=AES256 added to the request.
So I'm looking to figure out how to send additional headers through with my carrierwave and fog.
My thought was that maybe I could use the fog_attribute config line something like the following and maybe that might work but I'm not sure the fog_attribute is for partiular attribute or just a blanket header section.
config.fog_attributes = {'x-amz-server-side-encryption' => 'AES256','Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
So I finally got my app in shape to test this but unfortunately it didn't work.
I also found this: https://github.com/geemus/fog/commit/070e2565d3eb08d0daaa258ad340b6254a9c6ef2 commit in the fog repository that make me feel the fog_attributes method is for a defined list of attributes.
There has got to be a way to make this work. Anyone?
I believe that should actually be correct, note however that I don't believe the server side encryption stuff has been released, so you would need to use edge fog to get this behavior. I hope to do a release soon though and then it should be good to go. If you find that you still can't get it working on edge let me know though and we'll try and see what can be done.
I cannot speak about CarrierWave, but this works for saving files with AWS256 encryption with the (currently) standard Fog distribution:
file.attributes[:encryption ] = "AES256"
result = file.save()
However, that does not work for copying files. What works for copying is:
fogfile.copy(#bucket_archived, newfilename, {'x-amz-server-side-encryption' => 'AES256'})

Resources