【问题标题】:AWS credentials aren't working with Paperclip image upload in Rails 4 and mongoidAWS 凭证不适用于 Rails 4 和 mongoid 中的 Paperclip 图像上传
【发布时间】:2014-05-16 06:26:48
【问题描述】:

我收到以下错误:

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>

这是我的图片.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 收到错误时,我知道我取得了进展。谁能看到我的凭据不起作用的任何原因?我应该在请求参数中看到有关我的访问密钥的任何信息吗?因为我现在没有。

【问题讨论】:

  • 在你的 datum_params 中应该是图片属性(复数)。在您的图片参数中,您应该将附件的名称列入白名单。很奇怪,它似乎期望图片虽然在我看来它被命名为附件。我仍然会尝试 params.require(:picture).permit(:picture)。最后一个 :picture 指的是回形针附件的名称。
  • 感谢您的建议!我今天下班后可以尝试这些,但我可以说的一件事是,如果我尝试params.require(:picture).permit(:picture),它会抱怨Picture 类没有名为picture 的方法。如果我尝试使用jquery-fileupload-rails gem 让我能够一次上传多个文件,我是否需要对picturepictures 做更多的事情?
  • 嗯,使用回形针,您通常允许您的附件名称。在您的情况下,您会期望:附件。关于嵌套属性,在您允许的参数中对它的引用应该始终是复数。附:在使用 'pictures_attribute: [:picture]' 之前,您是否也遇到了完全相同的错误?
  • 不,我没有。它创造了画面,但从来没有给我一个为什么事情没有被推到 s3 的原因。我唯一一次遇到无方法错误是在我遇到params.require(:picture).permit(:picture) 的时候。我将其更改为params.require(:picture).permit(:attachment) 并将datum_params 方法主体更改为params.require(:datum).permit(..., pictures_attributes: [:pictures])。这符合你的建议吗?我可以在美国山区时间下午 6 点左右为您提供这些更改的结果。到目前为止,我非常感谢您的帮助。
  • 如果这没有为我指明解决这个问题的方向,还有哪些其他信息对我的问题有用?我还要添加用于 file_field 的表单...

标签: ruby-on-rails-4 amazon-s3 mongoid paperclip strong-parameters


【解决方案1】:

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

在典型情况下,您的表单中会出现这样的内容:

<%= 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。另一方面,实际的嵌套属性应该是单数的。

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

【讨论】:

  • 太棒了!谢谢!所以嵌套属性fields_for 块只有在我的form_for @article ... 中有这个块时才需要,对吗?如果我有上传图片的表单以form_for @artcile ... 之外的模式显示,那么我不需要使用fields_for 对吗?
  • 你的建议让我更接近了!我现在收到 AWS 错误!我认为这是一个改进!据说我提供的访问密钥不在他们的记录中……我已经转到 aws 的凭据页面,并从该站点复制并粘贴了我的访问密钥、密钥和存储桶名称。我不确定我还能做些什么来解决这个问题。有什么想法吗?
  • 第一条评论:没错,在您的情况下,您不需要嵌套属性。第二条评论:这似乎不再与强参数有关,我会检查 github 问题以及您收到的特定 aws 错误。
  • 到目前为止没有运气,这就是我一直在寻找的地方。但我不会放弃!我已经更新了我的问题以反映最近的事态发展。
  • 很高兴听到。如果你以后遇到强参数的问题,我刚刚写了一篇关于它的文章:rails4guides.com/admin/articles/strong-parameters-in-action
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
  • 2014-01-18
  • 2016-07-21
  • 2015-03-10
  • 2016-07-30
  • 1970-01-01
  • 2014-10-20
相关资源
最近更新 更多