AWS凭证不适用于Rails 4和Mongoid中的Paperclip图像上传

杰克·史密斯

我收到以下错误:

The AWS Access Key Id you provided does not exist in our records.

我不确定为什么会收到此错误,因为我在AWS安全凭证页面上显示了正确的访问密钥和秘密密钥(我已复制并粘贴多次,以防出现输入错误的情况)。如果我能够登录并访问STILL空存储桶,为什么会告诉我我的访问密钥不在他们的记录中?

这是我的s3.yml:

GMAIL_USERNAME: <my email>
GMAIL_PASSWORD: <my password>
MONGOHQ_URL: <my mongoid url>
S3_BUCKET_NAME_DEVELOPMENT: <my development bucket>
S3_BUCKET_NAME_PRODUCTION: <my production bucket>
S3_BUCKET_NAME_TEST: <my test bucket>
AWS_ACCESS_KEY_ID: <my access key>
AWS_SECRET_ACCESS_KEY: <my secret key>

这是我的picture.rb:

class Picture
  include Mongoid::Document
  include Mongoid::Paperclip

  embedded_in :datum, inverse_of: :pictures

  has_mongoid_attached_file :attachment,
                            path: ':attachment/:id/:style.:extension',
                            storage: :s3,
                            url: ':s3_domain_url',
                            bucket: Proc.new{ |a| a.instance.s3_bucket },
                            s3_credentials: File.join(Rails.root, 'config', 's3.yml'),
                            styles: {
                                original: ['1920x1680', :jpg],
                                small: ['100x100', :jpg],
                                medium: ['250x250', :jpg],
                                large: ['500x500', :jpg]
                            },
                            convert_options: { all: '-background white -flatten +matte' }

  def s3_bucket
    if Rails.env.development?
      ENV['S3_BUCKET_NAME_DEVELOPMENT']
    else
      ENV['S3_BUCKET_NAME_PRODUCTION']
    end
  end
end

这是我的datum.rb:

class Datum
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paperclip

  field :title, type: String
  field :user_id, type: Integer

  validates :title, presence: true
  validates :user_id, presence: true

  embeds_many :todo_items
  embeds_many :pictures, cascade_callbacks: true
  accepts_nested_attributes_for :pictures
end

这是我的datum_params方法:

# Never trust parameters from the scary internet, only allow the white list through.
def datum_params
  params.require(:datum).permit(:title, :user_id, :identifying_image, pictures_attributes: [:picture])
end

这是我的picture_params方法:

def picture_params
  params.require(:picture).permit(:datum_id, :attachment)
end

这是我用于上传的表格:

<%= bootstrap_form_for(picture, url: { action: :create }, html: { multipart: true }, remote: true ) do |f| %>
  <%= f.file_field :picture, multiple: true, name: 'picture[picture]' %>
  <%= f.submit 'Upload' %>
<% end %>

最初,我遇到了strong_parameters问题,rails4guides.com(具有以下答案的用户)能够帮助我解决。我知道当我停止遇到不允许的参数错误并开始从AWS中获取错误时,我已经取得了进步。谁能看到我的凭据不起作用的任何原因?我应该在请求参数中看到有关访问密钥的任何信息吗?因为我现在不在。

rails4guides.com

好的,我将在一个单独的答案中进一步解释,以使进一步的阐述更加清晰。

在典型情况下,您的表单中会包含以下内容:

<%= f.file_field :picture %>

图片是您要上传的文件的名称。现在,假设此表单会将其结果传递给ImagesController。为了使ImagesController能够处理图片,应该通过强大的参数将其列入白名单,如下所示:

def image_params
  params.require(:image).require(:picture)
end

另一种情况是使用嵌套属性。如果您想为文章设置图片,您的表单将如下所示:

<% f.fields_for :image do |image_form| %>
  <%= image_form.file_field :picture %>
<% end %>

您的article_params将如下所示:

def article_params
  params.require(:article).permit(:title, :content, images_attributes: [:picture])
end

小提示:应该是images_attributes(均为复数),而不是image_attributes。另一方面,实际的嵌套属性应为单数。

这几乎是强参数用法的基本概述,希望对您有所帮助。附言:可能有一些错别字,因为我是在手机上输入的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章