【问题标题】:Google Cloud Run is returning error 503 - Service UnavailableGoogle Cloud Run 返回错误 503 - 服务不可用
【发布时间】:2021-05-10 04:08:24
【问题描述】:

由于某种我无法发现的原因,云运行总是返回 503 - 不可用。不管我做什么。这在本地有效,所以我不知道可能是什么问题。应用程序的所有其他端点都可以工作。一切正常运行,但是这个请求,由于某种原因,只能在 localhost 上运行,但是当部署到 GCR 时,它就不起作用了。

我想要做的是将文件上传到谷歌驱动器。这是请求的逻辑:

router.post("/upload", async(req, res)=>{
  const payload = req.body;
  const resource = {
    name: payload.fileName || "Picture",
    driveId: process.env.DRIVE_ID
  };
  const uploadedFile = await uploadToTeamDrive(payload.imgData, resource).catch(error=>{
    console.error(`Error uploading file to team drive: ${error.toString()}`);
    gapiError(res, error);
  });
  if(!uploadedFile){ return; }
  res.status(201).send({success: true});
});

const uploadToTeamDrive = async(base64Img, resource)=>{
  const base64String = base64Img.split(",")[1];
  const imgData = Readable.from(Buffer.from(base64String, "base64"));
  const mimeType = base64Img.split(",")[0].split(";")[0].split(":")[1];
  const scopes = ["https://www.googleapis.com/auth/drive"];
  const client = await getClient(scopes);
  const service = google.drive({version: "v3", auth: client});
  const request = await service.files.create({
    media: {
      body: imgData,
      mimeType: mimeType
    },
    requestBody: resource,
    fields: "id,webViewLink,name,thumbnailLink,iconLink",
    supportsAllDrives: true
  });
  const file = request.data;
  return file;
}

云运行的日志是这样说的:

Default
2021-02-05 13:01:20.945 CSTevents.js:187
Default
2021-02-05 13:01:20.945 CST throw er; // Unhandled 'error' event
Default
2021-02-05 13:01:20.945 CST ^
Default
2021-02-05 13:01:20.945 CST
Default
2021-02-05 13:01:20.945 CSTTypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type number
Default
2021-02-05 13:01:20.945 CST at validChunk (_stream_writable.js:268:10)
Default
2021-02-05 13:01:20.945 CST at ProgressStream.Writable.write (_stream_writable.js:303:21)
Default
2021-02-05 13:01:20.945 CST at Readable.ondata (_stream_readable.js:727:22)
Default
2021-02-05 13:01:20.945 CST at Readable.emit (events.js:210:5)
Default
2021-02-05 13:01:20.945 CST at addChunk (_stream_readable.js:309:12)
Default
2021-02-05 13:01:20.945 CST at readableAddChunk (_stream_readable.js:290:11)
Default
2021-02-05 13:01:20.945 CST at Readable.push (_stream_readable.js:224:10)
Default
2021-02-05 13:01:20.945 CST at next (internal/streams/from.js:34:27)
Default
2021-02-05 13:01:20.945 CST at processTicksAndRejections (internal/process/task_queues.js:93:5)
Default
2021-02-05 13:01:20.945 CSTEmitted 'error' event on Readable instance at:
Default
2021-02-05 13:01:20.945 CST at emitErrorNT (internal/streams/destroy.js:92:8)
Default
2021-02-05 13:01:20.945 CST at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
Default
2021-02-05 13:01:20.945 CST at processTicksAndRejections (internal/process/task_queues.js:80:21) {
2021-02-05 13:01:20.945 CST code: 'ERR_INVALID_ARG_TYPE'
Default
2021-02-05 13:01:20.945 CST}
2021-02-05 13:01:20.959 CSTPOST503661 B53 msChrome 88 https://xxx-yyy-test-75i2ghnk3q-uc.a.run.app/images/upload
The request failed because either the HTTP response was malformed or connection to the instance had an error.
Default
2021-02-05 13:01:20.961 CSTnpm ERR! code ELIFECYCLE
Default
2021-02-05 13:01:20.961 CSTnpm ERR! errno 1
Default
2021-02-05 13:01:20.962 CSTnpm ERR! xxx-yyy@0.0.0 startapp: `node server/server.js`
Default
2021-02-05 13:01:20.962 CSTnpm ERR! Exit status 1
Default
2021-02-05 13:01:20.962 CSTnpm ERR!
Default
2021-02-05 13:01:20.963 CSTnpm ERR! Failed at the xxx-yyy@0.0.0 startapp script.
Default
2021-02-05 13:01:20.963 CSTnpm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Default
2021-02-05 13:01:22.078 CST
Default
2021-02-05 13:01:22.078 CST> xxx-yyy@0.0.0 startapp /usr/src/app
Default
2021-02-05 13:01:22.078 CST> node server/server.js
Default
2021-02-05 13:01:22.078 CST
Default
2021-02-05 13:01:23.240 CSTListening on port: 8080

我已遵循troubleshooting documentation 中的所有建议,但似乎没有任何提示。日志上没有更多内容,我已经查看了GCR limitations,似乎找不到任何问题。

【问题讨论】:

  • 你是在本地尝试容器还是只尝试代码?
  • @guillaumeblaquiere 只有代码。我想我需要在本地测试容器。
  • 是的,两个都试试。检查nodeJS运行时版本是否相同,以及安装的依赖。

标签: node.js google-drive-api google-cloud-run


【解决方案1】:

感谢@guillaumeblaquiere 的建议,我能够在本地对容器进行故障排除。我意识到由于某种原因,导致问题的线路是这一行:

const imgData = Readable.from(Buffer.from(base64String, "base64"));

那行代码在我的开发环境windows上运行,但在容器环境linux上不行。感谢this post 我将那行代码更改为:

const theBuff = Buffer.from(base64String, "base64");
const imgData = new PassThrough();
imgData.end(theBuff);

以上似乎与 linux 配合得很好,现在错误消失了。总之,这不是谷歌云运行问题,而是环境代码问题。我仍然必须调查错误的原因。如果有人知道,请提供一些启示。

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 2021-04-25
    • 2020-08-02
    • 2012-03-10
    • 1970-01-01
    • 2014-01-23
    • 2011-02-25
    • 2014-02-19
    • 2012-08-15
    相关资源
    最近更新 更多