【hugo】使用 AWS S3 服务 hugo 博客

Posted by 西维蜀黍 on 2020-09-05, Last Modified on 2021-09-21

Create Buckets

Set up non-WWW Bucket (Root Domain Bucket)

One bucket for the naked domain called: example.com. This bucket will hold our static website files.

  1. Go to https://console.aws.amazon.com/s3/home
  2. Press Create bucket button
    1. Enter bucket name: example.com
    2. Choose the Region where you want to create the bucket.
    3. Press Next, Next and Create bucket.
      • Now your bucket list should contain the example.com bucket
  3. Click example.com bucket and select the Properties tab.

Set up WWW Bucket (Subdomain Bucket)

One bucket for the naked domain called: example.com. This bucket will hold our static website files.

  1. Go to https://console.aws.amazon.com/s3/home
  2. Press Create bucket button
    1. Enter bucket name: www.example.com
    2. Press Next, Next and Create bucket.
      • Now your bucket list should contain the www.example.com bucket
  3. Click www.example.com bucket and select the Properties tab.

Config Buckets

Configure your root domain bucket for website hosting

In this step, you configure your root domain bucket (example.com) as a website. This bucket will contain your website content. When you configure a bucket for website hosting, you can access the website using the Website endpoints.

To enable static website hosting

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.amazonaws.cn/s3/.

  2. In the Bucket name list, choose the bucket that you want to use for your static website.

  3. Choose Properties.

  4. Choose Static website hosting.

  5. Choose Use this bucket to host a website.

  6. Enter the name of your index document.

    The index document name is typically index.html. The index document name is case sensitive and must exactly match the file name of the HTML index document that you plan to upload to your S3 bucket. For more information, see Configuring an index document.

  7. (Optional) If you want to add a custom error document, in the Error document box, enter the key name for the error document (for example, error.html).

    The error document name is case sensitive and must exactly match the file name of the HTML error document that you plan to upload to your S3 bucket. For more information, see (Optional) configuring a custom error document.

  8. (Optional) If you want to specify advanced redirection rules, in Edit redirection rules, use XML to describe the rules.

    For more information, see Configuring advanced conditional redirects.

  9. Under Static website hosting, note the Endpoint.

    The Endpoint is the Amazon S3 website endpoint for your bucket. After you finish configuring your bucket as a static website, you can use this endpoint to test your website.

  10. Choose Save.

In the next step, you configure your subdomain (www.example.com) to redirect requests to your domain (example.com).

Configure your subdomain bucket for website redirect

After you configure your root domain bucket for website hosting, you can configure your subdomain bucket to redirect all requests to the domain. In this example, all requests for www.example.com are redirected to example.com.

To configure a redirect request

  1. On the Amazon S3 console, in the Buckets list, choose your subdomain bucket ( www.example.com in this example).
  2. Choose Properties.
  3. Choose Static website hosting.
  4. Choose Redirect requests.
  5. In the Target bucket or domain box, enter your domain (for example, example.com).
  6. In the Protocol box, enter http.
  7. Choose Save.

Edit Permission

Edit block public access settings

In this example, you edit block public access settings for the domain bucket (example.com) to allow public access.

By default, Amazon S3 blocks public access to your account and buckets. If you want to use a bucket to host a static website, you can use these steps to edit your block public access settings.

  1. Open the Amazon S3 console at https://console.amazonaws.cn/s3/.

  2. Choose the name of the bucket that you have configured as a static website.

  3. Choose Permissions.

  4. Choose Edit.

  5. Clear Block *all* public access, and choose Save.

  6. In the confirmation box, enter confirm, and then choose Confirm.

    Under S3 buckets, the Access for your bucket updates to Objects can be public. You can now add a bucket policy to make the objects in the bucket publicly readable. If the Access still displays as Bucket and objects not public, you might have to edit the block public access settings for your account before adding a bucket policy.

    Attach a bucket policy

    After you edit S3 Block Public Access settings, you can add a bucket policy to grant public read access to your bucket. When you grant public read access, anyone on the internet can access your bucket.

    1. Under Buckets, choose the name of your bucket.

    2. Choose Permissions.

    3. Choose Bucket Policy.

    4. To grant public read access for your website, copy the following bucket policy, and paste it in the Bucket policy editor.

    { “Version”: “2012-10-17”, “Statement”: [ { “Sid”: “PublicReadGetObject”, “Effect”: “Allow”, “Principal”: “”, “Action”: [ “s3:GetObject” ], “Resource”: [ “arn:aws-cn:s3:::example.com/” ] } ] }

    
    5. Update the `Resource` to include your bucket name.
    
    In the preceding example bucket policy, `example.com` is the bucket name. To use this bucket policy with your own bucket, you must update this name to match your bucket name.
    
    6. Choose **Save**.
    
    A warning appears indicating that the bucket has public access. In **Bucket Policy**, a **Public** label appears.
    

Upload index and website content

In this step, you upload your index document and optional website content to your root domain bucket.

When you enable static website hosting for your bucket, you enter the name of the index document (for example, index.html). After you enable static website hosting for the bucket, you upload an HTML file with this index document name to your bucket.

Test your domain endpoint

After you configure your domain bucket to host a public website, you can test your endpoint. For more information, see Website endpoints. You will only be able to test the endpoint for your domain bucket since your subdomain bucket is set up for website redirect and not static website hosting.

Note

Amazon S3 does not support HTTPS access to the website. If you want to use HTTPS, you can use Amazon CloudFront to serve a static website hosted on Amazon S3.

If you noted your website endpoint when you enabled static website hosting, to test your website, enter the website endpoint in your browser. If your browser displays your index.html page, the website was successfully deployed. For more information, see Amazon S3 Website Endpoints.

If you need to get your website endpoint before testing, follow these steps:

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.

  2. In Buckets list, choose the name of the bucket that you want to use to host a static website.

  3. Choose Properties.

  4. Choose Static website hosting.

  5. To test your website endpoint, next to Endpoint, choose your website endpoint.

    If your browser displays your index.html page, the website was successfully deployed.

Use Amazon S3 for DNS Resolving

In this step, you create the alias records that you add to the hosted zone for your domain maps example.com and www.example.com. Instead of using IP addresses, the alias records use the Amazon S3 website endpoints. Amazon Route 53 maintains a mapping between the alias records and the IP addresses where the Amazon S3 buckets reside. You create two alias records, one for your root domain and one for your subdomain.

To add an alias record for your root domain (example.com)

  1. Open the Route 53 console at https://console.aws.amazon.com/route53/.

    Note

    If you don’t already use Route 53, see Step 1: Register a Domain in the Amazon Route 53 Developer Guide. After completing your setup, you can resume the instructions.

  2. Choose Hosted zones.

  3. In the list of hosted zones, choose the name of the hosted zone that matches your domain name.

  4. Choose Create record.

  5. Choose Simple routing, and choose Next.

  6. Choose Define simple record.

  7. In Record name accept the default value, which is the name of your hosted zone and your domain.

  8. In Value/Route traffic to, choose Alias to S3 website endpoint.

  9. Choose the Region.

  10. Choose the S3 bucket.

    The bucket name should match the name that appears in the Name box. In the Alias Target listing, the bucket name is followed by the Amazon S3 website endpoint for the Region where the bucket was created, for example example.com (s3-website-us-west-2). Alias Target lists a bucket if:

    • You configured the bucket as a static website.
    • The bucket name is the same as the name of the record that you’re creating.
    • The current AWS account created the bucket.

    If your bucket does not appear in the Alias Target listing, enter the Amazon S3 website endpoint for the Region where the bucket was created, for example, s3-website-us-west-2. For a complete list of Amazon S3 website endpoints, see Amazon S3 Website Endpoints. For more information about the alias target, see Alias Target in the Amazon Route 53 Developer Guide.

  11. In Record type, choose A ‐ Routes traffic to an IPv4 address and some AWS resources.

  12. For Evaluate target health, choose No.

  13. Choose Define simple record.

To add an alias record for your subdomain (www.example.com)

  1. In the hosted zone for your root domain (example.com), choose Create record.
  2. In Record name for your subdomain, type www.
  3. In Value/Route traffic to, choose Alias to S3 website endpoint.
  4. Choose the Region.
  5. Choose the S3 bucket, for example example.com (s3-website-us-west-2).
  6. In Record type, choose A ‐ Routes traffic to an IPv4 address and some AWS resources.
  7. For Evaluate target health, choose No.
  8. Choose Define simple record.

Reference