railsからs3に画像をアップロードしようとしているのですが、"The AWS Access Key Id you provided does not exist in our records."のエラーが発生してしまい、ファイルをアップロードできない状態になっています。aws_access_keyは過去の物が残ってしまい、最新のものが認識されてない状態です。もしこのエラーがわかる方がいらしたら、教えていただきたいです。
新しいaws access id(仮定) AKIATYDUVIEUI4A2VYXX
過去のaws access id(仮定) AKIATYDUVIEUI4A2VYYY
エラーメッセージ
Excon::Error::Forbidden (Expected(200) <=> Actual(403 Forbidden)
excon.error.response
:body => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>InvalidAccessKeyId</Code><Message>The AWS Access Key Id you provided does not exist in our records.</Message><AWSAccessKeyId>AKIATYDUVIEUI4A2VYYY</AWSAccessKeyId><RequestId>xxxxxxxxxxxxx</RequestId><HostId>XXXXXXXXXXXXXXXXXXXX</HostId></Error>"
:cookies => [
]
:headers => {
"Connection" => "close"
"Content-Type" => "application/xml"
"Date" => "Tue, 17 Mar 2020 22:47:43 GMT"
"Server" => "AmazonS3"
"x-amz-id-2" => "####################"
"x-amz-request-id" => "##############"
}
:host => "s3.amazonaws.com"
:local_address => "10.0.0.42"
:local_port => 42372
:path => "/uploads/user/student_license/21/S__14327812.jpg"
:port => 443
:reason_phrase => "Forbidden"
:remote_ip => "52.217.12.22"
:status => 403
:status_line => "HTTP/1.1 403 Forbidden\r\n"
):
carrierwave
1require 'carrierwave/storage/abstract' 2require 'carrierwave/storage/file' 3require 'carrierwave/storage/fog' 4 5if Rails.env.production? 6 CarrierWave.configure do |config| 7 config.storage = :fog 8 config.fog_provider = 'fog/aws' #←ここを追記 9 config.fog_directory = '########' 10 config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/######' 11 config.fog_public = false 12 config.fog_credentials = { 13 :provider => 'AWS', 14 :region => 'ap-northeast-1', 15 :host => '######.s3-ap-northeast-1.amazonaws.com', 16 :aws_access_key_id => ENV["AWS_ACCESS_KEY_ID"], 17 :aws_secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"] 18 } 19 20 end 21end 22CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:].\-+]/
バケットポリシー
{
"Version": "2008-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "",
"Action": "s3:",
"Resource": [
"arn:aws:s3:::#######",
"arn:aws:s3:::#######/*"
],
"Condition": {
"StringEquals": {
"aws:SourceIp": "###########"
}
}
}
]
}
env
1AWS_SECRET_ACCESS_KEY = AKIATYDUVIEUI4A2VYXX 2AWS_ACCESS_KEY_ID = ##############
ローカル環境 vim ~/.bash_profile
local
1export AWS_ACCESS_KEY_ID="AKIATYDUVIEUI4A2VYXX" 2export AWS_SECRET_ACCESS_KEY="#################"
本番環境 vim ~/.bash_profile
export AWS_ACCESS_KEY_ID="AKIATYDUVIEUI4A2VYXX" export AWS_SECRET_ACCESS_KEY="#########################"
実行トライ&エラー
$ aws configure
AWS Access Key ID AKIATYDUVIEUI4A2VYXX:
AWS Secret Access Key ###############:
Default region name [None]:
Default output format [None]:
$ aws configure list
Name Value Type Location
---- ----- ---- --------
profile <not set> None None
access_key AKIATYDUVIEUI4A2VYXX env
secret_key #################### env
region <not set> None None
参考にしたサイト
https://qiita.com/Rubyist_SOTA/items/f1c36cab1eceedf00008
あなたの回答
tips
プレビュー