| 1 |
<?php |
| 2 |
namespace Aws\S3; |
| 3 |
|
| 4 |
use GuzzleHttp\Promise\PromiseInterface; |
| 5 |
use GuzzleHttp\Promise\PromisorInterface; |
| 6 |
use GuzzleHttp\Psr7; |
| 7 |
use GuzzleHttp\Psr7\FnStream; |
| 8 |
use Psr\Http\Message\StreamInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ObjectUploader implements PromisorInterface |
| 15 |
{ |
| 16 |
const DEFAULT_MULTIPART_THRESHOLD = 16777216; |
| 17 |
|
| 18 |
private $client; |
| 19 |
private $bucket; |
| 20 |
private $key; |
| 21 |
private $body; |
| 22 |
private $acl; |
| 23 |
private $options; |
| 24 |
private static $defaults = [ |
| 25 |
'before_upload' => null, |
| 26 |
'concurrency' => 3, |
| 27 |
'mup_threshold' => self::DEFAULT_MULTIPART_THRESHOLD, |
| 28 |
'params' => [], |
| 29 |
'part_size' => null, |
| 30 |
]; |
| 31 |
private $addContentMD5; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( |
| 51 |
S3ClientInterface $client, |
| 52 |
$bucket, |
| 53 |
$key, |
| 54 |
$body, |
| 55 |
$acl = 'private', |
| 56 |
array $options = [] |
| 57 |
) { |
| 58 |
$this->client = $client; |
| 59 |
$this->bucket = $bucket; |
| 60 |
$this->key = $key; |
| 61 |
$this->body = $this->createStream($body); |
| 62 |
$this->acl = $acl; |
| 63 |
$this->options = $options + self::$defaults; |
| 64 |
|
| 65 |
$this->addContentMD5 = isset($options['add_content_md5']) |
| 66 |
&& $options['add_content_md5'] === true; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function promise(): PromiseInterface |
| 73 |
{ |
| 74 |
|
| 75 |
$mup_threshold = $this->options['mup_threshold']; |
| 76 |
if ($this->requiresMultipart($this->body, $mup_threshold)) { |
| 77 |
|
| 78 |
return (new MultipartUploader($this->client, $this->body, [ |
| 79 |
'bucket' => $this->bucket, |
| 80 |
'key' => $this->key, |
| 81 |
'acl' => $this->acl |
| 82 |
] + $this->options))->promise(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
$command = $this->client->getCommand('PutObject', [ |
| 87 |
'Bucket' => $this->bucket, |
| 88 |
'Key' => $this->key, |
| 89 |
'Body' => $this->body, |
| 90 |
'ACL' => $this->acl, |
| 91 |
'AddContentMD5' => $this->addContentMD5 |
| 92 |
] + $this->options['params']); |
| 93 |
if (is_callable($this->options['before_upload'])) { |
| 94 |
$this->options['before_upload']($command); |
| 95 |
} |
| 96 |
return $this->client->executeAsync($command); |
| 97 |
} |
| 98 |
|
| 99 |
public function upload() |
| 100 |
{ |
| 101 |
return $this->promise()->wait(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function createStream($body): StreamInterface |
| 113 |
{ |
| 114 |
$stream = Psr7\Utils::streamFor($body); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if (is_resource($body)) { |
| 121 |
return FnStream::decorate($stream, [ |
| 122 |
'close' => function () use ($stream) { |
| 123 |
$stream->detach(); |
| 124 |
}, |
| 125 |
]); |
| 126 |
} |
| 127 |
|
| 128 |
return $stream; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private function requiresMultipart(StreamInterface &$body, $threshold) |
| 142 |
{ |
| 143 |
|
| 144 |
if ($body->getSize() !== null) { |
| 145 |
return $body->getSize() >= $threshold; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
$buffer = Psr7\Utils::streamFor(); |
| 154 |
Psr7\Utils::copyToStream($body, $buffer, MultipartUploader::PART_MIN_SIZE); |
| 155 |
|
| 156 |
|
| 157 |
if ($buffer->getSize() < MultipartUploader::PART_MIN_SIZE) { |
| 158 |
$buffer->seek(0); |
| 159 |
$body = $buffer; |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
if ($body->isSeekable() && $body->getMetadata('uri') !== 'php://input') { |
| 165 |
|
| 166 |
$body->seek(0); |
| 167 |
} else { |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
$buffer->seek(0); |
| 173 |
$body = new Psr7\AppendStream([$buffer, $body]); |
| 174 |
} |
| 175 |
|
| 176 |
return true; |
| 177 |
} |
| 178 |
} |