Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/ObjectUploader.php

raw symbols SCIP
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 * Uploads an object to S3, using a PutObject command or a multipart upload as
12 * appropriate.
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 * @param S3ClientInterface $client The S3 Client used to execute
35 * the upload command(s).
36 * @param string $bucket Bucket to upload the object, or
37 * an S3 access point ARN.
38 * @param string $key Key of the object.
39 * @param mixed $body Object data to upload. Can be a
40 * StreamInterface, PHP stream
41 * resource, or a string of data to
42 * upload.
43 * @param string $acl ACL to apply to the copy
44 * (default: private).
45 * @param array $options Options used to configure the
46 * copy process. Options passed in
47 * through 'params' are added to
48 * the sub command(s).
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 // Handle "add_content_md5" option.
65 $this->addContentMD5 = isset($options['add_content_md5'])
66 && $options['add_content_md5'] === true;
67 }
68
69 /**
70 * @return PromiseInterface
71 */
72 public function promise(): PromiseInterface
73 {
74 /** @var int $mup_threshold */
75 $mup_threshold = $this->options['mup_threshold'];
76 if ($this->requiresMultipart($this->body, $mup_threshold)) {
77 // Perform a multipart upload.
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 // Perform a regular PutObject operation.
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 * Creates a stream from the provided body, ensuring that user-provided
106 * PHP stream resources are not closed when the stream is destructed.
107 *
108 * @param mixed $body
109 *
110 * @return StreamInterface
111 */
112 private function createStream($body): StreamInterface
113 {
114 $stream = Psr7\Utils::streamFor($body);
115
116 // When the user provides a raw PHP resource, we should not take
117 // ownership of it (i.e., we should not fclose it on destruct).
118 // Decorate with a no-op close to prevent the underlying Stream
119 // from closing the user's resource handle.
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 * Determines if the body should be uploaded using PutObject or the
133 * Multipart Upload System. It also modifies the passed-in $body as needed
134 * to support the upload.
135 *
136 * @param StreamInterface $body Stream representing the body.
137 * @param integer $threshold Minimum bytes before using Multipart.
138 *
139 * @return bool
140 */
141 private function requiresMultipart(StreamInterface &$body, $threshold)
142 {
143 // If body size known, compare to threshold to determine if Multipart.
144 if ($body->getSize() !== null) {
145 return $body->getSize() >= $threshold;
146 }
147
148 /**
149 * Handle the situation where the body size is unknown.
150 * Read up to 5MB into a buffer to determine how to upload the body.
151 * @var StreamInterface $buffer
152 */
153 $buffer = Psr7\Utils::streamFor();
154 Psr7\Utils::copyToStream($body, $buffer, MultipartUploader::PART_MIN_SIZE);
155
156 // If body < 5MB, use PutObject with the buffer.
157 if ($buffer->getSize() < MultipartUploader::PART_MIN_SIZE) {
158 $buffer->seek(0);
159 $body = $buffer;
160 return false;
161 }
162
163 // If body >= 5 MB, then use multipart. [YES]
164 if ($body->isSeekable() && $body->getMetadata('uri') !== 'php://input') {
165 // If the body is seekable, just rewind the body.
166 $body->seek(0);
167 } else {
168 // If the body is non-seekable, stitch the rewind the buffer and
169 // the partially read body together into one stream. This avoids
170 // unnecessary disc usage and does not require seeking on the
171 // original stream.
172 $buffer->seek(0);
173 $body = new Psr7\AppendStream([$buffer, $body]);
174 }
175
176 return true;
177 }
178 }