PutObjectUrlMiddleware.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\CommandInterface;
  4. use Aws\ResultInterface;
  5. use Psr\Http\Message\RequestInterface;
  6. /**
  7. * Injects ObjectURL into the result of the PutObject operation.
  8. *
  9. * @internal
  10. */
  11. class PutObjectUrlMiddleware
  12. {
  13. /** @var callable */
  14. private $nextHandler;
  15. /**
  16. * Create a middleware wrapper function.
  17. *
  18. * @return callable
  19. */
  20. public static function wrap()
  21. {
  22. return function (callable $handler) {
  23. return new self($handler);
  24. };
  25. }
  26. /**
  27. * @param callable $nextHandler Next handler to invoke.
  28. */
  29. public function __construct(callable $nextHandler)
  30. {
  31. $this->nextHandler = $nextHandler;
  32. }
  33. public function __invoke(CommandInterface $command, RequestInterface $request = null)
  34. {
  35. $next = $this->nextHandler;
  36. return $next($command, $request)->then(
  37. function (ResultInterface $result) use ($command) {
  38. $name = $command->getName();
  39. switch ($name) {
  40. case 'PutObject':
  41. case 'CopyObject':
  42. $result['ObjectURL'] = $result['@metadata']['effectiveUri'];
  43. break;
  44. case 'CompleteMultipartUpload':
  45. $result['ObjectURL'] = $result['Location'];
  46. break;
  47. }
  48. return $result;
  49. }
  50. );
  51. }
  52. }