S3UriParser.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Aws\S3;
  3. use GuzzleHttp\Psr7;
  4. use Psr\Http\Message\UriInterface;
  5. /**
  6. * Extracts a region, bucket, key, and and if a URI is in path-style
  7. */
  8. class S3UriParser
  9. {
  10. private $pattern = '/^(.+\\.)?s3[.-]([A-Za-z0-9-]+)\\./';
  11. private static $defaultResult = [
  12. 'path_style' => true,
  13. 'bucket' => null,
  14. 'key' => null,
  15. 'region' => null
  16. ];
  17. /**
  18. * Parses a URL into an associative array of Amazon S3 data including:
  19. *
  20. * - bucket: The Amazon S3 bucket (null if none)
  21. * - key: The Amazon S3 key (null if none)
  22. * - path_style: Set to true if using path style, or false if not
  23. * - region: Set to a string if a non-class endpoint is used or null.
  24. *
  25. * @param string|UriInterface $uri
  26. *
  27. * @return array
  28. * @throws \InvalidArgumentException
  29. */
  30. public function parse($uri)
  31. {
  32. $url = Psr7\uri_for($uri);
  33. if (!$url->getHost()) {
  34. throw new \InvalidArgumentException('No hostname found in URI: '
  35. . $uri);
  36. }
  37. if (!preg_match($this->pattern, $url->getHost(), $matches)) {
  38. return $this->parseCustomEndpoint($url);
  39. }
  40. // Parse the URI based on the matched format (path / virtual)
  41. $result = empty($matches[1])
  42. ? $this->parsePathStyle($url)
  43. : $this->parseVirtualHosted($url, $matches);
  44. // Add the region if one was found and not the classic endpoint
  45. $result['region'] = $matches[2] == 'amazonaws' ? null : $matches[2];
  46. return $result;
  47. }
  48. private function parseCustomEndpoint(UriInterface $url)
  49. {
  50. $result = $result = self::$defaultResult;
  51. $path = ltrim($url->getPath(), '/ ');
  52. $segments = explode('/', $path, 2);
  53. if (isset($segments[0])) {
  54. $result['bucket'] = $segments[0];
  55. if (isset($segments[1])) {
  56. $result['key'] = $segments[1];
  57. }
  58. }
  59. return $result;
  60. }
  61. private function parsePathStyle(UriInterface $url)
  62. {
  63. $result = self::$defaultResult;
  64. if ($url->getPath() != '/') {
  65. $path = ltrim($url->getPath(), '/');
  66. if ($path) {
  67. $pathPos = strpos($path, '/');
  68. if ($pathPos === false) {
  69. // https://s3.amazonaws.com/bucket
  70. $result['bucket'] = $path;
  71. } elseif ($pathPos == strlen($path) - 1) {
  72. // https://s3.amazonaws.com/bucket/
  73. $result['bucket'] = substr($path, 0, -1);
  74. } else {
  75. // https://s3.amazonaws.com/bucket/key
  76. $result['bucket'] = substr($path, 0, $pathPos);
  77. $result['key'] = substr($path, $pathPos + 1) ?: null;
  78. }
  79. }
  80. }
  81. return $result;
  82. }
  83. private function parseVirtualHosted(UriInterface $url, array $matches)
  84. {
  85. $result = self::$defaultResult;
  86. $result['path_style'] = false;
  87. // Remove trailing "." from the prefix to get the bucket
  88. $result['bucket'] = substr($matches[1], 0, -1);
  89. $path = $url->getPath();
  90. // Check if a key was present, and if so, removing the leading "/"
  91. $result['key'] = !$path || $path == '/' ? null : substr($path, 1);
  92. return $result;
  93. }
  94. }