Firebase 存储和云功能 - ECONNRESET
Firebase Storage & Cloud Functions - ECONNRESET
我开发了一个 Firebase Cloud 函数,可以对上传的图像进行多种操作。
我的代码基于 this documentation article and this Cloud Function example. Hence, it is using Google Cloud Storage package.
它几乎一直工作正常,但有时我在上传到存储或从存储中删除时遇到此错误:
Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
我正在使用我的应用程序的默认存储桶,由 event.data.bucket
引用。
如果您需要其他信息或代码片段,请告诉我,即使我的代码非常接近我之前链接的函数示例。
我找到了 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);
});
}
...
这似乎与 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, so you'll need to vendor a copy of the modified library into your project rather than include it as an NPM dependency. See the related public issue for more details on the issue and a link to a fork with the patch applied 中硬编码。
我开发了一个 Firebase Cloud 函数,可以对上传的图像进行多种操作。 我的代码基于 this documentation article and this Cloud Function example. Hence, it is using Google Cloud Storage package.
它几乎一直工作正常,但有时我在上传到存储或从存储中删除时遇到此错误:
Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
我正在使用我的应用程序的默认存储桶,由 event.data.bucket
引用。
如果您需要其他信息或代码片段,请告诉我,即使我的代码非常接近我之前链接的函数示例。
我找到了 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);
});
}
...
这似乎与 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, so you'll need to vendor a copy of the modified library into your project rather than include it as an NPM dependency. See the related public issue for more details on the issue and a link to a fork with the patch applied 中硬编码。