How can I set paperclip's storage mechanism based on the current Rails environment? - ruby-on-rails

I have a rails application that has multiple models with paperclip attachments that are all uploaded to S3. This app also has a large test suite that is run quite often. The downside with this is that a ton of files are uploaded to our S3 account on every test run, making the test suite run slowly. It also slows down development a bit, and requires you to have an internet connection in order to work on the code.
Is there a reasonable way to set the paperclip storage mechanism based on the Rails environment? Ideally, our test and development environments would use the local filesystem storage, and the production environment would use S3 storage.
I'd also like to extract this logic into a shared module of some kind, since we have several models that will need this behavior. I'd like to avoid a solution like this inside of every model:
### We don't want to do this in our models...
if Rails.env.production?
has_attached_file :image, :styles => {...},
:path => "images/:uuid_partition/:uuid/:style.:extension",
:storage => :s3,
:url => ':s3_authenticated_url', # generates an expiring url
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:s3_permissions => 'private',
:s3_protocol => 'https'
else
has_attached_file :image, :styles => {...},
:storage => :filesystem
# Default :path and :url should be used for dev/test envs.
end
Update: The sticky part is that the attachment's :path and :url options need to differ depending on which storage system is being used.
Any advice or suggestions would be greatly appreciated! :-)

I like Barry's suggestion better and there's nothing keeping you from setting the variable to a hash, that can then be merged with the paperclip options.
In config/environments/development.rb and test.rb set something like
PAPERCLIP_STORAGE_OPTIONS = {}
And in config/environments/production.rb
PAPERCLIP_STORAGE_OPTIONS = {:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:filename"}
Finally in your paperclip model:
has_attached_file :image, {
:styles => {:thumb => '50x50#', :original => '800x800>'}
}.merge(PAPERCLIP_STORAGE_OPTIONS)
Update: A similar approach was recently implemented in Paperclip for Rails 3.x apps. Environment specific settings can now be set with config.paperclip_defaults = {:storage => :s3, ...}.

You can set global default configuration data in the environment-specific configuration files. For example, in config/environments/production.rb:
Paperclip::Attachment.default_options.merge!({
:storage => :s3,
:bucket => 'wheresmahbucket',
:s3_credentials => {
:access_key_id => ENV['S3_ACCESS_KEY_ID'],
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY']
}
})

After playing around with it for a while, I came up with a module that does what I want.
Inside app/models/shared/attachment_helper.rb:
module Shared
module AttachmentHelper
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def has_attachment(name, options = {})
# generates a string containing the singular model name and the pluralized attachment name.
# Examples: "user_avatars" or "asset_uploads" or "message_previews"
attachment_owner = self.table_name.singularize
attachment_folder = "#{attachment_owner}_#{name.to_s.pluralize}"
# we want to create a path for the upload that looks like:
# message_previews/00/11/22/001122deadbeef/thumbnail.png
attachment_path = "#{attachment_folder}/:uuid_partition/:uuid/:style.:extension"
if Rails.env.production?
options[:path] ||= attachment_path
options[:storage] ||= :s3
options[:url] ||= ':s3_authenticated_url'
options[:s3_credentials] ||= File.join(Rails.root, 'config', 's3.yml')
options[:s3_permissions] ||= 'private'
options[:s3_protocol] ||= 'https'
else
# For local Dev/Test envs, use the default filesystem, but separate the environments
# into different folders, so you can delete test files without breaking dev files.
options[:path] ||= ":rails_root/public/system/attachments/#{Rails.env}/#{attachment_path}"
options[:url] ||= "/system/attachments/#{Rails.env}/#{attachment_path}"
end
# pass things off to paperclip.
has_attached_file name, options
end
end
end
end
(Note: I'm using some custom paperclip interpolations above, like :uuid_partition, :uuid and :s3_authenticated_url. You'll need to modify things as needed for your particular application)
Now, for every model that has paperclip attachments, you just have to include this shared module, and call the has_attachment method (instead of paperclip's has_attached_file)
An example model file: app/models/user.rb:
class User < ActiveRecord::Base
include Shared::AttachmentHelper
has_attachment :avatar, :styles => { :thumbnail => "100x100>" }
end
With this in place, you'll have files saved to the following locations, depending on your environment:
Development:
RAILS_ROOT + public/attachments/development/user_avatars/aa/bb/cc/aabbccddeeff/thumbnail.jpg
Test:
RAILS_ROOT + public/attachments/test/user_avatars/aa/bb/cc/aabbccddeeff/thumbnail.jpg
Production:
https://s3.amazonaws.com/your-bucket-name/user_avatars/aa/bb/cc/aabbccddeeff/thumbnail.jpg
This does exactly what I'm looking for, hopefully it'll prove useful to someone else too. :)
-John

How about this:
Defaults are established in application.rb. The default storage of :filesystem is used, but the configuration for s3 is initialized
Production.rb enables :s3 storage and changes the default path
Application.rb
config.paperclip_defaults =
{
:hash_secret => "LongSecretString",
:s3_protocol => "https",
:s3_credentials => "#{Rails.root}/config/aws_config.yml",
:styles => {
:original => "1024x1024>",
:large => "600x600>",
:medium => "300x300>",
:thumb => "100x100>"
}
}
Development.rb (uncomment this to try with s3 in development mode)
# config.paperclip_defaults.merge!({
# :storage => :s3,
# :bucket => "mydevelopmentbucket",
# :path => ":hash.:extension"
# })
Production.rb:
config.paperclip_defaults.merge!({
:storage => :s3,
:bucket => "myproductionbucket",
:path => ":hash.:extension"
})
In your model:
has_attached_file :avatar

Couldn't you just set an environment variable in production/test/development.rb?
PAPERCLIP_STORAGE_MECHANISM = :s3
Then:
has_attached_file :image, :styles => {...},
:storage => PAPERCLIP_STORAGE_MECHANISM,
# ...etc...

My solution is same with #runesoerensen answer:
I create a module PaperclipStorageOption in config/initializers/paperclip_storage_option.rb
The code is very simple:
module PaperclipStorageOption
module ClassMethods
def options
Rails.env.production? ? production_options : default_options
end
private
def production_options
{
storage: :dropbox,
dropbox_credentials: Rails.root.join("config/dropbox.yml")
}
end
def default_options
{}
end
end
extend ClassMethods
end
and use it in our model
has_attached_file :avatar, { :styles => { :medium => "1200x800>" } }.merge(PaperclipStorageOption.options)
Just it, hope this help

Use the :rails_env interpolation when you define the attachment path:
has_attached_file :attachment, :path => ":rails_root/storage/:rails_env/attachments/:id/:style/:basename.:extension"

Related

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 one type of attachment not being uploaded to S3, others are?

I have a class, called Report, and Reports are generated via a Rake task. I also use Paperclip for images (user avatars), and it uploads to my S3 bucket fine. Here is the top of the Report model:
require 'csv'
class Report < ActiveRecord::Base
attr_accessible :csv_file, :category
Paperclip.interpolates :category do |attachment, style|
attachment.instance.category.downcase
end
has_attached_file :csv_file,
path: (Rails.env.staging? || Rails.env.production?) ? ":class/:category/:basename.:extension" : ":rails_root/public/system/:class/:category/:basename.:extension"
And my Paperclip.rb file is this:
Paperclip.options[:log] = false
Paperclip.options[:command_path] = if Rails.env.dev?
"/usr/local/bin"
else
"/usr/bin"
end
PAPERCLIP_OPTIONS = {
:hash_secret => "HASHSECRETHERE",
:default_url => "http://placehold.it/:style",
:processors => [:thumbnail]
}
PAPERCLIP_STORAGE_OPTIONS = if Rails.env.staging? || Rails.env.production?
{ :storage => :s3,
:s3_credentials => "#{Rails.root}/config/apis/s3.yml",
:s3_permissions => :public_read,
:s3_protocol => :https,
:path => ":class/:attachment/:id_partition/:style/:hash.:extension" }
else
{ :path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:hash.:extension",
:url => "/system/:class/:attachment/:id_partition/:style/:hash.:extension" }
end
PAPERCLIP_OPTIONS.merge!(PAPERCLIP_STORAGE_OPTIONS)
It saves the reports to:
/system/reports/csv_files/000/000/002/original/general-report-2013-9-25-T-3-56-PM.csv?1380142571
rather than S3 like my avatars:
//s3.amazonaws.com/production/media/avatar-placeholder.gif
Anyone see why?
My guess is that your environment does not have RAILS_ENV (or RACK_ENV) set, so your rake task is being run in the development environment, which does not save to s3 because of this line :
PAPERCLIP_STORAGE_OPTIONS = if Rails.env.staging? || Rails.env.production?
You probably want to run your rake task like this:
RAILS_ENV=production rake do_some_thing
Fixed it by merging the options into PAPERCLIP_OPTIONS like so:
has_attached_file :csv_file, PAPERCLIP_OPTIONS.merge(
path: (Rails.env.staging? || Rails.env.production?) ? ":class/:category/:basename.:extension" : ":rails_root/public/system/:class/:category/:basename.:extension",
processors: []
)

Rails MIME::Types.type_for(photoshop_1354320001.psd) - MIME Type not found?

I'm using Rails 3, Paperclip(3.3.0), aws-sdk (1.7.1).
My paperclip attachments are being stored securely on S3.
attachment.rb
has_attached_file :attachment,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_protocol => 'https',
:s3_permissions => :private, # Sets the file, not the folder as private in S3
:use_timestamp => false,
:default_style => :original, # NEEDS to be original or download_url method below wont work
:default_url => '/images/:attachment/default_:style.png',
:path => "/:rails_env/private/s/:s_id/uuploaded_files/:basename.:extension"
In order to download the files I generate a secure URL like so:
def authenticated_url(style = nil, expires_in = 1.hour)
mime_type = MIME::Types.type_for(self.attachment_file_name)[0]
attachment.s3_object(style).url_for(:read, :secure => true, :response_content_type => mime_type.to_s, :expires => expires_in).to_s
end
The problem is for PSDs: This is return empty:
Rails MIME::Types.type_for('photoshop_1354320001.psd')
In the code it looks like:
mime_type = MIME::Types.type_for(self.attachment_file_name)[0]
It works for other files fine but not PSDs. Any idea why and how to resolve?
Thanks
Sure. MIME::Types lets you specify custom types.
Stick this into an initializer
# Not quite sure what the appropriate MIMEtype for PSDs are,
# but this is the gist of it.
# .PSB is a larger version of .PSD supporting up to 300000x300000 px
psd_mime_type = MIME::Type.new('image/x-photoshop') do |t|
t.extensions = %w(psd psb)
t.encoding = '8bit'
end
MIME::Types.add psd_mime_type
Now MIME::Types.type_for "test.psd" should give you "image/x-photoshop".

Amazon S3 path or to_file wont work

Hey guys ive got the following code in my Model
require 'RMagick'
class Upload < ActiveRecord::Base
belongs_to :card
has_attached_file :photo,
:styles => {
:thumb => ["100x100", :jpg],
:pagesize => ["500x400", :jpg],
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'your_deck',
:default_style => :pagesize
attr_accessor :x1, :y1, :width, :height
def update_attributes(att)
scaled_img = Magick::ImageList.new(self.photo.path)
orig_img = Magick::ImageList.new(self.photo.path(:original))
scale = orig_img.columns.to_f / scaled_img.columns
args = [ att[:x1], att[:y1], att[:width], att[:height] ]
args = args.collect { |a| a.to_i * scale }
orig_img.crop!(*args)
orig_img.write(self.photo.path(:original))
self.photo.reprocess!
self.save
super(att)
end
end
This code works offline however when on Heroku with Amazon S3 it fails to work, ive tried the code with to_file and it also wont work
I get the following error
can't convert Array into String
I had the same problem and it's due to an update to paperclip. I'm surprised heroku are still using this gem version as it will surely affect all their users; I installed a previous version as a plugin and it's fine. Don't forget to remove the gem from your .gems file or specify the previous version in your gems manifest.

AWS::S3::MissingAccessKey in Paperclip but I've defined both

I'm on Heroku, and this is a portfolio thing which I'm putting up on github for potential employers to look at, so obviously I don't want to stick my keys in a S3.yml file. I've exported S3_KEY and S3_SECRET to my environment both on Heroku and my machine and ruby can access them. But when I try and upload, it gives me the following error:
AWS::S3::MissingAccessKey in Portfolio itemsController#update
You did not provide both required access keys. Please provide the access_key_id and the secret_access_key.
The trace is irrelevant except for my controller line #, which works fine until I try and upload a file. Here's what I have:
class Asset < ActiveRecord::Base
attr_accessible :image, :image_file_name, :image_content_type, :image_file_size, :portfolio_item_id, :order
has_attached_file :image,
:styles => {
:thumb => "100x100#",
:small => "300x300",
:large => "600x600>"
},
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV["S3_KEY"],
:secret_access_key => ENV["S3_SECRET"]
},
:bucket => "bucketybucket",
:path => "portfolio"
end
Anyone know what's going on here? How am I constructing this hash wrong?
Oh, and I've followed this thread, no dice: Paperclip and Amazon S3 Issue
same problem...
seems like that ENV const doesn't load before loading the module. solve by using file argument
like this
:s3_credentials => Rails.root.join('config/amazon_s3.yml')
and in amazon_s3.yml
access_key_id: 'your_key'
secret_access_key: 'your_sec_key'
bucket: 'somebucket'
furthermore, you can set environment variable by using heroku config:add command, which is describe in Heroku DevCenter
The problem is because the Enviroment variable in heroku is different that the enviroment variable in your system, so it may happen that the application works just in one enviroment

Resources