2017 年 5 月 9 日更新
根据this thread,google cloud nodejs API 开发人员对初始化时使用的默认值进行了一些更改,这应该可以解决这些 ECONNRESET 套接字问题。
来自@stephen++ on github GoogleCloudPlatform/google-cloud-node issue 2254:
我们在 Cloud Function 中默认禁用了永久代理
环境。如果您卸载并重新安装 @google-cloud/storage,您
将自动选择新行为。感谢所有的
调试有用,大家好!
较早的帖子关注:
对于我在云功能平台上使用存储的类似ECONNRESET 问题的解决方案是使用npm:promise-retry,但是设置自己的重试策略,因为默认重试 10 次太多了。
我报告了一个ECONNRESET issue with cloud functions to Google Support(如果您在这种情况下也获得 ECONNRESET 而不是在其他情况下,您可能会加注星标),他们回答说“不会修复”该行为是预期的。谷歌支持表示 API 客户端库用于连接的套接字会在几分钟后超时,然后当您的云功能尝试再次使用它时,您将获得 ECONNRESET。他们建议在初始化存储 API 时添加autoRetry:true,但这并没有帮助。
ECONNRESET 也发生在读取端。在读取和写入的情况下promise-retry 都有帮助,而且大多数时候只需重试 1 次即可重置坏套接字。
所以我写了npm:pipe-to-storage 来返回一个承诺进行重试,检查 md5 等,但我没有用二进制数据测试它,只有文本,所以我不知道你是否可以使用它图像文件。调用如下所示:
const fs = require('fs');
const storage = require('@google-cloud/storage')();
const pipeToStorage = require('pipe-to-storage')(storage);
const source = ()=>(fs.createReadStream("/path/to/your/file/to/upload"));
pipeToStorage(source, bucketName, fileNameInBucket).then(//do next step);
另见How do I read the contents of a new cloud storage file of type .json from within a cloud function?