Ruby on Rails 4 - Paperclip AWS Image URL is wrong? - ruby-on-rails

I've used paperclip and AWS together before, but I just created this Rails 4 app, and my image.url method is showing looking for the image at 'http://s3.amazonaws.com/', when it should be looking for them at 's3-us-west-2.amazonaws.com'. I'm assuming this is because the region for the first account is US Standard, whereas the new one is Oregon.
Any way for me to change the region in the config files or something?

Apparently there used to be a bunch of workarounds for this, but now there is an option in the config called s3_host_name.
ExampleApp:Application.configure do
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => {
:bucket => '#',
:access_key_id => '#',
:secret_access_key => '#'
}
}
end

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

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.

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

Getting S3 Credentials from the database at startup in rails

Using a combination of the rails-settings gem and paperclip, I've got my application set up such that one can specify S3 credentials in the database (through an interface in the front end) and can then declare attachments like this;
class Asset < ActiveRecord::Base
has_attached_file :attachment,
:default_url => "content/no-img.jpg",
:s3_credentials => {
:access_key_id => Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => Settings.AWS_SECRET_ACCESS_KEY
}
This works great, but this isn't the only type of attachment in the application (there are other things such as client logos). I'm finding that I need to specify the S3 credentials on every one. I've already tucked some of the defaults away in my app config file;
class Application < Rails::Application
#Paperclip Defaults
config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name"
}
And this all works great, but if I try to pull down the username and password into the app config too, I get the following error;
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
A fairly self explanatory error message, but how do I get around it? If I put the config in the environment files I get the same issue, and if I put them in an initializer like this;
#Paperclip Defaults
MyApp::Application.config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name",
:s3_credentials => {
:access_key_id => RailsSettings::Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => RailsSettings::Settings.AWS_SECRET_ACCESS_KEY
}
}
Then the values don't seem to get picked up and paperclip defaults back to storing the files in my local system directory. Could anyone advise on the best approach to handling this?
EDIT
So after re-reading the documentation I discovered "After Initializers", which gets around the whole active-record not being loaded issue. I have this in my app config;
config.after_initialize do
#Paperclip Defaults
config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name",
:s3_credentials => {
:access_key_id => RailsSettings::Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => RailsSettings::Settings.AWS_SECRET_ACCESS_KEY
}
}
end
And specify this in each model;
has_attached_file :attachment,
:default_url => "content/no-img.jpg",
:path => "#{Rails.env}/:client_name/#{DateTime.now.year}/:name/:basename/:basename-:style.:extension"
yet it still isn't picking up the defaults I specify (it still tries to put them in the public/system folder)
Right, sorted. If anyone has this issue, here's the solution (you basically need to use the "initializers" method to switch active record on before you set the config);
initializer "active_record.initialize_database" do |app|
app.config.paperclip_defaults =
{
:storage => :s3,
:bucket => "my-bucket-name",
:s3_credentials => {
:access_key_id => RailsSettings::Settings.AWS_ACCESS_KEY_ID,
:secret_access_key => RailsSettings::Settings.AWS_SECRET_ACCESS_KEY
}
}
end

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