简体   繁体   English

如何通过 API 网关公开存储在 aws lambda 代码中的 static 目录?

[英]how to expose static directories stored in aws lambda code over API gateway?

I am trying to serve static react app bundle using aws lambda only.我正在尝试仅使用 aws lambda 服务 static 反应应用程序包。 I have used a NodejsFunction and applied commandHooks during bundling to bundle a react build along with my code as shown below.我在捆绑期间使用了NodejsFunction并应用了commandHooks以将反应构建与我的代码捆绑在一起,如下所示。 I have also attached it to an API gateway as u can see.如您所见,我还将它附加到 API 网关。

  private uiLmbda = new NodejsFunction(this, "renderer", {
    entry: path.join(__dirname, "..", "handlers", "ui-handler.ts"),
    handler: "handler",
    bundling: {
      commandHooks: {
        beforeBundling(inputDir: string, outputDir: string) {
          const staticAssets = path.join(__dirname, "..", "build");
          const relativePath = path.relative(inputDir, staticAssets);
          return [`cp -r ${relativePath} ${outputDir}`];
        },
        afterBundling(inputDir: string, outputDir: string) {
          return [];
        },
        beforeInstall() {
          return [];
        },
      },
    },
  });

  private scanAPI = new RestApi(this, "scan-api");
  private uiGatewayIntegration: LambdaIntegration = new LambdaIntegration(
    this.uiLmbda
  );

And in constructor i am calling this:-在构造函数中我称之为:-

 this.scanAPI.root.addMethod("GET", this.uiGatewayIntegration, {});

Now,i have an index.js as my lambda handler and a build folder with index.html and other refered static files as shown below.现在,我有一个index.js作为我的 lambda 处理程序和一个包含index.html和其他引用 static 文件的构建文件夹,如下所示。

在此处输入图像描述

the handler code is as shown:-处理程序代码如下所示:-

import * as fs from "fs";
import * as path from "path";

export const handler = async (event: any) => {
  try {
    console.log(path.resolve("./build/index.html"));
    return {
      statusCode: 200,
      headers: {
        "content-type": "text/html",
      },
      body: fs
        .readFileSync(path.join(__dirname, "build", "index.html"))
        .toString("utf-8"),
      isBase64Encoded: false,
    };
  } catch (error) {
    console.log(error);
  }
};


so i am able to send the html using above handler.所以我可以使用上面的处理程序发送 html。 but not the relative files as it seems the api gateway is not aware that i wish to render subdirectories from the get method.但不是相关文件,因为 api 网关似乎不知道我希望从 get 方法呈现子目录。 Any help on how i can do that?关于我该怎么做的任何帮助? attching screenshots where u can see that main.js ( referenced through html as <script src='/task/var/build/main'><script/> ) gives 403 from api gateway.附加屏幕截图,您可以在其中看到 main.js(通过 html 引用为<script src='/task/var/build/main'><script/> )从 api 网关提供 403。

在此处输入图像描述

Okay, turns out i needed to handle this case in 2 places, one was api gateway and other was lambda handler.好的,原来我需要在 2 个地方处理这个案例,一个是 api 网关,另一个是 lambda 处理程序。 I attached the lambda to serve static files using express.static and in api gateway i enabled proxy, and it worked.我附加了 lambda 以使用express.static服务 static 文件,在 api 网关中我启用了代理,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM