前提
- S3 のwebサイトホスティングをcliでやってみようの巻。
- 今回は bucket-acl のみで bucket-policy は使ってません。
- CloudFrontは無しの構成です。
流れ
- バケット作成
- webサイト設定の有効化
- コンテンツを格納
- 公開
手順
バケット作成
aws s3 mb s3://mybucket
webサイト設定の有効化
以下のファイルを作成する(website.json)
website.json(例)
{
"IndexDocument": {
"Suffix": "index.html"
},
"ErrorDocument": {
"Key": "error.html"
}
}
上で作ったファイルをオプションで指定してwebサイト用の設定を投入
aws s3api put-bucket-website --bucket mybucket --website-configuration file://website.json
コンテンツ格納
aws s3 cp index.html s3://mybucket/index.html --acl public-read
- その他のファイルをアップロードする場合は、上のコマンドの index.html を置き換えてください。
- 注意
-
aws s3api put-object
を使う場合はContent-type
(text/html)やContent-Length
オプションを設定して下さい。aws s3 cp
であれば自動でいい感じに設定してくれますが、aws s3api put-object
だと設定してくれないです。
-
公開
aws s3api put-bucket-acl --bucket mybucket --acl public-read
アクセス
http://mybucket.s3-website-us-west-2.amazonaws.com/
- APIのエンドポイントとURLが異なりますのでご注意ください
- リージョンはバケットを作成したリージョンに置き換えてください