PostObjectV4.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Aws\S3;
  3. use Aws\Credentials\CredentialsInterface;
  4. use GuzzleHttp\Psr7\Uri;
  5. use Aws\Signature\SignatureTrait;
  6. use Aws\Signature\SignatureV4 as SignatureV4;
  7. use Aws\Api\TimestampShape as TimestampShape;
  8. /**
  9. * Encapsulates the logic for getting the data for an S3 object POST upload form
  10. *
  11. * @link http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
  12. * @link http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html
  13. */
  14. class PostObjectV4
  15. {
  16. use SignatureTrait;
  17. private $client;
  18. private $bucket;
  19. private $formAttributes;
  20. private $formInputs;
  21. /**
  22. * Constructs the PostObject.
  23. *
  24. * The options array accepts the following keys:
  25. * @link http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
  26. *
  27. * @param S3ClientInterface $client Client used with the POST object
  28. * @param string $bucket Bucket to use
  29. * @param array $formInputs Associative array of form input
  30. * fields.
  31. * @param array $options Policy condition options
  32. * @param mixed $expiration Upload expiration time value. By
  33. * default: 1 hour valid period.
  34. */
  35. public function __construct(
  36. S3ClientInterface $client,
  37. $bucket,
  38. array $formInputs,
  39. array $options = [],
  40. $expiration = '+1 hours'
  41. ) {
  42. $this->client = $client;
  43. $this->bucket = $bucket;
  44. // setup form attributes
  45. $this->formAttributes = [
  46. 'action' => $this->generateUri(),
  47. 'method' => 'POST',
  48. 'enctype' => 'multipart/form-data'
  49. ];
  50. $credentials = $this->client->getCredentials()->wait();
  51. if ($securityToken = $credentials->getSecurityToken()) {
  52. $options [] = ['x-amz-security-token' => $securityToken];
  53. $formInputs['X-Amz-Security-Token'] = $securityToken;
  54. }
  55. // setup basic policy
  56. $policy = [
  57. 'expiration' => TimestampShape::format($expiration, 'iso8601'),
  58. 'conditions' => $options,
  59. ];
  60. // setup basic formInputs
  61. $this->formInputs = $formInputs + ['key' => '${filename}'];
  62. // finalize policy and signature
  63. $this->formInputs += $this->getPolicyAndSignature(
  64. $credentials,
  65. $policy
  66. );
  67. }
  68. /**
  69. * Gets the S3 client.
  70. *
  71. * @return S3ClientInterface
  72. */
  73. public function getClient()
  74. {
  75. return $this->client;
  76. }
  77. /**
  78. * Gets the bucket name.
  79. *
  80. * @return string
  81. */
  82. public function getBucket()
  83. {
  84. return $this->bucket;
  85. }
  86. /**
  87. * Gets the form attributes as an array.
  88. *
  89. * @return array
  90. */
  91. public function getFormAttributes()
  92. {
  93. return $this->formAttributes;
  94. }
  95. /**
  96. * Set a form attribute.
  97. *
  98. * @param string $attribute Form attribute to set.
  99. * @param string $value Value to set.
  100. */
  101. public function setFormAttribute($attribute, $value)
  102. {
  103. $this->formAttributes[$attribute] = $value;
  104. }
  105. /**
  106. * Gets the form inputs as an array.
  107. *
  108. * @return array
  109. */
  110. public function getFormInputs()
  111. {
  112. return $this->formInputs;
  113. }
  114. /**
  115. * Set a form input.
  116. *
  117. * @param string $field Field name to set
  118. * @param string $value Value to set.
  119. */
  120. public function setFormInput($field, $value)
  121. {
  122. $this->formInputs[$field] = $value;
  123. }
  124. private function generateUri()
  125. {
  126. $uri = new Uri($this->client->getEndpoint());
  127. if ($this->client->getConfig('use_path_style_endpoint') === true
  128. || ($uri->getScheme() === 'https'
  129. && strpos($this->bucket, '.') !== false)
  130. ) {
  131. // Use path-style URLs
  132. $uri = $uri->withPath("/{$this->bucket}");
  133. } else {
  134. // Use virtual-style URLs if haven't been set up already
  135. if (strpos($uri->getHost(), $this->bucket . '.') !== 0) {
  136. $uri = $uri->withHost($this->bucket . '.' . $uri->getHost());
  137. }
  138. }
  139. return (string) $uri;
  140. }
  141. protected function getPolicyAndSignature(
  142. CredentialsInterface $credentials,
  143. array $policy
  144. ){
  145. $ldt = gmdate(SignatureV4::ISO8601_BASIC);
  146. $sdt = substr($ldt, 0, 8);
  147. $policy['conditions'][] = ['X-Amz-Date' => $ldt];
  148. $region = $this->client->getRegion();
  149. $scope = $this->createScope($sdt, $region, 's3');
  150. $creds = "{$credentials->getAccessKeyId()}/$scope";
  151. $policy['conditions'][] = ['X-Amz-Credential' => $creds];
  152. $policy['conditions'][] = ['X-Amz-Algorithm' => "AWS4-HMAC-SHA256"];
  153. $jsonPolicy64 = base64_encode(json_encode($policy));
  154. $key = $this->getSigningKey(
  155. $sdt,
  156. $region,
  157. 's3',
  158. $credentials->getSecretKey()
  159. );
  160. return [
  161. 'X-Amz-Credential' => $creds,
  162. 'X-Amz-Algorithm' => "AWS4-HMAC-SHA256",
  163. 'X-Amz-Date' => $ldt,
  164. 'Policy' => $jsonPolicy64,
  165. 'X-Amz-Signature' => bin2hex(
  166. hash_hmac('sha256', $jsonPolicy64, $key, true)
  167. ),
  168. ];
  169. }
  170. }