Skip to content

setChecksum* related methods for PostPolicy #1418

@qwwqe

Description

@qwwqe

I've noticed there aren't any checksum-related methods defined on PostPolicy, such as something comparable to the SetChecksum receiver implemented in the Go version of the minio SDK. Is this a deliberate design choice, or would a PR request adding such functionality be welcomed?

If the various algorithms were to be broken into distinct methods, the implementation might look something like this:

export class PostPolicy {
  ...

  setChecksumSHA256(checksum: string) {
    if (!checksum) {
      throw new Error('SHA256 checksum cannot be null')
    }

    this.policy.conditions.push(['eq', '$x-amz-checksum-algorithm', 'SHA256']);
    this.formData['x-amz-checksum-algorithm'] = 'SHA256';

    this.policy.conditions.push(['eq', '$x-amz-checksum-sha256', checksum]);
    policy.formData['x-amz-checksum-sha256'] = checksumSha256;
  }

  ...
}

Alternatively, a combined approach might look like this (or using native enum if that's preferred):

const ChecksumAlgorithmHeaders = {
  CRC32: 'x-amz-checksum-crc32',
  CRC32C: ' x-amz-checksum-crc32c',
  SHA1: 'x-amz-checksum-sha1',
  SHA256: 'x-amz-checksum-sha256',
}

export type ChecksumAlgorithm = keyof typeof ChecksumAlgorithmHeaders

export class PostPolicy {
  ...

  setChecksum(algorithm: ChecksumAlgorithm, checksum: string) {
    if (!ChecksumAlgorithmHeaders[algorithm]) {
      throw new Error(`Unknown checksum algorithm: ${algorithm}`) 
    }

    if (!checksum) {
      throw new Error('Checksum cannot be null')
    }

    this.policy.conditions.push(['eq', '$x-amz-checksum-algorithm', algorithm]);
    this.formData['x-amz-checksum-algorithm'] = algorithm;

    this.policy.conditions.push(['eq', `$${ChecksumAlgorithmHeaders[algorithm]}`, checksum]);
    this.formData[ChecksumAlgorithmHeaders[algorithm]] = checksum;
  }

  ...
}

Relevant AWS documentation can be found here and here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions