ProjectKey.js 638 B

1234567891011121314151617181920212223
  1. const path = require('node:path')
  2. //
  3. // The advice in http://docs.aws.amazon.com/AmazonS3/latest/dev/
  4. // request-rate-perf-considerations.html is to avoid sequential key prefixes,
  5. // so we reverse the project ID part of the key as they suggest.
  6. //
  7. function format(projectId) {
  8. const prefix = naiveReverse(pad(projectId))
  9. return path.join(prefix.slice(0, 3), prefix.slice(3, 6), prefix.slice(6))
  10. }
  11. function pad(number) {
  12. return (number || 0).toString().padStart(9, '0')
  13. }
  14. function naiveReverse(string) {
  15. return string.split('').reverse().join('')
  16. }
  17. exports.format = format
  18. exports.pad = pad
  19. exports.naiveReverse = naiveReverse