【发布时间】:2018-12-28 00:59:21
【问题描述】:
我将修改的时间(以毫秒为单位)和文件大小保存到 s3 中对象的元数据中。我意识到即使我没有为我的文件更改任何内容,如果我打开它,只需保存文件而不进行编辑。修改的时间将被更改,在这种情况下它将更新 s3 对象。我想过使用尺寸,但尺寸也不会那么准确,因为即使修改后尺寸也有可能相同。我还使用了从s3.getObject 和本地文件Binary 返回的Binary,但没有任何更改。 Binary 也不一样。有什么更好更准确的方法来跟踪更改?
我的代码中有这样的东西,可以保存文件修改后的 ms 和文件大小
fs.readFile(path, async (err, fileBinary) => {
if (err) throw err;
const s3 = new AWS.S3();
const Key = path.replace(process.env.WATCH_PATH, '');
const filename = Key.split('/').pop();
// if filename is within the regex, ignore the file. Do nothing.
if (new RegExp(IGNORE_FILES_TO_S3()).test(filename)) return false;
const getStat = await getFileStat(path);
// console.log(getStat, 'getstatsssssssssssssss');
const s3PutParams = {
Body: fileBinary,
Bucket: process.env.S3_BUCKET,
Key,
Metadata: { // thought of saving these two as comparison in future usage, which works but really really accurate though
mtimeMs: String(getStat.mtimeMs),
size: String(getStat.size)
}
};
// rest of the code here just do comparisons and decide if `s3.putOjbect` should be done or not.
});
我的getFileStat()
exports.getFileStat = (path) => {
/*
SAMPLE: success
{
dev: 2097,
mode: 33204,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 5639856,
size: 2,
blocks: 8,
atimeMs: 1545952029779.866,
mtimeMs: 1545952020431.9802,
ctimeMs: 1545952020439.98,
birthtimeMs: 1545952020439.98,
atime: 2018-12-27T23:07:09.780Z,
mtime: 2018-12-27T23:07:00.432Z,
ctime: 2018-12-27T23:07:00.440Z,
birthtime: 2018-12-27T23:07:00.440Z
}
*/
return new Promise((res, rej) => {
fs.stat(path, (err, stat) => {
if (err) rej(err);
res(stat);
});
});
};
提前感谢您的任何建议和帮助。
PS。这不会将任何内容保存到数据库中,因此根本不会保存任何信息,以防万一有将某些内容保存到数据库中以进行比较的想法
【问题讨论】:
-
我对您希望最终结果是什么以及您的问题到底是什么感到有点困惑。
-
@CharlieFish sryz 在拥有
const s3PutParams之后我没有发布其余代码,其余的只是使用正确的比较来比较对象然后决定是否使用putObject跨度>
标签: javascript object amazon-s3 compare