project_key.js 610 B

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