S3SignatureV4.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * Always add a x-amz-content-sha-256 for data integrity.
  12. */
  13. public function signRequest(
  14. RequestInterface $request,
  15. CredentialsInterface $credentials
  16. ) {
  17. if (!$request->hasHeader('x-amz-content-sha256')) {
  18. $request = $request->withHeader(
  19. 'X-Amz-Content-Sha256',
  20. $this->getPayload($request)
  21. );
  22. }
  23. return parent::signRequest($request, $credentials);
  24. }
  25. /**
  26. * Always add a x-amz-content-sha-256 for data integrity.
  27. */
  28. public function presign(
  29. RequestInterface $request,
  30. CredentialsInterface $credentials,
  31. $expires,
  32. array $options = []
  33. ) {
  34. if (!$request->hasHeader('x-amz-content-sha256')) {
  35. $request = $request->withHeader(
  36. 'X-Amz-Content-Sha256',
  37. $this->getPresignedPayload($request)
  38. );
  39. }
  40. return parent::presign($request, $credentials, $expires, $options);
  41. }
  42. /**
  43. * Override used to allow pre-signed URLs to be created for an
  44. * in-determinate request payload.
  45. */
  46. protected function getPresignedPayload(RequestInterface $request)
  47. {
  48. return SignatureV4::UNSIGNED_PAYLOAD;
  49. }
  50. /**
  51. * Amazon S3 does not double-encode the path component in the canonical request
  52. */
  53. protected function createCanonicalizedPath($path)
  54. {
  55. // Only remove one slash in case of keys that have a preceding slash
  56. if (substr($path, 0, 1) === '/') {
  57. $path = substr($path, 1);
  58. }
  59. return '/' . $path;
  60. }
  61. }