AWS EmulatorでS3の静的ウェブサイトホスティングを試す方法
AWS Emulator は、S3 の静的ウェブサイトホスティングの設定を受け付けるだけでなく、ウェブサイトエンドポイントとしての応答も返します。curl で Host ヘッダーを付けて送ると、インデックスのページ、独自のエラーページ、リダイレクトのルールがどう効くかを、AWS のアカウントを使わずに手元で確かめられます。HTML や画像を S3 に置いて公開するサイトや、移転したページの転送ルールを、本物のバケットに置く前に試すのに使えます。
検証環境は Ubuntu 24.04(x86_64)、AWS Emulator 1.0.602(2026年11月24日リリース)、AWS CLI 2.42.2、curl 8.5.0 です。AWS のアカウントと認証情報は使っていません。本物の S3 のウェブサイトエンドポイントでの動き、ブラウザでの表示、CloudFront や独自ドメインとの組み合わせ、料金は確かめていません。本物の S3 について書いているところは、公式ドキュメントを出どころにしています。AWS Emulator の公式ドキュメントによると、AWS がエミュレーターの動きを本物と照らし合わせているのは、米国西部(オレゴン、us-west-2)リージョンだけです(グローバルなサービスは us-east-1)。この記事で使った us-east-1 の S3 は、その対象外です。
エミュレーターを起動して、ページを置く
AWS Emulator は、AWS の API を手元で受け付けるエミュレーターです。公式ドキュメントによると、AWS のアカウントは要らず、リクエストは AWS に送られません。入れ方と、AWS CLI をエミュレーターに向ける環境変数は、別の記事「AWS EmulatorをUbuntuに入れて使い始める方法」と「AWS EmulatorでS3にファイルを置いて取り出す方法」で扱っています。ここでは、公式ドキュメントにある環境変数を設定したシェルで、aws-emulator -t off で起動したエミュレーターを相手にします。-t off は、エミュレーターが既定で AWS に送る利用状況のデータ(テレメトリー)を切る指定です。
繋がっていれば、aws sts get-caller-identity の ARN のユーザー名が emulator になります(12桁のアカウントIDは置き換えています)。
$ aws sts get-caller-identity --query Arn --output text
arn:aws:iam::<12桁のアカウントID>:user/emulator
バケット example-site を作り、トップページの index.html、エラーページの error.html、サブフォルダーのトップページにあたる docs/index.html を置きました。aws s3 cp で置いた index.html の Content-Type は、text/html になっていました。
$ aws s3 mb s3://example-site
make_bucket: example-site
$ aws s3 cp index.html s3://example-site/index.html --no-progress
upload: ./index.html to s3://example-site/index.html
$ aws s3 cp error.html s3://example-site/error.html --no-progress
upload: ./error.html to s3://example-site/error.html
$ aws s3 cp docs-index.html s3://example-site/docs/index.html --no-progress
upload: ./docs-index.html to s3://example-site/docs/index.html
$ aws s3api head-object --bucket example-site --key index.html --query ContentType
"text/html"
ウェブサイトの設定を入れる
作ったばかりのバケットには、ウェブサイトの設定がありません。get-bucket-website は NoSuchWebsiteConfiguration を返しました。
$ aws s3api get-bucket-website --bucket example-site
aws: [ERROR]: An error occurred (NoSuchWebsiteConfiguration) when calling the GetBucketWebsite operation: The specified bucket does not have a website configuration
設定は aws s3 website で入れられます。--index-document がフォルダーにあたる URL で返すファイルの名前(インデックスドキュメント)、--error-document がエラーのときに返すファイル(エラードキュメント)です。
$ aws s3 website s3://example-site --index-document index.html --error-document error.html
$ aws s3api get-bucket-website --bucket example-site
{
"IndexDocument": {
"Suffix": "index.html"
},
"ErrorDocument": {
"Key": "error.html"
}
}
ウェブサイトエンドポイントには Host ヘッダーで送る
本物の S3 では、ウェブサイトとして公開したバケットは、REST API とは別のウェブサイトエンドポイントで見られます。us-east-1 なら http://<バケット名>.s3-website-us-east-1.amazonaws.com の形です。
エミュレーターは、https://127.0.0.1:9297 に届いたリクエストの Host ヘッダーがこの形になっていると、ウェブサイトエンドポイントとして応答しました。毎回同じオプションを書かずに済むように、次の関数 site を作り、シェルに読み込んでから使いました。証明書は、エミュレーターが書き出した ca.pem を指定しています。応答ヘッダーのうち、毎回変わるリクエストIDと日時の行は落としています。
# site <バケット名> <パス> [curl のほかのオプション]:ウェブサイトエンドポイントの Host ヘッダーを付けてエミュレーターに送る
site() { b=$1; p=$2; shift 2; curl -s -i --cacert ~/.aws/emulator/9297/ca.pem -H "Host: $b.s3-website-us-east-1.amazonaws.com" "$@" "https://127.0.0.1:9297$p" | grep -v -i -E '^(x-amz-id-2|x-amz-request-id|date):' | tr -d '\r'; echo; }
本物のウェブサイトエンドポイントは HTTP だけで、公式ドキュメントには HTTPS に対応していないと書かれています。エミュレーターに送るときは、エミュレーターの待ち受けに合わせて HTTPS を使います。URL のホスト名をそのままにして接続先だけをエミュレーターに向ける方法(curl の --connect-to)も試しましたが、エミュレーターの証明書は localhost と 127.0.0.1 向けなので、証明書の検証で失敗しました。
curl: (60) SSL: no alternative certificate subject name matches target host name 'example-site.s3-website-us-east-1.amazonaws.com'
公開の設定をしないと 403 が返る
ウェブサイトの設定を入れただけの状態で送ると、403 AccessDenied が HTML で返りました。
$ site example-site /
HTTP/1.1 403 Forbidden
content-type: text/html
content-length: 162
<html><head><title>403 AccessDenied</title></head><body><h1>403 AccessDenied</h1><ul><li>Code: AccessDenied</li><li>Message: Access Denied</li></ul></body></html>
ウェブサイトのアクセス許可についての公式ドキュメントによると、本物の S3 でサイトを公開するには、バケットのブロックパブリックアクセスを切り、誰でも s3:GetObject できるバケットポリシーを置きます。公式ドキュメントにある例と同じ内容のポリシーを用意しました。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-site/*"
}
]
}
エミュレーターでも、作ったばかりのバケットはブロックパブリックアクセスの4つの設定がすべて true で、このポリシーは BlockPublicPolicy を理由に断られました。
$ aws s3api get-public-access-block --bucket example-site
{
"PublicAccessBlockConfiguration": {
"BlockPublicAcls": true,
"IgnorePublicAcls": true,
"BlockPublicPolicy": true,
"RestrictPublicBuckets": true
}
}
$ aws s3api put-bucket-policy --bucket example-site --policy file://policy.json
aws: [ERROR]: An error occurred (AccessDenied) when calling the PutBucketPolicy operation: User: arn:aws:iam::<12桁のアカウントID>:user/emulator is not authorized to perform: s3:PutBucketPolicy on resource: "arn:aws:s3:::example-site" because public policies are prevented by the BlockPublicPolicy setting in S3 Block Public Access.
ブロックパブリックアクセスの設定を消すと、ポリシーを置けました。
$ aws s3api delete-public-access-block --bucket example-site
$ aws s3api put-bucket-policy --bucket example-site --policy file://policy.json
公式ドキュメントには、本物の S3 でブロックパブリックアクセスを切ると、インターネットの誰でもバケットにアクセスできるようになると書かれています。また、AWS Emulator の公式ドキュメントによると、エミュレーターは IAM のポリシーやバケットポリシーを AWS と同じようには評価しません。エミュレーターでページが返ったからといって、本物のバケットで同じ公開の設定が安全だとは言えません。
トップページとエラーページの返り方
公開の設定をしたあとは、/ でも /index.html でも index.html の中身が返りました。/docs/ のようにスラッシュで終わるパスには、docs/index.html が返りました。
$ site example-site /
HTTP/1.1 200 OK
content-type: text/html
content-length: 15
<h1>Hello</h1>
$ site example-site /index.html
HTTP/1.1 200 OK
content-type: text/html
content-length: 15
<h1>Hello</h1>
$ site example-site /docs/
HTTP/1.1 200 OK
content-type: text/html
content-length: 14
<h1>Docs</h1>
無いファイルを指すと、404 Not Found の状態コードで error.html の中身が返りました。
$ site example-site /nothing.html
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 19
<h1>Not found</h1>
公式ドキュメントに書かれた本物の S3 の動きと違ったのは、末尾にスラッシュのない /docs です。インデックスドキュメントの公式ドキュメントによると、本物の S3 は docs というファイルが無いとき、docs/index.html を探し、あれば 302 Found で docs/ に転送します。エミュレーターでは、転送されずに 404 とエラーページが返りました。
$ site example-site /docs
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 19
<h1>Not found</h1>

フォルダーのリンクを末尾のスラッシュなしで書いているサイトをエミュレーターで試すと、本物では転送されるページが 404 になります。テストで 404 が出たときは、リンクの書き方がこの形でないかを確かめてください。
REST API のエンドポイントとの違い
ウェブサイトエンドポイントは、エラーを HTML で返します。同じバケットに REST API のパスの形(https://127.0.0.1:9297/example-site/)で、署名なしで送ると、エラーは XML で返りました。
$ curl -s -i --cacert ~/.aws/emulator/9297/ca.pem https://127.0.0.1:9297/example-site/ | grep -v -i -E "^(x-amz-id-2|x-amz-request-id|date):"; echo
HTTP/1.1 403 Forbidden
content-type: application/xml
content-length: 192
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>0005S99PHNZ4E09P</RequestId><HostId>wLvzzM7QNLRWP2yhsy3ZhtC7YKkwdPlIsoUsKH195mXzBp5S58MITSy3SzA4/qWz</HostId></Error>
ウェブサイトエンドポイントに PUT で送ると、405 Method Not Allowed が返りました。公式ドキュメントの比較表にも、ウェブサイトエンドポイントが受け付けるのはファイルへの GET と HEAD だけで、エラーは HTML の文書で返すと書かれています。
$ site example-site /new.html -X PUT --data hello
HTTP/1.1 405 Method Not Allowed
content-type: text/html
content-length: 219
<html><head><title>405 MethodNotAllowed</title></head><body><h1>405 MethodNotAllowed</h1><ul><li>Code: MethodNotAllowed</li><li>Message: The specified method is not allowed against this resource.</li></ul></body></html>
ファイルごとに転送先を決める
ページを移したときに、古い URL から新しい URL へ転送するには、古いファイルに --website-redirect-location を付けて置きます。old.html に /index.html を指定して置くと、ウェブサイトエンドポイントでは 301 Moved Permanently と location: /index.html が返りました。
$ aws s3api put-object --bucket example-site --key old.html --body old.html --content-type text/html --website-redirect-location /index.html --query ETag
"\"814fa5ca98406a903e22b43d9b610105\""
$ site example-site /old.html
HTTP/1.1 301 Moved Permanently
location: /index.html
content-length: 0
AWS CLI で取り出すと、転送されずにファイルの中身がそのまま返り、転送先は WebsiteRedirectLocation に入っていました。
$ aws s3api get-object --bucket example-site --key old.html out.txt --query WebsiteRedirectLocation && cat out.txt
"/index.html"
old
リダイレクトの公式ドキュメントにも、この設定のファイルは、ウェブサイトエンドポイントでは 301 で転送され、REST のエンドポイントでは転送されずにファイルが返ると書かれています。同じバケットの別のファイルを指すときは、値の先頭に / を付けます。
ルーティングルールでまとめて転送する
フォルダーごと名前を変えたときのように、たくさんのページを転送するときは、ウェブサイトの設定にルーティングルール(RoutingRules)を書きます。次の website.json には、3つのルールを書きました。
blog/で始まるパスを、articles/で始まるパスに転送するimages/で始まるパスを、すべてfolderdeleted.htmlに 302 で転送する- 404 になるリクエストを、
https://example.com/report-404/の下に転送する
{
"IndexDocument": { "Suffix": "index.html" },
"ErrorDocument": { "Key": "error.html" },
"RoutingRules": [
{
"Condition": { "KeyPrefixEquals": "blog/" },
"Redirect": { "ReplaceKeyPrefixWith": "articles/" }
},
{
"Condition": { "KeyPrefixEquals": "images/" },
"Redirect": { "ReplaceKeyWith": "folderdeleted.html", "HttpRedirectCode": "302" }
},
{
"Condition": { "HttpErrorCodeReturnedEquals": "404" },
"Redirect": { "HostName": "example.com", "Protocol": "https", "ReplaceKeyPrefixWith": "report-404/" }
}
]
}
aws s3 website にはルールを渡すオプションがないので、aws s3api put-bucket-website で設定をまるごと渡します。
$ aws s3api put-bucket-website --bucket example-site --website-configuration file://website.json
$ aws s3api get-bucket-website --bucket example-site --query "RoutingRules[].Condition"
[
{
"KeyPrefixEquals": "blog/"
},
{
"KeyPrefixEquals": "images/"
},
{
"HttpErrorCodeReturnedEquals": "404"
}
]
それぞれのルールに当たるパスに送ると、次のように返りました。
$ site example-site /blog/2026/hello.html
HTTP/1.1 301 Moved Permanently
location: http://example-site.s3-website-us-east-1.amazonaws.com/articles/2026/hello.html
content-length: 0
$ site example-site /images/cat.png
HTTP/1.1 302 Found
location: http://example-site.s3-website-us-east-1.amazonaws.com/folderdeleted.html
content-length: 0
$ site example-site /nothing.html
HTTP/1.1 301 Moved Permanently
location: https://example.com/report-404/nothing.html
content-length: 0
ReplaceKeyPrefixWith は、パスの前の部分だけを置き換えるので、blog/2026/hello.html は articles/2026/hello.html になりました。ReplaceKeyWith は、パス全体を指定した名前に置き換えます。HttpRedirectCode を書かないルールは 301 になりました。転送先の location には、Host ヘッダーで送ったホスト名と http:// が入っています。
404 のルールを入れたあとは、前の節で error.html が返った /nothing.html が、エラーページではなく転送になりました。リダイレクトの公式ドキュメントには、404 のルーティングルールがないときに、エラードキュメントが返ると書かれています。ルールに当たらない /index.html は、今までどおり 200 で返りました。
$ site example-site /index.html
HTTP/1.1 200 OK
content-type: text/html
content-length: 15
<h1>Hello</h1>
書き間違えた設定は断られる
次の3つの設定は、put-bucket-website の時点で断られました。インデックスドキュメントを書かずにエラードキュメントだけを書いたもの、インデックスドキュメントの名前に / を入れたもの、1つのルールに ReplaceKeyWith と ReplaceKeyPrefixWith の両方を書いたものです。
$ aws s3api put-bucket-website --bucket example-site --website-configuration '{"ErrorDocument":{"Key":"error.html"}}'
aws: [ERROR]: An error occurred (InvalidArgument) when calling the PutBucketWebsite operation: A value for IndexDocument Suffix must be provided if RedirectAllRequestsTo is empty
$ aws s3api put-bucket-website --bucket example-site --website-configuration '{"IndexDocument":{"Suffix":"docs/index.html"}}'
aws: [ERROR]: An error occurred (InvalidArgument) when calling the PutBucketWebsite operation: The IndexDocument Suffix is not well formed
$ aws s3api put-bucket-website --bucket example-site --website-configuration '{"IndexDocument":{"Suffix":"index.html"},"RoutingRules":[{"Condition":{"KeyPrefixEquals":"a/"},"Redirect":{"ReplaceKeyWith":"x.html","ReplaceKeyPrefixWith":"b/"}}]}'
aws: [ERROR]: An error occurred (InvalidRequest) when calling the PutBucketWebsite operation: You can only define ReplaceKeyPrefix or ReplaceKey but not both.
断られたあとに読み出すと、ルーティングルールは前の3つのままでした。
$ aws s3api get-bucket-website --bucket example-site --query "length(RoutingRules)"
3
IndexDocument の API リファレンスには、インデックスドキュメントの名前(Suffix)は空にできず、スラッシュを含められないと書かれています。ReplaceKeyWith と ReplaceKeyPrefixWith のどちらか一方しか書けないことは、リダイレクトの公式ドキュメントのルールの説明にあります。
すべてのリクエストを別のホストに転送する
www.example.com のバケットへのアクセスを、すべて example.com に送りたいときは、RedirectAllRequestsTo を使います。WebsiteConfiguration の API リファレンスによると、これを書くときは、ほかの項目は書けません。エミュレーターでは、パスを残したまま https://example.com に 301 で転送されました。
$ aws s3 mb s3://www.example.com
make_bucket: www.example.com
$ aws s3api put-bucket-website --bucket www.example.com --website-configuration '{"RedirectAllRequestsTo":{"HostName":"example.com","Protocol":"https"}}'
$ site www.example.com /about/team.html
HTTP/1.1 301 Moved Permanently
location: https://example.com/about/team.html
content-length: 0
ウェブサイトの設定を消す
delete-bucket-website で設定を消すと、ウェブサイトエンドポイントは 404 NoSuchWebsiteConfiguration を返しました。存在しないバケットの名前で送ったときは、404 NoSuchBucket です。どちらも HTML で返ります。
$ aws s3api delete-bucket-website --bucket example-site
$ site example-site /
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 249
<html><head><title>404 NoSuchWebsiteConfiguration</title></head><body><h1>404 NoSuchWebsiteConfiguration</h1><ul><li>Code: NoSuchWebsiteConfiguration</li><li>Message: The specified bucket does not have a website configuration</li></ul></body></html>
$ site no-such-bucket /
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 184
<html><head><title>404 NoSuchBucket</title></head><body><h1>404 NoSuchBucket</h1><ul><li>Code: NoSuchBucket</li><li>Message: The specified bucket does not exist</li></ul></body></html>
エラードキュメントの公式ドキュメントにも、ウェブサイトエンドポイントが 404 を返す場合として、バケットが無いときと、バケットがあってもウェブサイトとして設定されていないときが挙がっています。サイトの配信を止めたつもりの手順をテストするときは、この 404 が返るかどうかで確かめられます。





