The Whiz of Oz
The Whiz of Oz

Reputation: 7043

AWS and Rails: The authorization header is malformed; the Credential is mal-formed

I am trying to access my S3 bucket from a production server. Everything works fine in development, however in my prod console I am getting:

Aws::S3::Errors::AuthorizationHeaderMalformed: The authorization header is malformed; the Credential is mal-formed; expecting "<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request".

My code:

class AwsFileDownloader
  def initialize(args)
    @s3 = Aws::S3::Client.new
    @aws_file_path = ...
  end

  def get_file
    temp_file = File.new('temp.csv', 'r+')
    @s3.get_object({bucket: Rails.application.secrets.aws_bucket, key: @aws_file_path}, target: temp_file)
    ...
  end
end

My aws initializer (which seems to work fine in both environments):

require 'aws-sdk'

Aws.config.update({
  region: Rails.application.secrets.aws_region,
  credentials: Aws::Credentials.new(Rails.application.secrets.access_key_id, Rails.application.secrets.secret_access_key)
})

appreciate any advice!

Upvotes: 12

Views: 20852

Answers (6)

Ace
Ace

Reputation: 1146

In my case the issue was that I was writing tests using the RestAssured library and made a GET request to a signed URL obtained from an API call. The URL had the slashes from "<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request" url encoded, then RestAssured encoded the percent signs from the already url encoded URL. So I ended up with something like %252Finstead of %2F in place of the slashes. My fix was to decode the signed url before passing it to RestAssured.

given(requestSpec)
  .when()
  .get(URLDecoder.decode(s3SignedUrl, StandardCharsets.UTF_8))

Upvotes: 5

KGP Vertiv
KGP Vertiv

Reputation: 29

for Windows Users I got the same issue over and over again, even after copying the correct credentials. Turns out Windows Bash was not accepting CTRL+C and CTRL+V for copy and paste. only right clicking and copy paste option worked for me.

Upvotes: 0

shivani premi
shivani premi

Reputation: 59

Also, got the same error but the problem was I interchanged the values of access key and secret key by mistake.

Upvotes: -1

Alexa
Alexa

Reputation: 938

This answer is very late but maybe someone will find it useful. The above error is caused when you switch up the AWS KEY ID and the AWS ACCESS KEY.

Use the correct credential in the right place and it should fix your issue.

Upvotes: 68

gwcodes
gwcodes

Reputation: 5690

Things to check:

  • bucket ACLs and bucket policies. Does production match dev?
  • do any of your keys contain slashes, that could be incorrectly parsed?
  • are your dev and production regions different? Does it make a difference if you try a different region?

Upvotes: 5

Sajin
Sajin

Reputation: 1638

As the error suggests the credentials is not being set correctly. One possible reason (since in works in development environment) is that the config variables access_key_id and secret_access_key might be environment specific.

Upvotes: 0

Related Questions