Paperclip不会生成S3 URL

时间:2014-08-13 14:57:44

标签: ruby-on-rails amazon-s3 paperclip

我正在将用户上传的文件迁移到S3存储桶。它们已经成功迁移thanks to this question,或者我认为,因为它们都列在我的桶的仪表板中。然后,我按照thisthisthis指南设置了S3集成。我的Gemfile看起来像这样:

(...)
#Image upload
gem 'paperclip'
gem 'aws-s3' # gem 'aws-sdk' won't work either
(...)

config/initializers/paperclip.rb

Paperclip::Attachment.default_options[:path] = "/:class/:attachment/:id_partition/:style/:filename"
Paperclip::Attachment.default_options[:s3_host_name] = "sa-east-1.amazonaws.com"
Paperclip::Attachment.default_options[:url] = ":s3_path_url"

config/environments/development.rbconfig/environments/production.rb都以此配置结束:

config.paperclip_defaults = {
    :storage => :s3,
    :s3_credentials => {
      :bucket => "MY_BUCKET",
      :access_key_id => "MY_ACCESS_ID",
      :secret_access_key => "MY_SECRET"
    }
  }

但是当我尝试获取附件网址时,例如User.find(163).avatar.url,它会产生:

":s3_path_url?1405653975"

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用User.find(163).avatar.url,如果稍后指定样式,可以将其传递给url方法,如下所示:User.find(163).avatar.url(:style)

编辑:

我建议您删除文件initializers / paperclip.rb并包含配置路径.paperclip_defaults如下:

config.paperclip_defaults = {
  :storage => :s3,
  :path => ":class/:attachment/:id/:style/:filename.:extension",
  :s3_credentials => {
    :bucket => "MY_BUCKET",
    :access_key_id => "MY_ACCESS_ID",
    :secret_access_key => "MY_SECRET"
  }
}