Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda