Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Paperclip: Choose between saving files local or S3 at runtime

I'm using Paperclip for saving files. I have configured successfully for Paperclip saving files directly to Amazon S3. But in some situations, I need files only be saved locally. My question is: How can I do this.

Here is my example Paperclip configuration:

Paperclip.interpolates(:upload_url) { |attachment, style| "#{ENV.fetch('UPLOAD_PROTOCOL', 'http')}://#{ENV.fetch('UPLOAD_DOMAIN', 'localhost:3000')}/uploads/:class/:attachment/:id_:style.:extension" }

Paperclip::Attachment.default_options.merge!(
    storage: :s3,
    s3_region: ENV['CEPH_REGION'],
    s3_protocol: 'http',
    s3_host_name: ENV['CEPH_HOST_NAME'],
    s3_credentials: {
        access_key_id: ENV['CEPH_ACCESS_KEY_ID'],
        secret_access_key: ENV['CEPH_SECRET_KEY'],
    },

    s3_options: {
        endpoint: ENV['CEPH_END_POINT'],
        force_path_style: true
    },

    s3_permissions: 'public-read',

    bucket: ENV['CEPH_BUCKET'],
    url: ':s3_path_url',
    path: ':class/:id/:basename.:extension',

    use_timestamp: false
)

module Paperclip
  def self.string_to_io(options)
    data = StringIO.new(options[:data])
    data.class.class_eval{ attr_accessor :original_filename }
    data.original_filename = options[:original_file_name]
    data
  end
end
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use a lambda. As shown here. https://github.com/thoughtbot/paperclip#dynamic-configuration

it would look something like this.

class YOURMODEL < ActiveRecord::Base
  has_attached_file :FILE, storage: lambda { |attachment| (attachment.instance.use_s3? ? :s3 : :filesystem) }
end

Then you would have to add a method use_s3? to your model to check if you wanted to store the file locally or with s3.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!