Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda