首页 文章

Firebase存储和 Cloud 功能 - ECONNRESET

提问于
浏览
3

我开发了一个Firebase Cloud功能,可以处理上传图像的多个操作 . 我的代码基于this documentation articlethis Cloud Function example . 因此,它正在使用Google Cloud Storage package .

它几乎一直工作正常,但有时我在上传到存储或从存储中删除时出现此错误:

Error: read ECONNRESET
    at exports._errnoException (util.js:1026:11)
    at TLSWrap.onread (net.js:569:26)

我正在使用我的应用程序的默认存储桶,由 event.data.bucket 引用 .

如果您需要其他信息或代码段,请告诉我,即使我的代码非常接近我之前链接的Function示例 .

我找到this GitHub issue,但我检查过我每次都会回复一个承诺 . 例如,以下是触发错误的删除部分:

index.js

exports.exampleFunction = functions.storage.object().onChange(event => {
    return f_thumbnails.exampleFunction(event);
});

example_function.js

module.exports = exports = function (_admin, _config) {
    admin = _admin;
    config = _config;

    return {
        "exampleFunction": function (event) {
            return exampleFunction(event);
        }
    };
};

const exampleFunction = function (event) {
    const gcsSourceFilePath = event.data.name;
    const gcsSourceFilePathSplit = gcsSourceFilePath.split('/');
    const gcsBaseFolder = gcsSourceFilePathSplit.length > 0 ? gcsSourceFilePathSplit[0] : '';
    const gcsSourceFileName = gcsSourceFilePathSplit.pop();
    const gceSourceFileDir = gcsSourceFilePathSplit.join('/') + (gcsSourceFilePathSplit.length > 0 ? '/' : '');

    // Not an image
    if (!event.data.contentType.startsWith('image/')) {
        console.log('Not an image !');
        return;
    }

    // Thumbnail
    if (gcsSourceFileName.startsWith(config.IMAGES_THUMBNAIL_PREFIX)) {
        console.log('Thumbnail !');
        return;
    }

    const bucket = gcs.bucket(event.data.bucket);
    const gcsThumbnailFilePath = gceSourceFileDir + config.IMAGES_THUMBNAIL_PREFIX + gcsSourceFileName;


    // File deletion
    if (event.data.resourceState === 'not_exists') {
        console.log('Thumbnail deletion : ' + gcsThumbnailFilePath);
        return bucket.file(gcsThumbnailFilePath).delete().then(() => {
            console.log('Deleted thumbnail ' + gcsThumbnailFilePath);
        });
    }
    ...

1 回答

  • 3

    这似乎与 google-cloud-node 库对套接字的处理以及Cloud Functions环境中的默认套接字超时有关 .

    用户验证的一个解决方案是修改库调用 requests 的方式,不要通过指定 forever: false 来永久保持套接字打开,例如 .

    var request = require('request').defaults({
      timeout: 60000,
      gzip: true,
      forever: false,
      pool: {
        maxSockets: Infinity
      }
    });
    

    这在packages/common/src/utils.js中是硬编码的,因此您需要将修改后的库的副本提供给项目,而不是将其作为NPM依赖项包含在内 . 有关该问题的更多详细信息,请参阅related public issue以及使用patch applied指向fork的链接 .

相关问题