How to use lambda write a html file upload to s3 as a static web page hosting

Viewed 69

I have a bucket in s3 : http://www.sentencex.com.s3-website-us-east-1.amazonaws.com/

The root page is index.html.

Now I use vscode write a new html file, hello world from local.html,use AWS console upload to my bucket.

When Url route to:

http://www.sentencex.com.s3-website-us-east-1.amazonaws.com/hello%20world%20from%20local.html

it return the 'hello world from local' page from browser.

But When I use lambda to create a file called hello world from lambda.html.

This is My code in lambda:

const AWS = require('aws-sdk')
const s3 = new AWS.S3()

exports.handler = async (event) => {
    let html = `
        <!DOCTYPE html>
        <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Document</title>
        </head>
        <body>
          <h1 class="sayHello">Hello</h1>
          <script src="./app.js" type="module"></script>
        </body>
        </html>
    `
    let putHtmlParam = {
        Body:html,
        Metadata: {
        'Content-Type': 'text/html'
        },
        Bucket:'www.sentencex.com',
        Key:'hello world from lambda.html',
    }
    let putHtmlResult = await s3.putObject(putHtmlParam).promise()
    return putHtmlResult

};

I can upload this hello world from lambda.html to my s3.

But when the url route to

http://www.sentencex.com.s3-website-us-east-1.amazonaws.com/hello%20world%20from%20lambda.html

It can not open the page in the browser directly.It has to download the page and read it from local.

0 Answers
Related