| 1 |
<?php |
| 2 |
namespace Aws\S3; |
| 3 |
|
| 4 |
use Aws\Credentials\CredentialsInterface; |
| 5 |
use GuzzleHttp\Psr7\Uri; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class PostObject |
| 11 |
{ |
| 12 |
private $client; |
| 13 |
private $bucket; |
| 14 |
private $formAttributes; |
| 15 |
private $formInputs; |
| 16 |
private $jsonPolicy; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( |
| 31 |
S3ClientInterface $client, |
| 32 |
$bucket, |
| 33 |
array $formInputs, |
| 34 |
$jsonPolicy |
| 35 |
) { |
| 36 |
$this->client = $client; |
| 37 |
$this->bucket = $bucket; |
| 38 |
|
| 39 |
if (is_array($jsonPolicy)) { |
| 40 |
$jsonPolicy = json_encode($jsonPolicy); |
| 41 |
} |
| 42 |
|
| 43 |
$this->jsonPolicy = $jsonPolicy; |
| 44 |
$this->formAttributes = [ |
| 45 |
'action' => $this->generateUri(), |
| 46 |
'method' => 'POST', |
| 47 |
'enctype' => 'multipart/form-data' |
| 48 |
]; |
| 49 |
|
| 50 |
$this->formInputs = $formInputs + ['key' => '${filename}']; |
| 51 |
$credentials = $client->getCredentials()->wait(); |
| 52 |
$this->formInputs += $this->getPolicyAndSignature($credentials); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function getClient() |
| 61 |
{ |
| 62 |
return $this->client; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function getBucket() |
| 71 |
{ |
| 72 |
return $this->bucket; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function getFormAttributes() |
| 81 |
{ |
| 82 |
return $this->formAttributes; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function setFormAttribute($attribute, $value) |
| 92 |
{ |
| 93 |
$this->formAttributes[$attribute] = $value; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function getFormInputs() |
| 102 |
{ |
| 103 |
return $this->formInputs; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function setFormInput($field, $value) |
| 113 |
{ |
| 114 |
$this->formInputs[$field] = $value; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function getJsonPolicy() |
| 123 |
{ |
| 124 |
return $this->jsonPolicy; |
| 125 |
} |
| 126 |
|
| 127 |
private function generateUri() |
| 128 |
{ |
| 129 |
$uri = new Uri($this->client->getEndpoint()); |
| 130 |
|
| 131 |
if ($this->client->getConfig('use_path_style_endpoint') === true |
| 132 |
|| ($uri->getScheme() === 'https' |
| 133 |
&& strpos($this->bucket, '.') !== false) |
| 134 |
) { |
| 135 |
|
| 136 |
$uri = $uri->withPath("/{$this->bucket}"); |
| 137 |
} else { |
| 138 |
|
| 139 |
$uri = $uri->withHost($this->bucket . '.' . $uri->getHost()); |
| 140 |
} |
| 141 |
|
| 142 |
return (string) $uri; |
| 143 |
} |
| 144 |
|
| 145 |
protected function getPolicyAndSignature(CredentialsInterface $creds) |
| 146 |
{ |
| 147 |
$jsonPolicy64 = base64_encode($this->jsonPolicy); |
| 148 |
|
| 149 |
return [ |
| 150 |
'AWSAccessKeyId' => $creds->getAccessKeyId(), |
| 151 |
'policy' => $jsonPolicy64, |
| 152 |
'signature' => base64_encode(hash_hmac( |
| 153 |
'sha1', |
| 154 |
$jsonPolicy64, |
| 155 |
$creds->getSecretKey(), |
| 156 |
true |
| 157 |
)) |
| 158 |
]; |
| 159 |
} |
| 160 |
} |