我正在使用 CfS 将一些图像上传到 Amazon S3。因此我有两个商店:原始文件和缩略图。对于原始文件,我添加了图像的尺寸,并将缩略图调整为 96 像素的正方形图像。这运作良好。
var original = new FS.Store.S3("original", {
accessKeyId: "XXX",
secretAccessKey: "XXX",
bucket: "XXX",
folder: "original",
transformWrite: function (fileObj, readStream, writeStream) {
readStream.pipe(writeStream);
var transformer = gm(readStream, fileObj.name());
transformer.size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
if (!err) fileObj.update({$set: {'metadata.orgWidth': size.width, 'metadata.orgHeight': size.height}});
}));
}
});
var thumbnail = new FS.Store.S3("thumbnail", {
accessKeyId: "XXX",
secretAccessKey: "XXX",
bucket: "XXX",
folder: "thumbnail",
// Create squared thumbnail
transformWrite: function (fileObj, readStream, writeStream) {
var size = '96';
gm(readStream, fileObj.name()).autoOrient().resize(size, size + '^').gravity('Center').extent(size, size).stream().pipe(writeStream);
}
});
Images = new FS.Collection("images", {
stores: [ original, thumbnail ]
});
现在我需要另外两个商店来存储同一个图像:一个“工作”图像和一个“公共”图像。工作图像用于图像处理/编辑,如像素化、去饱和等(您始终可以通过复制原始存储文件进行重置)和公共图像,它将使用工作图像,但如果是,则将其调整为 900px比这更广泛。
用户只会看到公共图像,编辑器正在使用工作图像。这就是我想做的。
所以我添加了这两个商店:
var working = new FS.Store.S3("main", {
accessKeyId: "XXX",
secretAccessKey: "XXX",
bucket: "XXX",
folder: "working",
transformWrite: function (fileObj, readStream, writeStream) {
readStream.pipe(writeStream);
var transformer = gm(readStream, fileObj.name());
transformer.size({bufferStream: true}, FS.Utility.safeCallback(function (err, size) {
if (!err) fileObj.update({$set: {'metadata.width': size.width, 'metadata.height': size.height}});
}));
}
});
var public = new FS.Store.S3("public", {
accessKeyId: "XXX",
secretAccessKey: "XXX",
bucket: "XXX",
folder: "public",
// Do resize to 900px, if image is larger then this
transformWrite: function (fileObj, readStream, writeStream) {
var transformer = gm(readStream, fileObj.name());
transformer.size({bufferStream: true}, FS.Utility.safeCallback(function (error, size) {
if (error) console.warn(error);
else {
if(size.width > 900) transformer.resize('900').stream().pipe(writeStream);
else transformer.stream().pipe(writeStream);
}
}));
}
});
Images = new FS.Collection("images", {
stores: [ original, working, public, thumbnail ]
});
基本上工作商店与原始商店相同 - 因为上传两者是相同的。公共存储应始终是工作图像的副本,最大宽度为 900 像素。
但是如果我添加这两个商店,有时上传和服务器会崩溃。在日志中我发现了错误
/docker/xxx/bundle/programs/server/npm/cfs_gridfs/node_modules/mongodb/lib/mongodb/connection/base.js:246
throw message;
^
Error: 56e715494166660700edb9c3 does not exist
我只是很惊讶为什么只有在我添加工作和公共商店时才会发生这种情况。那么这有什么问题呢?