日本語

How to Test S3 Static Website Hosting with AWS Emulator

​​AWS Emulator​Amazon S3,AWS CLI,curl

AWS Emulator not only accepts S3 static website hosting settings, it also responds as a website endpoint. By sending requests with a Host header via curl, you can check locally, without an AWS account, how the index page, a custom error page, and redirect rules behave. This is useful for testing a site that serves HTML and images from S3, or redirect rules for moved pages, before putting them on a real bucket.

Note

The test environment was Ubuntu 24.04 (x86_64), AWS Emulator 1.0.602 (released November 24, 2026), AWS CLI 2.42.2, and curl 8.5.0. I did not use an AWS account or credentials. I did not check how the real S3 website endpoint behaves, how pages render in a browser, how it works with CloudFront or a custom domain, or pricing. Where I describe the real S3, I am relying on the official documentation. According to the AWS Emulator official documentation, AWS checks the emulator's behavior against the real service only in the US West (Oregon, us-west-2) Region (global services use us-east-1). The us-east-1 S3 used in this article is outside that scope.

Start the emulator and put the pages in place

AWS Emulator is an emulator that accepts AWS API calls locally. According to the official documentation, no AWS account is needed and requests are not sent to AWS. How to install it and the environment variables that point the AWS CLI at the emulator are covered in the separate articles "How to install and start using AWS Emulator on Ubuntu" and "How to put and get files in S3 with AWS Emulator". Here, I worked with an emulator started with aws-emulator -t off in a shell where the environment variables from the official documentation were set. -t off turns off the usage data (telemetry) that the emulator sends to AWS by default.

If the connection works, the user name in the ARN returned by aws sts get-caller-identity is emulator (the 12-digit account ID is replaced).

text
$ aws sts get-caller-identity --query Arn --output text
arn:aws:iam::<12桁のアカウントID>:user/emulator

I created the bucket example-site and put index.html as the top page, error.html as the error page, and docs/index.html as the top page of a subfolder. The Content-Type of index.html uploaded with aws s3 cp was text/html.

text
$ 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"

Apply the website configuration

A newly created bucket has no website configuration. get-bucket-website returned NoSuchWebsiteConfiguration.

text
$ 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

The configuration can be applied with aws s3 website. --index-document is the file name returned for a URL that points to a folder (the index document), and --error-document is the file returned on an error (the error document).

text
$ 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"
    }
}

Send requests to the website endpoint with a Host header

On the real S3, a bucket published as a website is reached through a website endpoint that is separate from the REST API. In us-east-1, the form is http://<バケット名>.s3-website-us-east-1.amazonaws.com.

When a request reached https://127.0.0.1:9297 with a Host header in this form, the emulator responded as a website endpoint. To avoid writing the same options every time, I created the following function site and loaded it into the shell. For the certificate, I specified ca.pem exported by the emulator. From the response headers, I removed the request ID and date lines, which change every time.

bash
# site <bucket name> <path> [other curl options]: Sends the request to the emulator with the website endpoint's Host header
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; }

The real website endpoint is HTTP only, and the official documentation says it does not support HTTPS. When sending to the emulator, I used HTTPS to match how the emulator listens. I also tried a method that keeps the hostname in the URL and points only the connection at the emulator (curl's --connect-to), but it failed certificate verification because the emulator's certificate is for localhost and 127.0.0.1.

text
curl: (60) SSL: no alternative certificate subject name matches target host name 'example-site.s3-website-us-east-1.amazonaws.com'

Without a public access setting, 403 is returned

When I sent a request with only the website configuration applied, 403 AccessDenied was returned as HTML.

text
$ 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>

According to the official documentation on website access permissions, to publish a site on the real S3, you turn off Block Public Access for the bucket and add a bucket policy that allows anyone to call s3:GetObject. I prepared a policy with the same content as the example in the official documentation.

json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::example-site/*"
    }
  ]
}

In the emulator too, a newly created bucket has all four Block Public Access settings set to true, and this policy was rejected because of BlockPublicPolicy.

text
$ 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.

After I deleted the Block Public Access settings, the policy could be added.

text
$ aws s3api delete-public-access-block --bucket example-site
$ aws s3api put-bucket-policy --bucket example-site --policy file://policy.json
Warning

The official documentation says that if you turn off Block Public Access on the real S3, anyone on the internet can access the bucket. Also, according to the AWS Emulator official documentation, the emulator does not evaluate IAM policies or bucket policies the same way AWS does. A page being returned by the emulator does not mean the same public access settings are safe on a real bucket.

How the index page and the error page are returned

After the public access setting was applied, index.html was returned for both / and /index.html. A path ending in a slash, such as /docs/, returned docs/index.html.

text
$ 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>

When I pointed to a file that does not exist, the contents of error.html were returned with the status code 404 Not Found.

text
$ site example-site /nothing.html
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 19

<h1>Not found</h1>

The behavior differed from the real S3 as described in the official documentation for /docs without a trailing slash. According to the official documentation on the index document, when there is no docs file, the real S3 looks for docs/index.html and, if it exists, redirects to docs/ with 302 Found. In the emulator, there was no redirect, and a 404 with the error page was returned.

text
$ site example-site /docs
HTTP/1.1 404 Not Found
content-type: text/html
content-length: 19

<h1>Not found</h1>
Diagram of requests and responses to the website endpoint. / returns 200 with index.html, /docs/ returns 200 with docs/index.html, and /nothing.html returns 404 with error.html. In the emulator, /docs returns 404 with error.html, but the official documentation says the real S3 redirects it with 302 to /docs/
Responses per path (only /docs differs from the real S3)

If a site links to folders without a trailing slash, testing it in the emulator will return 404 for pages that the real service would redirect. If you see a 404 in your tests, check whether your links are written this way.

Differences from the REST API endpoint

The website endpoint returns errors as HTML. When I sent a request without a signature to the same bucket using the REST API path form (https://127.0.0.1:9297/example-site/), the error was returned as XML.

text
$ 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>

When I sent PUT to the website endpoint, 405 Method Not Allowed was returned. The official comparison table also says that the website endpoint accepts only GET and HEAD for files, and returns errors as HTML documents.

text
$ 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>

Set the redirect destination per file

To redirect an old URL to a new URL when a page has moved, put the old file with --website-redirect-location. When I put old.html with /index.html as the target, the website endpoint returned 301 Moved Permanently and location: /index.html.

text
$ 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

When I retrieved the object with the AWS CLI, the file contents were returned as-is without a redirect, and the destination was in WebsiteRedirectLocation.

text
$ aws s3api get-object --bucket example-site --key old.html out.txt --query WebsiteRedirectLocation && cat out.txt
"/index.html"
old

The official redirect documentation also says that a file with this setting is redirected with 301 on the website endpoint, while on the REST endpoint the file is returned without a redirect. To point to another file in the same bucket, put a / at the start of the value.

Redirect in bulk with routing rules

When you rename a folder and need to redirect many pages, write routing rules (RoutingRules) in the website configuration. The following website.json has three rules.

  • Redirect paths starting with blog/ to paths starting with articles/
  • Redirect all paths starting with images/ to folderdeleted.html with 302
  • Redirect requests that would return 404 to the path under https://example.com/report-404/
json
{
  "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 has no option to pass rules, so I passed the whole configuration with aws s3api put-bucket-website.

text
$ 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"
    }
]

When I sent requests to paths that match each rule, the responses were as follows.

text
$ 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 replaces only the front part of the path, so blog/2026/hello.html became articles/2026/hello.html. ReplaceKeyWith replaces the whole path with the given name. A rule without HttpRedirectCode returned 301. In the location of the destination, the hostname sent in the Host header and http:// are used.

After I added the 404 rule, /nothing.html, which had returned error.html in the previous section, was redirected instead of returning the error page. The official redirect documentation says that without a 404 routing rule, the error document is returned. /index.html, which matches no rule, still returned 200 as before.

text
$ site example-site /index.html
HTTP/1.1 200 OK
content-type: text/html
content-length: 15

<h1>Hello</h1>

Incorrect configurations are rejected

The following three configurations were rejected by put-bucket-website: one with an error document but no index document, one with a / in the index document name, and one rule that specified both ReplaceKeyWith and ReplaceKeyPrefixWith.

text
$ 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.

When I read the configuration afterward, the routing rules were still the previous three.

text
$ aws s3api get-bucket-website --bucket example-site --query "length(RoutingRules)"
3

The IndexDocument API reference says the index document name (Suffix) cannot be empty and cannot contain a slash. The fact that only one of ReplaceKeyWith and ReplaceKeyPrefixWith can be written is explained in the rule description of the official redirect documentation.

Redirect all requests to another host

If you want to send all requests to a bucket such as www.example.com to example.com, use RedirectAllRequestsTo. According to the WebsiteConfiguration API reference, when you write this, you cannot write the other items. In the emulator, the request was redirected with 301 to https://example.com with the path kept.

text
$ 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

Remove the website configuration

When I removed the configuration with delete-bucket-website, the website endpoint returned 404 NoSuchWebsiteConfiguration. When I sent a request using the name of a bucket that does not exist, it returned 404 NoSuchBucket. Both are returned as HTML.

text
$ 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>

The official error document documentation lists, as cases where the website endpoint returns 404, both when the bucket does not exist and when the bucket exists but is not configured as a website. When testing a procedure meant to stop serving a site, you can confirm it by checking whether this 404 is returned.

​Posted by 86