Rails Paperclip adds extra folder pointing to Cloudfront (between :url and :path) - ruby-on-rails

I'm having trouble with loading images uploaded to s3 via my cloudfront distribution. I can get it to work fine loading straight from s3, but not from cloudfront.
I have set the config.action_controller.asset_host to my distribution url and have set up paperclip
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-sa-east-1.amazonaws.com',
:url => ':asset_host',
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
with the appropriate code in the model :path =>
':class/:id/:style/:class:style:id.:extension'
and every time I upload a file and try to access it i get
https://XXXXXX.cloudfront.net/images/:class/:id/:style/:class:style:id.:extension
I honestly can't figure out where the /images/ is coming from that is added between the url and the path. And I would be very greatful for any help or tips to solve this!
Thanks

Related

set up paperclip with amazon s3

I am trying to set up the paperclip with amazon s3, I have found a tutorial which is like the following code. What do I have to put as a url and path ?
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},
:url => ':s3_domain_url',
:path => "/:class/:attachment/:id_partition/:style/:filename"
}
You don't have to worry about touching url, unless you care about whether or not the URL is placed domain-style (bucket-name.s3.amazonaws) or path-style (s3.amazonaws.com/bucket-name). You could not add that in your configurations and it will default to domain-style.
Path tells Paperclip what path to save your attachments. This means in local file system, it will save it under "(model's name)/(attachment name)/(id)/(style e.g. small, thumbnail)/(file name)." It will do the same thing for your bucket, so you'll have all these fun folders to click through if you view your physical bucket. You're free to leave it to the default, or change it if you want your path to be a little shorter.

Paperclip including bucket name in cloudfront url

When I try to use Cloudfront as my CDN the url incorrectly includes the bucket name such as
cloudfronturl.net/bucketname/pathToImage
instead of
cloudfronturl.net/pathToImage
on my image model
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:url => ':s3_domain_url',
:s3_host_name => ENV['CLOUDFRONT_URL']
}
I could try using a gsub on the image urls to replace ['AWS_BUCKET']+"/" but is there a way to configure my cloudfront or paperclip to do this automatically?
This was a pretty silly mistake. The url, path and host alias need to go on the same level as the s3_credentials
:url => ':s3_alias_url',
:s3_host_alias => ENV['CLOUDFRONT_URL'],
:path => ":attachment/:id/:style.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
Seems as if your not specifying a path within your Paperclip config. You may need something like this
path: '/images/folder_name/:id/:style.:extension',
This is what i do and have never encountered the issue you specify with Cloudfront, i use it all the time.

How to edit production.rb in my rails app to get it working using heroku and paperclip?

I am using paperclip + AWS in my rails app to upload images.
When I reference images in my view as
<%= image_tag product.avatar.url(:medium) %>
current image urls are showing up as:
http://s3.amazonaws.com/rockywolfugc/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
I need them to show up as:
http://rockywolfugc.s3-us-west-2.amazonaws.com/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
At http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3 I'm seeing there is an option for ":s3_domain_url" but I'm not quite sure how to use it.
Below is the relevant portion of production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Where do I integrate the s3_domain_url in this file? Additionally, what do I have to do on heroku to get this to run? Example: heroku config:set xxxx=yyyy
try setting
config.action_controller.asset_host = "//#{ENV['AWS_BUCKET_NAME']}.s3-us-west-2.amazonaws.com"
in your production.rb

How to automate a paperclip upload to amazon s3 in rails app

I have some images that I want to move from myBucket/... to myBucket/someFolder/.... I have paperclip and aws-sdk setup to put new images in that location when uploaded on a form, but I can't figure out how to move the images to the location defined in production.rb:
config.paperclip_defaults = {
:storage => :s3,
:url => ":s3_eu_url",
:s3_protocol => "https",
:path => ":class/images/000/000/:id/:style/:basename.:extension",
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
How do you call paperclip on an image in a remote location in a controller method? I want something in my illustrationsController like the following:
def convert
#illustration = Illustration.find(params[:id])
Paperclip.someMagicMethod(#illustration.image_url)
end
Thanks.

Using a non - US s3 server with rails, heroku and paperclip

I'm trying to set this up using the article on herokus developer site.
https://devcenter.heroku.com/articles/paperclip-s3
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
:url => 'https://s3-ap-southeast-2.amazonaws.com'
}
}
However all of my images are pointing to the us http://s3.amazonaws.com/ server
How can i set paperclip to use the sydney server?
UPDATE:
paperclip successfully uploads files to the sydney server, it just wrongly uses the US server rather than the sydney one when trying to view them
Paperclip provides a handful of options, to optimize the configuration of your bucket.
The ones you're interested are url and s3_alias_url.
Your configuration would look like something like this :
config.paperclip_defaults = {
:storage =>:s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:url => ':s3_alias_url',
:s3_alias_url => 's3-ap-southeast-2.amazonaws.com'
}
You can find more information and an other example in this google groups discussion.

Resources