【问题标题】:accurate method to compare the s3 object and local object比较s3对象和本地对象的准确方法
【发布时间】: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


【解决方案1】:

要将本地文件的内容与 Amazon S3 对象进行比较,请使用 ETag,它是内容的校验和。 ETag 在检索有关 S3 对象的信息时可用。

见:All about AWS S3 ETags - Teppen.io

另外,请注意,通过分段上传上传的对象的计算稍微复杂一些。见:What is the algorithm to compute the Amazon-S3 Etag for a file larger than 5GB?

【讨论】:

  • John - 对于内容哈希 (md5),etag 不再可靠。 stackoverflow.com/a/53886736/8016720
  • @JohnHanley 我可能完全错了。但我认为 OP 正在谈论元数据。 The ETag reflects changes only to the contents of an object, not its metadata.
猜你喜欢
  • 2016-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
相关资源
最近更新 更多