在rails 3.1中使用paperclip和aws-s3的AWS :: Errors :: MissingCredentialsError

时间:2012-08-31 23:56:42

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

我正在尝试在aws上传图片。

class Asset < ActiveRecord::Base
  belongs_to :post
  attr_accessible :image
  has_attached_file :image, :styles => { :medium => "640x480>", 
                                     :thumb => "100x100#"},
  :storage => :s3,
   :s3_credentials => "#{Rails.root}/config/s3.yml",
  :path => ":attachment/:id/:style.:extension",
  :bucket => 'yourbucket'
end

s3.yml

development:
  access_key_id: xxxxxxxx

   secret_code: xxxxx

我收到了一条消息

 AWS::Errors::MissingCredentialsError in PostsController#create

Missing Credentials.

 Unable to find AWS credentials.  You can configure your AWS credentials
 a few different ways:

 * Call AWS.config with :access_key_id and :secret_access_key

 * Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV

* On EC2 you can run instances with an IAM instance profile and credentials 
 will be auto loaded from the instance metadata service on those
  instances.

* Call AWS.config with :credential_provider.  A credential provider should
either include AWS::Core::CredentialProviders::Provider or respond to
the same public methods.

= Ruby on Rails

在Ruby on Rails应用程序中,您还可以指定您的凭据    以下方式:

  • 使用上述任何方法通过配置初始化程序脚本 (例如RAILS_ROOT / config / initializers / aws-sdk.rb)。

  • 通过位于RAILS_ROOT / config / aws.yml的yaml配置文件。 该文件应该像默认的RAILS_ROOT / config / database.yml一样格式化 文件

我相信我正在做最后一步。

Gemfile

gem 'rails', '3.1.3'
gem 'mysql'
gem 'koala'
gem 'paperclip'
gem 'aws-s3'
gem 'aws-sdk'

1 个答案:

答案 0 :(得分:7)

以下对我有用:

  1. 在初始化程序中创建一个名为aws.rb
  2. 的文件
  3. 在aws.rb文件中输入以下内容:

    AWS.config(
    access_key_id: 'your_access_key',
    secret_access_key: 'your_secret_access_key')
    
  4. 然后我的回形针选项如下所示:

    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" },       
    :default_url => "missing_:style.png", :default_url => 'missing_:style.png', :storage =>   
    :s3, :bucket => "<my_bucket>"