S3SignatureV4.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Aws\Signature;
  3. use Aws\Credentials\CredentialsInterface;
  4. use Psr\Http\Message\RequestInterface;
  5. /**
  6. * Amazon S3 signature version 4 support.
  7. */
  8. class S3SignatureV4 extends SignatureV4
  9. {
  10. /**
  11. * S3-specific signing logic
  12. *
  13. * @param RequestInterface $request
  14. * @param CredentialsInterface $credentials
  15. * @return \GuzzleHttp\Psr7\Request|RequestInterface
  16. */
  17. public function signRequest(
  18. RequestInterface $request,
  19. CredentialsInterface $credentials
  20. ) {
  21. // Always add a x-amz-content-sha-256 for data integrity
  22. if (!$request->hasHeader('x-amz-content-sha256')) {
  23. $request = $request->withHeader(
  24. 'x-amz-content-sha256',
  25. $this->getPayload($request)
  26. );
  27. }
  28. return parent::signRequest($request, $credentials);
  29. }
  30. /**
  31. * Always add a x-amz-content-sha-256 for data integrity.
  32. */
  33. public function presign(
  34. RequestInterface $request,
  35. CredentialsInterface $credentials,
  36. $expires,
  37. array $options = []
  38. ) {
  39. if (!$request->hasHeader('x-amz-content-sha256')) {
  40. $request = $request->withHeader(
  41. 'X-Amz-Content-Sha256',
  42. $this->getPresignedPayload($request)
  43. );
  44. }
  45. return parent::presign($request, $credentials, $expires, $options);
  46. }
  47. /**
  48. * Override used to allow pre-signed URLs to be created for an
  49. * in-determinate request payload.
  50. */
  51. protected function getPresignedPayload(RequestInterface $request)
  52. {
  53. return SignatureV4::UNSIGNED_PAYLOAD;
  54. }
  55. /**
  56. * Amazon S3 does not double-encode the path component in the canonical request
  57. */
  58. protected function createCanonicalizedPath($path)
  59. {
  60. // Only remove one slash in case of keys that have a preceding slash
  61. if (substr($path, 0, 1) === '/') {
  62. $path = substr($path, 1);
  63. }
  64. return '/' . $path;
  65. }
  66. }