2012-08-31 89 views
3

我想在aws上傳圖片。AWS :: Errors :: MissingCredentialsError在rails 3.1中使用回形針和aws-s3

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 /配置/初始化/ 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' 

回答

7

以下爲我工作:

  1. 創建你的初始化文件名​​爲aws.rb
  2. 在你aws.rb文件下面放:

    AWS.config(
    access_key_id: 'your_access_key', 
    secret_access_key: 'your_secret_access_key') 
    
  3. 然後我的回形針的選擇是這樣的:

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

這肯定適用於獲取紙質和運行的「第一關」,並且是我一直在尋找。請注意,TJ Sherrill的答案中提到的環境變量方法是規範的「正確」答案。 – deepwinter

+0

@Yanik謝謝只有這個解決方案適用於我... –

+0

我想添加新的sdk,你將不得不使用Aws.config.update({}),因爲配置不再是一個方法,而是一個散列。 – Tommyixi

相關問題