Paperclip including bucket name in cloudfront url - ruby-on-rails

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.

Related

Rails Paperclip adds extra folder pointing to Cloudfront (between :url and :path)

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

Trouble uploading files to Amazon s3 in development

Able to upload files to Amazon s3 in my production environment with Heroku, but unable to do this in my development environment. Here is how the situation looks currently
Development.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']
}
}
After following the advice of this S.O. Post, I created a file application.yml in my config directory with the following code:
AWS_ACCESS_KEY_ID: "***"
AWS_SECRET_ACCESS_KEY: "***"
AWS_BUCKET: "themoderntrunk"
I didn't change my design model though
has_attached_file :photo, :styles => { :thumbnail => "80x80#",
:small => "150x150>"
}
When I'm uploading file's now, i get this error
missing required :bucket option
Many people I've seen had this same problem, but none of the answers have been able to solve my problem. Any guidance would be truly appreciated. Thanks.
Allegorically, this particular issue has commonly been resolved using Fog, Ruby's canonical cloud services library.
# Gemfile
gem 'fog'
Run bundle install, then modify your configuration file as follows:
# config/environments/development.rb
Paperclip::Attachment.default_options.merge!(
:storage => :fog,
:fog_credentials => {
:provider => 'AWS',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
},
:fog_directory => ENV['AWS_BUCKET'],
:bucket => ENV['AWS_BUCKET']
)
Restart your server to reload the environment – uploads should work.
UPDATE:
Upon a rereading of your question, I noticed that you're to passing an environment global named ENV['S3_BUCKET_NAME'], but the variable you should actually pass is named ENV['AWS_BUCKET']. I suspect this is why the missing required :bucket option was being thrown. Renaming the variable may resolve your original issue:
# config/environments/development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'], # Matches global declaration in `application.yml`
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}

Rails Paperclip S3 - missing required :bucket option

I'm trying to use Amazon S3 for Paperclip attachments. First, I'm trying to get it to work in development environment on my iMac.
I have created the Amazon buckets = ndeavor-dev and ndeavor-pro. In the code below, I have substituted the bucket name and keys. I have the gem's paperclip and aws-sdk.
The error I get is:
ArgumentError at /attachments
missing required :bucket option
I have tried this in my config/environments/development.rb:
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:bucket => ENV['AWS_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
And I tried this (moving the :bucket):
config.paperclip_defaults = {
:storage => :s3,
:s3_protocol => 'http',
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Thanks for the help!
Like dcro says, you need to set the AWS_BUCKET environment variable properly.
To do this, create a file at config/application.yml and put the following in it, using your Amazon credentials:
AWS_ACCESS_KEY_ID: "whatever_the_key_is"
AWS_SECRET_ACCESS_KEY: "whatever_the_secret_is"
AWS_BUCKET: "ndeavor-dev"
Then restart your server. You'll then be able to use your models something like this:
has_attached_file :attachment ,
:storage => :s3 ,
:s3_credentials => {:bucket => ENV['AWS_BUCKET' ],
:access_key_id => ENV['AWS_ACCESS_KEY_ID' ],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']},
:s3_protocol => "https" ,
:s3_host_name => "s3-eu-west-1.amazonaws.com"

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