async bestGuessMD5()

in source/checksum/lib/validation/index.js [219:260]


  async bestGuessMD5() {
    /* try tag first */
    const chksum = this.tags.find(x => x.Key === this.getChecksumTagName());
    if (chksum && chksum.Value.match(/^([0-9a-fA-F]{32})$/)) {
      this.comparedWith = 'object-tagging';
      return chksum.Value;
    }

    const params = {
      Bucket: this.bucket,
      Key: this.key,
    };
    const s3 = new AWS.S3({
      apiVersion: '2006-03-01',
      signatureVersion: 'v4',
      customUserAgent: process.env.ENV_CUSTOM_USER_AGENT,
    });

    const {
      ETag,
      ServerSideEncryption,
      Metadata = {},
    } = await s3.headObject(params).promise();

    /* try x-amz-meta-md5 metadata */
    if (Metadata.md5) {
      this.comparedWith = 'object-metadata';
      return Metadata.md5;
    }

    /* Last resort */
    /* use ETag if and only if it is NOT multipart-upload and SSE is either disabled or AES256 */
    if ((!ServerSideEncryption || ServerSideEncryption.toLowerCase() === 'aes256') && ETag) {
      /* the regex screens any multipart upload ETag */
      const matched = ETag.match(/^"([0-9a-fA-F]{32})"$/);
      if (matched) {
        this.comparedWith = 'object-etag';
        return matched[1];
      }
    }
    return undefined;
  }