如何从s3.getObject呈现index.html,该s3.getObject获取了S3上托管的网站?

问题描述 投票:0回答:1

我具有由AWS API网关调用的Lambda函数,在该lambda函数上,我需要在S3存储桶中为网站提供服务,以便我可以控制对该网站的访问和路由。

我使用的以下方式:

const AWS = require('aws-sdk');
//*/ get reference to S3 client 
var s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
    var params = {
  "Bucket": "mybucket",
  "Key": "index.html" 
    };


    s3.getObject(params, function(err, data){
       if(err) {
           callback(err, null);
       } 
       else {
           let response = {
                "statusCode": 200,
                "headers":
                {
                  "Access-Control-Allow-Origin": "*", // to 
                //   "Content-Type": "text/html"
                },
                "body": JSON.stringify(data),
                "isBase64Encoded": false
            };
           callback(null, response);
        }
    });
};

给出以下不需要的输出。

{"AcceptRanges":"bytes","LastModified":"2020-03-28T08:10:04.000Z","ContentLength":661,"ETag":"\"08ea404c1973c83c8731a80bf5852d05\"","ContentType":"text/html","Metadata":{},"Body":{"type":"Buffer","data":[60,33,68,79,67,84,89,80,69,32,104,116,109,108,62,13,10,60,104,116,109,108,32,108,97,110,103,61,34,101,110,34,62,13,10,60,104,101,97,100,62,13,10,32,32,60,116,105,116,108,101,62,66,111,111,1...}
amazon-s3 aws-lambda routing amazon-api-gateway
1个回答
0
投票
const AWS = require('aws-sdk'); var s3 = new AWS.S3(); exports.handler = (event, context, callback) => { var params = { "Bucket": "mybucket", "Key": "index.html" }; const data = await s3.getObject(params).promise(); callback(null, { "statusCode": 200, "headers": { "Access-Control-Allow-Origin": "*", }, "body": JSON.stringify(data), "isBase64Encoded": false }); };
© www.soinside.com 2019 - 2024. All rights reserved.