RoR,Paperclip和S3上传图片错误

时间:2015-12-08 13:47:56

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

我部署了我的rails应用程序,我使用S3来托管我将使用回形针上传的图像。即使我允许,这里仍然会出现错误:我的控制器中的照片参数

2015-12-08T13:40:00.439949+00:00 app[web.1]: [AWS S3 403 0.042095 0 retries] put_object(:acl=>:public_read,:bucket_name=>"customlized",:content_length=>30237,:content_type=>"image/jpeg",:data=>Paperclip::UploadedFileAdapter: shanab.jpg,:key=>"products/photos/000/000/009/original/shanab.jpg") AWS::S3::Errors::AccessDenied Access Denied
2015-12-08T13:40:00.439953+00:00 app[web.1]: 
2015-12-08T13:40:00.441272+00:00 app[web.1]:    (0.9ms)  ROLLBACK
2015-12-08T13:40:00.442792+00:00 app[web.1]: 
2015-12-08T13:40:00.442793+00:00 app[web.1]: AWS::S3::Errors::AccessDenied (Access Denied):
2015-12-08T13:40:00.442794+00:00 app[web.1]:   app/controllers/products_controller.rb:35:in `block in create'
2015-12-08T13:40:00.442795+00:00 app[web.1]:   app/controllers/products_controller.rb:34:in `create'

我的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']
   }
}

我确实在Heroku上配置了S3给我的凭据

我的产品.rb

has_attached_file :photo ,
        :storage => :s3,
        :s3_credentials => Proc.new{|a| a.instance.s3_credentials },
                    styles: { medium: '300x300>', thumb: '100x100>' }

def s3_credentials
   {:bucket => "Deleted", :access_key_id => "Deleted", :secret_access_key => "Deleted"}
end

1 个答案:

答案 0 :(得分:1)

尝试将您的AWS凭据移出paperclip_defaultsproduct.rb模型,然后添加到aws.yml目录中的config文件中:

production:
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>

(还要确保你的Gemfile中有gem 'aws-sdk', '~> 1.6'

有关使用Paperclip的S3的更多文档:https://github.com/thoughtbot/paperclip/wiki/Paperclip-with-Amazon-S3