Question

I'm receiving the following error:

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

I'm not sure why I would be getting this error because I have the correct access key and secret key shown to me in my aws security credentials page (I have copied and pasted multiple times to refute the possibility of a typo). Why would it be telling me that my access key is not in their records if I am able to log in and visit my STILL empty buckets?

Here is my 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>

Here is my 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

Here is my 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

Here is my datum_params method:

# 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

Here is my picture_params method:

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

Here is the form I use for the upload:

<%= 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 %>

Originally, I was having a strong_parameters issue that rails4guides.com (the user with the answer below) was able to help me overcome. I knew I made progress when I stopped getting an unpermitted parameter error and started getting errors from AWS. Can anyone see any reason why my credentials are not working? Am I supposed to be seeing anything about my access key in the request parameters? Because I don't at this time.

Was it helpful?

Solution

Okay, I will explain further in a separate answer to make further elaboration more clear.

In a typical situation you would have something like this in your form :

<%= f.file_field :picture %>

Picture is the name of the file you want to upload. Now imagine this form will pass on it's results to the ImagesController. In order for the ImagesController to be able to process the picture it should be whitelisted through strong parameters, as in:

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

An other case would be using nested attributes. If you want to set a picture for an article your form will look something like this:

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

And your article_params will look like this:

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

Small note: It should be images_attributes (both plural) and not image_attributes. The actual nested attributes on the other hand should be singular.

This is pretty much a basic outline of strong parameter usage, I hope it will help you. P.s. There might be some typos, since I'm typing this on my mobile phone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top