How to store files on amazon S3 conditionally based on what environment you are in - ruby-on-rails

I'm using Paperclip, and this code, along with the aws-s3 gem, allows me to store file uploads with Amazon S3:
has_attached_file :photo,
:styles => {
:tiny => "25x25#",
:shown => "40x40#",
:thumbnail => "50x50#",
:small => "150x150>",
:medium => "300x300>" },
:default_url => "/images/default_:style.jpg",
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "profile/:attachment/:style/:id.:extension"
However I do not want to store files on Amazon S3 when I am in my development environment. How do I set that in my application?

you could probably do something like
:storage => Rails.env.production? ? :s3 : :whatever

In the end of environment.rb:
APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
In config/config.yml:
development:
use_amazon: false
test:
use_amazon: false
production:
use_amazon: true
And in your controller:
if APP_CONFIG['use_amazon']
#USING AMAZON S3
else
#USING SOMETHING ELSE
end
This should work.
Good Luck!

Related

File upload to Amazon S3 using paperclip

I'm uploading files to S3 using paperclip so would like to know if i can set the path something like,
:path => "/advertisements/:username/:filename”
the thing is that :usename is from other model; i'm uploading files on model_2 and :username comes from model_1. How can i set the path to indicate the :username
Sample:
:path => "/advertisements/#model_1.username/:filename”
Any ideas?
Thanks in advance!
Here is nice explanation:
please view the answer.
Rails 4, Paperclip, Amazon S3 Config Amazon Path
Model:
#Image Upload
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "****",
:storage => :s3,
:bucket => '****',
:s3_credentials => S3_CREDENTIALS,
:url => "/:image/:id/:style/:basename.:extension",
:path => ":image/:id/:style/:basename.:extension"
config/application.rb
# Paperclip (for Amazon) (we use EU servers)
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
config/s3.yml
Amazon AWS Config
development:
access_key_id: **********
secret_access_key: **************
bucket: ****
production:
access_key_id: ***********
secret_access_key: ***********
bucket: ****
I hope this is what you're looking for :)

Rails 4 + Paperclip + Devise + S3: Images not uploading to S3?

I have an Active model (you can think of an Active as a User) that has authentication setup with Devise. I am trying to add a photograph attribute to my Active model and be able to upload pictures with S3.
Migration:
class AddAttachmentPhotographToActives < ActiveRecord::Migration
def self.up
change_table :actives do |t|
t.attachment :photograph
end
end
def self.down
drop_attached_file :actives, :photograph
end
end
Active model:
...
has_attached_file :photograph,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :s3,
:default_url => '/images/:attachment/missing_:style.png',
:path => "users/:id/photograph/:style.:extension",
:bucket => ... ,
:s3_credentials => {
:access_key_id => " ... ",
:secret_access_key => " ... "
}
Both config/environments/production.rb and config/environments/development.rb have the following:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV[' ... '],
:access_key_id => ENV[' ... '],
:secret_access_key => ENV[' ... ']
}
}
I have my form for uploading a picture in views/devise/registrations/edit.html.erb like so: <%= f.file_field :photograph %>. However, after I select and update an Active with this form (the update goes through successfully), the path to my image (generated with <%= image_tag #active.photograph.url %>) is:
http://localhost:3000/images/photographs/missing_original.png
instead of an S3 address.
Also note I am using the following gems:
gem "paperclip", "~> 3.0"
gem 'aws-sdk'
I have never used S3 before, but after I select an image to upload and hit "enter" on my update page, my S3 bucket on the Amazon portal is still empty, so the update never went through.
Did I not set something up correctly?
Your code looks fine - are you sure you have S3 set up correctly?
Here is the code we use - hopefully this will help:
#app/models/image.rb
Class Image < ActiveRecord::Base
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "***********",
:storage => :s3,
:bucket => '*********',
:s3_credentials => S3_CREDENTIALS
end
#config/initializers/s3.rb
if Rails.env == "production"
# set credentials from ENV hash
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "firststop"}
else
# get credentials from YML file
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end
#config/s3.yml
development:
access_key_id: **************
secret_access_key: ***************
bucket: *******
This works fine for us
Don't leave those keys in your code. At the minimum, place creds in config/environment.rb file:
S3_KEY='myKey'
S3_SECRET='mySecret'
#etc.
Then reference them in your code as S3_KEY, etc.
More on this and related methods: How do I store keys for API's in Rails?

Paperclip + AWS S3, Prefixing remote path with my local path

I'm using Paperclip with a Rails 4 app and Amazon S3 storage. On my development machine, the site is running at
/Users/Jeff/Sites/example.com/web
When I upload a file with Paperclip to S3, the remote path in S3 inherits my local folder structure.
http://s3.amazonaws.com/example_com_bucket/Users/Jeff/Sites/example.com/web/public/assets/uploads/my_class/8/medium/some_image.png?1383060287
Why is this happening? How do I strip that part out? I tried changing the :path property but that only seemed to affect the "application" part of the path (e.g. after /assets/uploads) My site is still in development, so I don't care about having to preserve links.
My config is...
config.paperclip_defaults = {
:storage => :s3,
:path => '/:class/:attachment/:id_partition/:style/:filename',
:s3_credentials => {
:bucket => 'example_com_bucket',
:access_key_id => '...',
:secret_access_key => '...'
}
}
I had this exact same issue when I was using the :url parameter where I should have been using the :path parameter:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:url => '/product/:attachment/:id/:style/:filename'
I fixed it by changing my config to this:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:path => '/product/:attachment/:id/:style/:filename'

Rails paperclip amazon aws s3 gem, how to change image url?

In my model I have:
has_attached_file :image,
:storage => :s3,
:styles => { :original => ["300x250>", :png], :small => ["165x138>", :png], :mini => ["120x120>", :png] },
:path => 'images/vind/:style/:id/:basename.:extension',
:url => 'images/vind/:style/:id/:basename.png',
:bucket => 'konkurrencerher',
:s3_credentials => {
:access_key_id => 'x',
:secret_access_key => 'x'
}
The problem is just that there is added the amazon s3 hostname to the url in view.
I have a solution to this, but is a bit ugly:
<%= image_tag(kon.photo.image.url(:small).gsub("http://s3.amazonaws.com/konkurrencerher", ""), :class => 'koni') %>
But, how is it possible to define the image url in the model, without the Amazon S3 hostname?
Take a look at Paperclip::Storage::S3, especially on the :s3_host_alias.
You can try configuring your has_attached_file with the following additional options
:url => ':s3_alias_url',
:s3_host_alias => "example.domain.net"
Hope this helps.
My solution created a file in the initializers map with this:
Paperclip.interpolates(:s3_path_url) { |attachment, style|
"#{(attachment.path).gsub("images/", "")}"
}
And then the url should be:
:url => ':s3_path_url'
This is a much better solution.

Paperclip Amazon S3 setup with Heroku

has_attached_file :image, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => "/:style/:filename"
I'm not sure what :path => "/:style/:filename" is.
I also want to to include the style for this attached image, is that what the :path is?
the style I want is this: :styles => { :medium => "275x275>", :thumb => "175x155>" }
Basically what's going on here is that I'm setting up on heroku and I'm having to use S3 which seems straightforward just not used to this attachment convention stuff.
Also, I just signed up for an S3 account... but heroku was spouting that its free or something. What's the deal with that?
The 'path' specifies the location on S3 where the files will be stored. Thus, if you specify an attachment as:
has_attached_file :image,
:styles => { :medium => "275x275>", :thumb => "175x155>" },
:storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
:path => "user/:attachment/:style/:id.:extension"
A sample URL will be:
http://s3.amazonaws.com/bucket/user/image/thumb/347853856.jpg
Finally, S3 is NOT free (Heroku simply states transfer / uploads are not counted in the usage based calculations). Heroku's documentation is excellent if you need further information.
Note that in Rails 3.1 and above, it should be Rails.root and not RAILS_ROOT

Resources