Aws::Errors::MissingCredentialsError 在 PostsController#update 中使用 Paperclip 和 AWS

Aws::Errors::MissingCredentialsError in PostsController#update with Paperclip and AWS

我已经为此工作了几个小时,但一直无法弄清楚。我 运行 git 清理然后意识到我的 s3.yml 文件被删除了。我创建了新密钥并将它们放入新的 s3.yml 文件中。我没有改变之前的任何其他东西,它工作得很好。我错过了什么?

我得到:Aws::Errors::MissingCredentialsError (unable to sign request without credentials set):

这是我的代码:

development.rb

  config.paperclip_defaults = {
    storage: :s3,
    s3_region: 'us-east-1',
    bucket:'mybucket',
    s3_credentials: "#{Rails.root}/config/s3.yml"
  }

production.rb

config.paperclip_defaults = {
      storage: :s3,
      s3_credentials: {
        bucket: ENV.fetch('S3_BUCKET_NAME'),
        access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
        secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
        s3_region: ENV.fetch('AWS_REGION'),
      }
    }
    # Secret key base for non-existent secrets.yml file
    config.secret_key_base = ENV["SECRET_KEY_BASE"]

s3.yml(使用新的访问密钥)

S3_BUCKET_NAME: mybucket
AWS_ACCESS_KEY_ID: ***
AWS_SECRET_ACCESS_KEY: ***
AWS_REGION: us-east-1

Post class

class Post < ApplicationRecord
  belongs_to :user, counter_cache: true
  belongs_to :category

  validates :title, :presence => true
  validates :content, :presence => true

  has_attached_file :thumbnail, styles: {
      medium: '270x170#',
      large: '560x280#',
      large2: '540x340#'
    }

    # Validate the attached image is image/jpg, image/png, etc
    validates_attachment_content_type :thumbnail, :content_type => /\Aimage\/.*\Z/
end

当你在开发环境中收到错误时,我认为错误是由于 s3_credentials: "#{Rails.root}/config/s3.yml"

鉴于您发布的 s3.yml 我不认为 s3_credentials 会像上面那样填充 json 结构你在生产环境中。

我建议您像在生产环境中一样加载文件并填写 json。