Mia
Mia

Reputation: 113

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

I've been working on this for hours and haven't been able to figure it out. I ran git clean and then realized that my s3.yml file got deleted. I created new keys and put them in a new s3.yml file. I haven't changed anything else from before which was working just fine. What am I missing?

I'm getting: Aws::Errors::MissingCredentialsError (unable to sign request without credentials set):

Here is my code:

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 (with new access keys)

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

Upvotes: 1

Views: 2439

Answers (1)

TheOni
TheOni

Reputation: 820

As you are getting the error on dev environment I think that the error is due to s3_credentials: "#{Rails.root}/config/s3.yml".

Given the s3.yml you have posted I don't think that s3_credentials will be filled with a json structure like the one you have in production environment.

I suggest you to load the file and fill the json like you do in your production environment.

Upvotes: 1

Related Questions