【问题标题】:Google Cloud Storage (Js), Async/Await & Cloud Functions ErrorsGoogle Cloud Storage (Js)、Async/Await 和 Cloud Functions 错误
【发布时间】:2017-09-30 03:25:06
【问题描述】:

这已在 GOOGLE 端解决了

Google Cloud NodeJs 库现已修复集成。保留这个问题仅供参考。

原始问题

我希望我的代码更简洁,并使用 Typescript 和 async/await 编写访问 Google Cloud Storage 的 Cloud Functions。

tsconfig.json的一些重要部分:

{
  "compilerOptions": {
    "declaration": false,
    "target": "es6",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "noUnusedLocals": true,
    "moduleResolution": "node",
    "sourceMap": false,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016"
    ],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "outDir": "./"
  }
}

我遇到的第一个错误是ECONNRESET,我正在拨打这样的电话,这可能是导致它的原因:

await bucket.file(path).download({
  destination: tempFilePath
})

我认为函数的其余部分没有等到这一行完成并且整个函数执行结束之前文件从 GCS 下载到临时路径。 所以我把那个部分放到了 try/catch 块中:

  try { 
    await bucket.file(path).download({
      destination: tempFilePath
    })
  } catch (error) {
    console.log(error);
  }

直到今天它都运行良好。今天我遇到了这个错误:

convert: Empty input file '/temp/img-file.jpg'

这再次让我想到在文件从存储桶下载到临时文件夹之前执行下一行(转换图像大小)。

我错过了什么吗?

【问题讨论】:

  • 您确定该文件存在吗?
  • @m_callens 在通话之前添加console.log,很快就会通知您。
  • 每次打印正确的路径并且我已经进行了 6 次测试,现在都可以正常工作。部署功能后是否需要等待一段时间?我在部署后的一分钟内进行了测试并出现了错误(当时我出现了错误),现在似乎一切正常。
  • 今天我遇到了这个错误:code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' 我要回到 Promise-Land 并放弃 async/await

标签: javascript node.js asynchronous google-cloud-storage google-cloud-functions


【解决方案1】:

显然我犯了一些错误,因为仍然有很多图书馆没有正确的type definitions,或者根本没有,包括谷歌的,而且我猜错了。

小心使用没有Type Definitions 的库并打开它们的源代码以查看返回类型。

在搜索解决方案时,我也遇到了这个问题:GCS permissions related error and a bash script to fix。如果您阅读此答案,可能您的问题与此有关,所以我也在这里分享。

更新: 我又看到了一个错误,所以要确保我在try/catch 中包含了两个异步调用:

  let myBucket : any = {};

  try {
    myBucket = await googlecloudstorage.bucket('<bucket-id>');
    await myBucket.file(path).download({
      destination: tempFilePath
    })
  } catch (error) {
    console.log(error);
  }

更新 2: 仍然有错误,尝试在 Typescript 中解决它,但我计划回到纯 javascript。此外,我尝试使用批处理文件更新 ACL,如我在上面提供的链接中所解释的,似乎没有帮助。

更新 3(希望是最后一个): 好吧,对于与 Google Cloud Storage 相关的调用,我现在使用普通的 javascript 承诺并放弃 async/await,就像在 Firebase-Functions Samples repo 中使用的那样,到目前为止一切都很好。

更新 4: 在更改代码后,上传/读取不一致时,我不断收到错误。进一步搜索并...

从另一个描述相同问题的问题中阅读this answer。基本上不是我们,是他们,但他们不承认。这是因为google-cloud-node 包使用套接字的方式。 如果出现解决此问题的合理方法,将在此处发布。

更新 5:

以下是如何不让套接字永远打开:

var gcs = require('@google-cloud/storage')(...)
gcs.interceptors.push({
  request: function(reqOpts) {
    reqOpts.forever = false
    return reqOpts
  }
})

...目前看来可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2019-07-11
    • 2019-06-01
    • 2018-04-22
    • 1970-01-01
    • 2020-02-08
    • 2020-03-07
    相关资源
    最近更新 更多