shuhei-T / reptiles_log

爬虫類飼育者のためのかんたん飼育記録サービス「レプログ!」
https://reptileslog.com/
8 stars 1 forks source link

Heroku環境でGoogleSearchConsoleのsitemap設定をAWS S3と上手く紐付けできない #103

Closed shuhei-T closed 2 years ago

shuhei-T commented 2 years ago

質問内容

下記の記事を参考にしてSearch Consoleの設定を完了したく、Heroku環境でsitemap設定をしようと試みていますが、エラーが出ます。 Herokuはsitemapを保存できないため、AWS S3のストレージに保存する必要があるみたいです。 もともと他の機能のため、gem 'fog-aws'を使用しています。 https://yuruli.info/rails5-how-to-put-sitemap-on-heroku/

現状発生している問題

gem sitemap_generatorを導入し、config/sitemap.rbの中に設定を追記しました。 ターミナルでrails sitemap:refreshを入力すると、下記のようなエラーが出ます。

> rails sitemap:refresh                                                                                                    +main
In '/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/tmp/':
rails aborted!
Excon::Error::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message><Region>ap-northeast-1</Region><RequestId>FGYR6BVTR8NBEFPF</RequestId><HostId>2xpKSeVV9Plz9vfyxIoLGVkV+6D1n8Mt75tlkB0rv1R7DpKj5/YtTbpnJv15w1Og68HVsC9t6v0=</HostId></Error>"
  :cookies       => [
  ]
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Sat, 05 Mar 2022 14:00:57 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "2xpKSeVV9Plz9vfyxIoLGVkV+6D1n8Mt75tlkB0rv1R7DpKj5/YtTbpnJv15w1Og68HVsC9t6v0="
    "x-amz-request-id" => "FGYR6BVTR8NBEFPF"
  }
  :host          => "reptiles-log.s3.amazonaws.com"
  :local_address => "192.168.11.5"
  :local_port    => 61183
  :path          => "/sitemaps/sitemap.xml.gz"
  :port          => 443
  :reason_phrase => "Bad Request"
  :remote_ip     => "52.219.172.89"
  :status        => 400
  :status_line   => "HTTP/1.1 400 Bad Request\r\n"
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/config/sitemap.rb:16:in `run'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/rails:5:in `<top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:10:in `block in <top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:7:in `<top (required)>'

Caused by:
Excon::Error::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message><Region>ap-northeast-1</Region><RequestId>FGYWDDNDEVXBYRTA</RequestId><HostId>k7JdbIPxmGMPAbrZtJZ47W3KcxVwxUaWGAhkFS17qPGR7G3VCR1w34Y/Y07TKzfcIDhB8C0LKTo=</HostId></Error>"
  :cookies       => [
  ]
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Sat, 05 Mar 2022 14:00:57 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "k7JdbIPxmGMPAbrZtJZ47W3KcxVwxUaWGAhkFS17qPGR7G3VCR1w34Y/Y07TKzfcIDhB8C0LKTo="
    "x-amz-request-id" => "FGYWDDNDEVXBYRTA"
  }
  :host          => "reptiles-log.s3.amazonaws.com"
  :local_address => "192.168.11.5"
  :local_port    => 61182
  :path          => "/sitemaps/sitemap.xml.gz"
  :port          => 443
  :reason_phrase => "Bad Request"
  :remote_ip     => "52.219.172.89"
  :status        => 400
  :status_line   => "HTTP/1.1 400 Bad Request\r\n"
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/config/sitemap.rb:16:in `run'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/rails:5:in `<top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:10:in `block in <top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:7:in `<top (required)>'

--------------以下省略----------------------------

どの処理までうまくいっているか

SitemapGenerator::Sitemap.adapter =の記述をコメントアウトにするとエラーは出ません。

> rails sitemap:refresh                                                                                                    +main
In '/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/tmp/':
+ sitemaps/sitemap.xml.gz                                  1 links /  329 Bytes
Sitemap stats: 1 links / 1 sitemaps / 0m00s

Pinging with URL 'https://s3-ap-northeast-1.amazonaws.com/sitemaps/sitemap.xml.gz':
  Successful ping of Google
Ping failed for Bing: #<OpenURI::HTTPError: 400 Bad Request> (URL http://www.bing.com/webmaster/ping.aspx?sitemap=https%3A%2F%2Fs3-ap-northeast-1.amazonaws.com%2Fsitemaps%2Fsitemap.xml.gz)

該当のソースコード

config/sitemap.rb

# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "https://www.reptileslog.com/"
# inform the map cross-linking where to find the other maps
SitemapGenerator::Sitemap.sitemaps_host = "https://s3-#{ENV['AWS_DEFAULT_REGION']}.amazonaws.com/#{ENV['AWS_S3_BADGET_NAME']}"
# pick a place safe to write the files
SitemapGenerator::Sitemap.public_path = 'tmp/'
# pick a namespace within your bucket to organize your maps
SitemapGenerator::Sitemap.sitemaps_path = 'sitemaps/'
# store on S3 using Fog (pass in configuration values as shown above if needed)
SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS',
                                      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
                                      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
                                      fog_directory: ENV['AWS_S3_BADGET_NAME'],
                                      aws_region: ENV['AWS_DEFAULT_REGION'])

エラーから考えられる原因

S3との紐付けが上手く行っていないのだと思います。

試したこと

参考記事

https://dosukoikoi.com/articles/331 https://github.com/kjvarga/sitemap_generator/wiki/Generate-Sitemaps-on-read-only-filesystems-like-Heroku https://qiita.com/you8/items/71a07ada6fd8661b5ddb https://yuruli.info/rails5-how-to-put-sitemap-on-heroku/

takyyk commented 2 years ago

sitemap generatorのgithubではsitemapのhostを指定する際、 sitemaps_host would be http://s3-us-west-2.amazonaws.com/your-bucket/ と末尾に"/"があり、 参考にされていた記事でも

SitemapGenerator::Sitemap.sitemaps_host = “https://s3-ap-northeast-1.amazonaws.com/#{ENV[‘AWS_S3_BUCKET’]}/”の最後の’/’は必須

とあるのでSitemapGenerator::Sitemap.sitemaps_hostの末尾に / を加えるとどうなりますかね?

shuhei-T commented 2 years ago

@takyyk コメントありがとうございます! 末尾に/を加えました。

SitemapGenerator::Sitemap.sitemaps_host = "https://s3-#{ENV['AWS_DEFAULT_REGION']}.amazonaws.com/#{ENV['AWS_S3_BADGET_NAME']}/"

ローカルで試してみたところ、エラー結果は変わりませんでした。

> rails sitemap:refresh                                                                                                 +feature
In '/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/tmp/':
rails aborted!
Excon::Error::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message><Region>ap-northeast-1</Region><RequestId>DHHV2G8F381X16BW</RequestId><HostId>iYrZlCSmBWP4NzehIyZnatCu7+AQHeuIIqaLpm0u6z3p+RX4a+8/pXnHPHEvQhVorYQbjagRdmo=</HostId></Error>"
  :cookies       => [
  ]
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Sun, 06 Mar 2022 01:32:48 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "iYrZlCSmBWP4NzehIyZnatCu7+AQHeuIIqaLpm0u6z3p+RX4a+8/pXnHPHEvQhVorYQbjagRdmo="
    "x-amz-request-id" => "DHHV2G8F381X16BW"
  }
  :host          => "reptiles-log.s3.amazonaws.com"
  :local_address => "192.168.11.5"
  :local_port    => 57193
  :path          => "/sitemaps/sitemap.xml.gz"
  :port          => 443
  :reason_phrase => "Bad Request"
  :remote_ip     => "52.219.152.41"
  :status        => 400
  :status_line   => "HTTP/1.1 400 Bad Request\r\n"
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/config/sitemap.rb:17:in `run'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/rails:5:in `<top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:10:in `block in <top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:7:in `<top (required)>'

Caused by:
Excon::Error::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message><Region>ap-northeast-1</Region><RequestId>002JCB1SD3F9P02E</RequestId><HostId>1bW5G4z349c1u8odi4yBeepmdjzhNS5Ev9LBIZdeyHfVg80f6TnsWZaKozKSZmHM7Trjxtp/7w0=</HostId></Error>"
  :cookies       => [
  ]
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Sun, 06 Mar 2022 01:32:48 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "1bW5G4z349c1u8odi4yBeepmdjzhNS5Ev9LBIZdeyHfVg80f6TnsWZaKozKSZmHM7Trjxtp/7w0="
    "x-amz-request-id" => "002JCB1SD3F9P02E"
  }
  :host          => "reptiles-log.s3.amazonaws.com"
  :local_address => "192.168.11.5"
  :local_port    => 57188
  :path          => "/sitemaps/sitemap.xml.gz"
  :port          => 443
  :reason_phrase => "Bad Request"
  :remote_ip     => "52.219.152.41"
  :status        => 400
  :status_line   => "HTTP/1.1 400 Bad Request\r\n"
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/config/sitemap.rb:17:in `run'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/rails:5:in `<top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:10:in `block in <top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:7:in `<top (required)>'

Caused by:
Excon::Error::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message><Region>ap-northeast-1</Region><RequestId>002HJRRGZ6V2SKAC</RequestId><HostId>XD0UZmntWctVOC9iIAfPdYMEwwoPbHqcanlS6CNgTBz+nK0CIMEYFK7fbklSM7yoebL+UxxhwdQ=</HostId></Error>"
  :cookies       => [
  ]
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Sun, 06 Mar 2022 01:32:48 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "XD0UZmntWctVOC9iIAfPdYMEwwoPbHqcanlS6CNgTBz+nK0CIMEYFK7fbklSM7yoebL+UxxhwdQ="
    "x-amz-request-id" => "002HJRRGZ6V2SKAC"
  }
  :host          => "reptiles-log.s3.amazonaws.com"
  :local_address => "192.168.11.5"
  :local_port    => 57187
  :path          => "/sitemaps/sitemap.xml.gz"
  :port          => 443
  :reason_phrase => "Bad Request"
  :remote_ip     => "52.219.152.41"
  :status        => 400
  :status_line   => "HTTP/1.1 400 Bad Request\r\n"
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/config/sitemap.rb:17:in `run'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/rails:5:in `<top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:10:in `block in <top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:7:in `<top (required)>'

Caused by:
Excon::Error::BadRequest: Expected(200) <=> Actual(400 Bad Request)
excon.error.response
  :body          => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AuthorizationHeaderMalformed</Code><Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message><Region>ap-northeast-1</Region><RequestId>002WS7NC0HNCM0QT</RequestId><HostId>/0UPJO3c7MO5MpSruSdY7xiHFSGPJHfuOBVt8/CVCBnK7kwY6VrOY1M+pyv8/g3uMYWjBwqzHmI=</HostId></Error>"
  :cookies       => [
  ]
  :headers       => {
    "Connection"       => "close"
    "Content-Type"     => "application/xml"
    "Date"             => "Sun, 06 Mar 2022 01:32:47 GMT"
    "Server"           => "AmazonS3"
    "x-amz-id-2"       => "/0UPJO3c7MO5MpSruSdY7xiHFSGPJHfuOBVt8/CVCBnK7kwY6VrOY1M+pyv8/g3uMYWjBwqzHmI="
    "x-amz-request-id" => "002WS7NC0HNCM0QT"
  }
  :host          => "reptiles-log.s3.amazonaws.com"
  :local_address => "192.168.11.5"
  :local_port    => 57186
  :path          => "/sitemaps/sitemap.xml.gz"
  :port          => 443
  :reason_phrase => "Bad Request"
  :remote_ip     => "52.219.152.41"
  :status        => 400
  :status_line   => "HTTP/1.1 400 Bad Request\r\n"
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/config/sitemap.rb:17:in `run'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/rails:5:in `<top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:10:in `block in <top (required)>'
/Users/takahashishuuhei/workspace/runteq/portfolio/reptiles_log/bin/spring:7:in `<top (required)>'
Tasks: TOP => sitemap:refresh => sitemap:create
(See full trace by running task with --trace)
takyyk commented 2 years ago

確認ありがとうございます!

それではError::BadRequest: Expected(200) <=> Actual(400 Bad Request) で200になることを期待していたが400 Bad Requestが返ってきてしまっているということなので、requestを見直したいですね。

エラーメッセージで

the region 'us-east-1' is wrong; expecting 'ap-northeast-1'

と出ているので一旦regionの設定回りを見直してみてください!

shuhei-T commented 2 years ago

@takyyk ありがとうございます。 AWS側のバケットの設定はこのようになっています。 Image from Gyazo

また、config/initializers/carrierwave.rbでも同様の指定をしていますので、リージョンは問題ないように思います。

require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'

CarrierWave.configure do |config|
  if Rails.env.production?
    config.storage :fog
    config.fog_provider = 'fog/aws'
    config.fog_directory  = ENV['AWS_S3_BADGET_NAME']
    config.asset_host = 'https://s3.amazonaws.com/reptiles-log'
    # NOTE: AWS側の設定を変えなくても、この1行の設定でアップロードできた
    config.fog_public = false # ←コレ
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      region: ENV['AWS_DEFAULT_REGION'],
      # path_style: true
    }
  else
    config.storage :file
    config.enable_processing = false if Rails.env.test?
  end
end
takyyk commented 2 years ago

<Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'ap-northeast-1'</Message>

と出ているのでap-northeast-1のS3に対してus-east-1でアクセスしようとしているのが原因な感じがしますね。

一度AWS_DEFAULT_REGIONがどうなっているか教えてもらってもいいですか?

またSitemapGenerator::Sitemap.sitemaps_hostのところに、https://s3-#{ENV['AWS_DEFAULT_REGION']}.amazonaws.comではなく、"https://s3-ap-northeast-1.amazonaws.comのように直で書いて試してみてください。

shuhei-T commented 2 years ago

@takyyk ありがとうございます。 リージョンはこのように設定されています。 AWS_DEFAULT_REGION='ap-northeast-1' 下記のように書きましたが、エラーは変わりませんでした。 SitemapGenerator::Sitemap.sitemaps_host = "https://s3-ap-northeast-1.amazonaws.com/reptiles-log/"

takyyk commented 2 years ago

確認ありがとうございます!

SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS',
                                      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
                                      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
                                      fog_directory: ENV['AWS_S3_BADGET_NAME'],
                                      aws_region: ENV['AWS_DEFAULT_REGION'])

aws_region: ENV['AWS_DEFAULT_REGION'])この部分をfog_region: 'ap-northeast-1'にしてもう一度確認していただいてもよろしいでしょうか?

shuhei-T commented 2 years ago

@takyyk ありがとうございます。 fog_region: ENV['AWS_DEFAULT_REGION']) にしてみましたが、エラーは変わりませんでした。

shuhei-T commented 2 years ago

@takyyk 別のトラブルが出てきたため、 こちらの問題は解決に時間がかかりそうなことと、優先度が低いので一旦後回しにしようと思います。

ありがとうございます!