AWS Beanstalk returns 404 as response, if I use query/S3-upload

Ich habe eine ExpressJS-Anwendung, die auf AWS Elastic Beanstalk gehostet wird und eine einzige Route /minify bereitstellt. Diese Route komprimiert und minifiziert übergebene Dateien und sendet sie als Antwort zurück. Zusätzlich gibt es eine optionale Query save=1, die die Datei zusätzlich auf AWS S3 speichert.

Das Problem tritt auf, wenn ich diese Query verwende. Obwohl die Datei erfolgreich auf S3 gespeichert wird, erhalte ich als Antwort einen HTTP 404 Status. Wenn ich die Funktion zum Speichern auf S3 auskommentiere, funktioniert alles korrekt und ich erhalte die erwartete Antwort.

Hier ist der relevante Teil meines Codes:

minifyController.ts:

export async function minifyController(
  req: Request,
  res: Response,
): Promise<void> {
  const saveFileOnS3 =
    parseFloat((req.query.save as string | undefined) || "0") === 1;

  console.log("saveFileOnS3", saveFileOnS3);

  const multerReq = req as Request & {
    files: { [fieldname: string]: Express.Multer.File[] };
  };

  if (
    !multerReq ||
    !multerReq.files ||
    (!multerReq.files.configFile && !multerReq.files.templateFile)
  ) {
    res.status(400).send(ERROR_MESSAGES.missingFiles);
    return;
  }
  if (!multerReq.files.configFile) {
    res.status(400).send(ERROR_MESSAGES.missingConfigFile);
    return;
  }
  if (!multerReq.files.templateFile) {
    res.status(400).send(ERROR_MESSAGES.missingTemplateFile);
    return;
  }

  try {
    const configFileContent =
      multerReq.files.configFile[0].buffer.toString("utf-8");
    const templateFileContent =
      multerReq.files.templateFile[0].buffer.toString("utf-8");

    const combinedContent = `${configFileContent}n${templateFileContent}`;

    const minified = await minifyFile(combinedContent);

    if (minified.code === undefined)
      throw new Error(ERROR_MESSAGES.minificationFailed);

    const brotli = await compressFile(minified.code);

    if (saveFileOnS3) await AWSUpload(brotli);

    console.log("Minification and compression successful");

    if (!res.headersSent)
      res
        .status(200)
        .setHeader("Content-Type", "application/javascript")
        .send(brotli);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- This is a catch-all error handler and the error can be of any type
  } catch (error: any) {
    console.error("Error in minification or compression:", error);
    res.status(500).send({ error: error.message || error });
  }
}

aws.ts:

export async function AWSUpload(file: Buffer): Promise<void> {
  const AWS_COMPRESSED_FILES_BUCKET = process.env.AWS_COMPRESSED_FILES_BUCKET;

  if (!AWS_COMPRESSED_FILES_BUCKET || AWS_COMPRESSED_FILES_BUCKET === "")
    throw new Error("AWS_COMPRESSED_FILES_BUCKET is not defined");

  const params = {
    Bucket: AWS_COMPRESSED_FILES_BUCKET,
    Key: `${crypto.randomUUID()}.min.js`,
    Body: file,
    ContentType: "application/javascript",
    ContentEncoding: "br", // Brotli encoding
  };

  try {
    const command = new PutObjectCommand(params);

    const response = await S3Client.send(command);

    if (response.$metadata.httpStatusCode !== 200) {
      throw new Error(
        `Failed to upload file to AWS - ${response.$metadata.httpStatusCode}`,
      );
    }
  } catch (error) {
    throw new Error(`Failed to upload file to AWS - ${error}`);
  }
}

index.ts

import app from "./config/init";

// The requestLogger middleware is only used in development
import { requestLogger } from "./middlewares/dev";
app.use(requestLogger);

// minify-Router
import minifyRouter from "./routes/minify";
app.use("/minify", minifyRouter);

Ich habe festgestellt, dass der Fehler nicht auftritt, wenn ich eine separate Route für die S3-Version definiere. Was könnte die Ursache dafür sein, dass der 404 Fehler nur auftritt, wenn ich versuche, die Datei auf S3 zu speichern?

Was ich bereits versucht habe:

  • Auskommentieren der S3-Funktion: dies führt dazu, dass alles korrekt funktioniert.
  • Erstellung einer separaten Route speziell für das Speichern auf S3: das Problem bleibt bestehen.
  • Port 5000 definiert

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật