2013-11-20 104 views
3

我一直在railstutorial.org上关注rails 4教程。我完成了大部分工作,该项目托管在heroku上,但现在想要将镜像上传到Amazon S3。我已经按照heroku网站上的指南进行了操作,但在S3(欧洲)上无法上传任何内容到我的存储桶中。rails 4回形针上传到亚马逊s3不起作用

我正在使用回形针3.5.2。

Post模型

has_attached_file :post_photo, 
        styles: { 
         thumb: '100x100>', 
         square: '200x200#', 
         medium: '300x300>' 
        }, 
        :storage => :s3, 
        :s3_credentials => { 
         :access_key_id => ENV['S3_KEY'], 
         :secret_access_key => ENV['S3_SECRET'] }, 
        :s3_protocol => "https", 
        :path => ":class/:id/:basename_:style.:extension", 
        :url => ':s3_eu_url', 
        :bucket => 'bucket_name' 

柱控制器

def post_params 
    params.require(:post).permit(:post_photo, :user_username, :title, :comment, :location, :user_id) 
end 

配置/初始化/ Paperclip.rb

Paperclip.interpolates(:s3_eu_url) { |attachment, style| 
    "#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}" 
} 

到config/environment.rb

require 'aws/s3' 
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com" 

配置/环境/ production.rb

# config/environments/production.rb 
    config.paperclip_defaults = { 
     :storage => :s3, 
     :s3_credentials => { 
      :bucket => ENV['S3_BUCKET_NAME'], 
      :access_key_id => ENV['AWS_ACCESS_KEY_ID'], 
      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] 
     }, 
     :url => ':s3_eu_url', 
     :path => ":class/:id/:basename_:style.:extension" 
    } 
+0

你使用的是'aws-sdk'吗? –

回答

3

所以,我已经得到了这个工作(不与欧洲S3,但是这不应该问题)config/environments/production.rb中没有任何东西 - 主要是因为我使用ENV变量来帮助控制指向哪个桶,API密钥等。

这是我的配置:

config/environments/production.rb

只有标准配置 - 无关回形针。

config/initializers/paperclip.rb

Paperclip::Attachment.default_options[:storage] = :s3 
Paperclip::Attachment.default_options[:s3_protocol] = 'http' 
Paperclip::Attachment.default_options[:s3_credentials] = 
    { :bucket => ENV['AWS_BUCKET'], 
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'], 
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] } 

上述情况,你要添加:

Paperclip::Attachment.default_options[:url] = ':s3_domain_url' 
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename' 

然后,你Post模型应该只需要有类似:

has_attached_file :post_photo, 
        styles: { 
         thumb: '100x100>', 
         square: '200x200#', 
         medium: '300x300>' 
        } 

这可能很明显,但也要确保你有aws-sdk Gem加载到您的Gemfile中。

如果您有任何疑问,请告知我。我已经设置了很多次,完全有助于排除故障。 :)

+0

是的,我在我的gemfile中包含了aws-sdk,我做了更改,但仍然没有上传到我的存储桶中。另外,当我运行heroku日志时,我没有看到有关上传到存储桶的任何错误消息,但我可以看到该文件正在被应用程序处理。 – BON

+0

这是生产吗?还是当地的环境?此外,值得将转换日志记录为DEBUG级别,以查看可能发生的事情。 – CDub

+0

对不起,我没有看到这一点。这是当我部署到heroku,所以我想这是生产?我不知道如何为我的本地环境设置亚马逊凭据。我应该用's3-eu-west-1.amazonaws.com'替换':s3_domain_url'吗? – BON