0

我有一个名为 Report 的类,报告是通过 Rake 任务生成的。我还对图像(用户头像)使用回形针,它可以很好地上传到我的 S3 存储桶。这是报告模型的顶部:

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"

我的 Paperclip.rb 文件是这样的:

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)

它将报告保存到:

/system/reports/csv_files/000/000/002/original/general-report-2013-9-25-T-3-56-PM.csv?1380142571

而不是像我的头像那样的 S3:

//s3.amazonaws.com/production/media/avatar-placeholder.gif

有人知道为什么吗?

4

2 回答 2

0

通过将选项合并到 PAPERCLIP_OPTIONS 中来修复它,如下所示:

  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: []
  )
于 2013-09-27T16:32:00.060 回答
0

我的猜测是您的环境没有RAILS_ENV(或RACK_ENV)设置,因此您的rake任务正在development环境中运行,由于这一行,它不会保存到 s3:

PAPERCLIP_STORAGE_OPTIONS = if Rails.env.staging? || Rails.env.production?

您可能希望rake像这样运行您的任务:

RAILS_ENV=production rake do_some_thing
于 2013-09-26T04:08:32.350 回答