@@ -35,93 +35,93 @@ |
||
35 | 35 | const S3_UPLOAD_PART_SIZE = 524288000; // 500MB |
36 | 36 | |
37 | 37 | trait S3ObjectTrait { |
38 | - /** |
|
39 | - * Returns the connection |
|
40 | - * |
|
41 | - * @return S3Client connected client |
|
42 | - * @throws \Exception if connection could not be made |
|
43 | - */ |
|
44 | - abstract protected function getConnection(); |
|
38 | + /** |
|
39 | + * Returns the connection |
|
40 | + * |
|
41 | + * @return S3Client connected client |
|
42 | + * @throws \Exception if connection could not be made |
|
43 | + */ |
|
44 | + abstract protected function getConnection(); |
|
45 | 45 | |
46 | - /** |
|
47 | - * @param string $urn the unified resource name used to identify the object |
|
48 | - * @return resource stream with the read data |
|
49 | - * @throws \Exception when something goes wrong, message will be logged |
|
50 | - * @since 7.0.0 |
|
51 | - */ |
|
52 | - function readObject($urn) { |
|
53 | - return SeekableHttpStream::open(function ($range) use ($urn) { |
|
54 | - $command = $this->getConnection()->getCommand('GetObject', [ |
|
55 | - 'Bucket' => $this->bucket, |
|
56 | - 'Key' => $urn, |
|
57 | - 'Range' => 'bytes=' . $range, |
|
58 | - ]); |
|
59 | - $request = \Aws\serialize($command); |
|
60 | - $headers = []; |
|
61 | - foreach ($request->getHeaders() as $key => $values) { |
|
62 | - foreach ($values as $value) { |
|
63 | - $headers[] = "$key: $value"; |
|
64 | - } |
|
65 | - } |
|
66 | - $opts = [ |
|
67 | - 'http' => [ |
|
68 | - 'protocol_version' => 1.1, |
|
69 | - 'header' => $headers, |
|
70 | - ], |
|
71 | - ]; |
|
46 | + /** |
|
47 | + * @param string $urn the unified resource name used to identify the object |
|
48 | + * @return resource stream with the read data |
|
49 | + * @throws \Exception when something goes wrong, message will be logged |
|
50 | + * @since 7.0.0 |
|
51 | + */ |
|
52 | + function readObject($urn) { |
|
53 | + return SeekableHttpStream::open(function ($range) use ($urn) { |
|
54 | + $command = $this->getConnection()->getCommand('GetObject', [ |
|
55 | + 'Bucket' => $this->bucket, |
|
56 | + 'Key' => $urn, |
|
57 | + 'Range' => 'bytes=' . $range, |
|
58 | + ]); |
|
59 | + $request = \Aws\serialize($command); |
|
60 | + $headers = []; |
|
61 | + foreach ($request->getHeaders() as $key => $values) { |
|
62 | + foreach ($values as $value) { |
|
63 | + $headers[] = "$key: $value"; |
|
64 | + } |
|
65 | + } |
|
66 | + $opts = [ |
|
67 | + 'http' => [ |
|
68 | + 'protocol_version' => 1.1, |
|
69 | + 'header' => $headers, |
|
70 | + ], |
|
71 | + ]; |
|
72 | 72 | |
73 | - $context = stream_context_create($opts); |
|
74 | - return fopen($request->getUri(), 'r', false, $context); |
|
75 | - }); |
|
76 | - } |
|
73 | + $context = stream_context_create($opts); |
|
74 | + return fopen($request->getUri(), 'r', false, $context); |
|
75 | + }); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $urn the unified resource name used to identify the object |
|
80 | - * @param resource $stream stream with the data to write |
|
81 | - * @throws \Exception when something goes wrong, message will be logged |
|
82 | - * @since 7.0.0 |
|
83 | - */ |
|
84 | - function writeObject($urn, $stream) { |
|
85 | - $count = 0; |
|
86 | - $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) { |
|
87 | - $count += $read; |
|
88 | - }); |
|
78 | + /** |
|
79 | + * @param string $urn the unified resource name used to identify the object |
|
80 | + * @param resource $stream stream with the data to write |
|
81 | + * @throws \Exception when something goes wrong, message will be logged |
|
82 | + * @since 7.0.0 |
|
83 | + */ |
|
84 | + function writeObject($urn, $stream) { |
|
85 | + $count = 0; |
|
86 | + $countStream = CallbackWrapper::wrap($stream, function ($read) use (&$count) { |
|
87 | + $count += $read; |
|
88 | + }); |
|
89 | 89 | |
90 | - $uploader = new MultipartUploader($this->getConnection(), $countStream, [ |
|
91 | - 'bucket' => $this->bucket, |
|
92 | - 'key' => $urn, |
|
93 | - 'part_size' => S3_UPLOAD_PART_SIZE, |
|
94 | - ]); |
|
90 | + $uploader = new MultipartUploader($this->getConnection(), $countStream, [ |
|
91 | + 'bucket' => $this->bucket, |
|
92 | + 'key' => $urn, |
|
93 | + 'part_size' => S3_UPLOAD_PART_SIZE, |
|
94 | + ]); |
|
95 | 95 | |
96 | - try { |
|
97 | - $uploader->upload(); |
|
98 | - } catch (S3MultipartUploadException $e) { |
|
99 | - // This is an empty file so just touch it then |
|
100 | - if ($count === 0 && feof($countStream)) { |
|
101 | - $uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, ''); |
|
102 | - $uploader->upload(); |
|
103 | - } else { |
|
104 | - throw $e; |
|
105 | - } |
|
106 | - } |
|
96 | + try { |
|
97 | + $uploader->upload(); |
|
98 | + } catch (S3MultipartUploadException $e) { |
|
99 | + // This is an empty file so just touch it then |
|
100 | + if ($count === 0 && feof($countStream)) { |
|
101 | + $uploader = new ObjectUploader($this->getConnection(), $this->bucket, $urn, ''); |
|
102 | + $uploader->upload(); |
|
103 | + } else { |
|
104 | + throw $e; |
|
105 | + } |
|
106 | + } |
|
107 | 107 | |
108 | - fclose($countStream); |
|
109 | - } |
|
108 | + fclose($countStream); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * @param string $urn the unified resource name used to identify the object |
|
113 | - * @return void |
|
114 | - * @throws \Exception when something goes wrong, message will be logged |
|
115 | - * @since 7.0.0 |
|
116 | - */ |
|
117 | - function deleteObject($urn) { |
|
118 | - $this->getConnection()->deleteObject([ |
|
119 | - 'Bucket' => $this->bucket, |
|
120 | - 'Key' => $urn, |
|
121 | - ]); |
|
122 | - } |
|
111 | + /** |
|
112 | + * @param string $urn the unified resource name used to identify the object |
|
113 | + * @return void |
|
114 | + * @throws \Exception when something goes wrong, message will be logged |
|
115 | + * @since 7.0.0 |
|
116 | + */ |
|
117 | + function deleteObject($urn) { |
|
118 | + $this->getConnection()->deleteObject([ |
|
119 | + 'Bucket' => $this->bucket, |
|
120 | + 'Key' => $urn, |
|
121 | + ]); |
|
122 | + } |
|
123 | 123 | |
124 | - public function objectExists($urn) { |
|
125 | - return $this->getConnection()->doesObjectExist($this->bucket, $urn); |
|
126 | - } |
|
124 | + public function objectExists($urn) { |
|
125 | + return $this->getConnection()->doesObjectExist($this->bucket, $urn); |
|
126 | + } |
|
127 | 127 | } |
@@ -28,155 +28,155 @@ |
||
28 | 28 | * A stream wrapper that uses http range requests to provide a seekable stream for http reading |
29 | 29 | */ |
30 | 30 | class SeekableHttpStream implements File { |
31 | - private const PROTOCOL = 'httpseek'; |
|
32 | - |
|
33 | - private static $registered = false; |
|
34 | - |
|
35 | - /** |
|
36 | - * Registers the stream wrapper using the `httpseek://` url scheme |
|
37 | - * $return void |
|
38 | - */ |
|
39 | - private static function registerIfNeeded() { |
|
40 | - if (!self::$registered) { |
|
41 | - stream_wrapper_register( |
|
42 | - self::PROTOCOL, |
|
43 | - self::class |
|
44 | - ); |
|
45 | - self::$registered = true; |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Open a readonly-seekable http stream |
|
51 | - * |
|
52 | - * The provided callback will be called with byte range and should return an http stream for the requested range |
|
53 | - * |
|
54 | - * @param callable $callback |
|
55 | - * @return false|resource |
|
56 | - */ |
|
57 | - public static function open(callable $callback) { |
|
58 | - $context = stream_context_create([ |
|
59 | - SeekableHttpStream::PROTOCOL => [ |
|
60 | - 'callback' => $callback |
|
61 | - ], |
|
62 | - ]); |
|
63 | - |
|
64 | - SeekableHttpStream::registerIfNeeded(); |
|
65 | - return fopen(SeekableHttpStream::PROTOCOL . '://', 'r', false, $context); |
|
66 | - } |
|
67 | - |
|
68 | - /** @var resource */ |
|
69 | - public $context; |
|
70 | - |
|
71 | - /** @var callable */ |
|
72 | - private $openCallback; |
|
73 | - |
|
74 | - /** @var resource */ |
|
75 | - private $current; |
|
76 | - /** @var int */ |
|
77 | - private $offset = 0; |
|
78 | - |
|
79 | - private function reconnect(int $start) { |
|
80 | - $range = $start . '-'; |
|
81 | - if ($this->current != null) { |
|
82 | - fclose($this->current); |
|
83 | - } |
|
84 | - |
|
85 | - $this->current = ($this->openCallback)($range); |
|
86 | - |
|
87 | - if ($this->current === false) { |
|
88 | - return false; |
|
89 | - } |
|
90 | - |
|
91 | - $responseHead = stream_get_meta_data($this->current)['wrapper_data']; |
|
92 | - $rangeHeaders = array_values(array_filter($responseHead, function ($v) { |
|
93 | - return preg_match('#^content-range:#i', $v) === 1; |
|
94 | - })); |
|
95 | - if (!$rangeHeaders) { |
|
96 | - return false; |
|
97 | - } |
|
98 | - $contentRange = $rangeHeaders[0]; |
|
99 | - |
|
100 | - $content = trim(explode(':', $contentRange)[1]); |
|
101 | - $range = trim(explode(' ', $content)[1]); |
|
102 | - $begin = intval(explode('-', $range)[0]); |
|
103 | - |
|
104 | - if ($begin !== $start) { |
|
105 | - return false; |
|
106 | - } |
|
107 | - |
|
108 | - $this->offset = $begin; |
|
109 | - |
|
110 | - return true; |
|
111 | - } |
|
112 | - |
|
113 | - public function stream_open($path, $mode, $options, &$opened_path) { |
|
114 | - $options = stream_context_get_options($this->context)[self::PROTOCOL]; |
|
115 | - $this->openCallback = $options['callback']; |
|
116 | - |
|
117 | - return $this->reconnect(0); |
|
118 | - } |
|
119 | - |
|
120 | - public function stream_read($count) { |
|
121 | - if (!$this->current) { |
|
122 | - return false; |
|
123 | - } |
|
124 | - $ret = fread($this->current, $count); |
|
125 | - $this->offset += strlen($ret); |
|
126 | - return $ret; |
|
127 | - } |
|
128 | - |
|
129 | - public function stream_seek($offset, $whence = SEEK_SET) { |
|
130 | - switch ($whence) { |
|
131 | - case SEEK_SET: |
|
132 | - if ($offset === $this->offset) { |
|
133 | - return true; |
|
134 | - } |
|
135 | - return $this->reconnect($offset); |
|
136 | - case SEEK_CUR: |
|
137 | - if ($offset === 0) { |
|
138 | - return true; |
|
139 | - } |
|
140 | - return $this->reconnect($this->offset + $offset); |
|
141 | - case SEEK_END: |
|
142 | - return false; |
|
143 | - } |
|
144 | - return false; |
|
145 | - } |
|
146 | - |
|
147 | - public function stream_tell() { |
|
148 | - return $this->offset; |
|
149 | - } |
|
150 | - |
|
151 | - public function stream_stat() { |
|
152 | - return fstat($this->current); |
|
153 | - } |
|
154 | - |
|
155 | - public function stream_eof() { |
|
156 | - return feof($this->current); |
|
157 | - } |
|
158 | - |
|
159 | - public function stream_close() { |
|
160 | - fclose($this->current); |
|
161 | - } |
|
162 | - |
|
163 | - public function stream_write($data) { |
|
164 | - return false; |
|
165 | - } |
|
166 | - |
|
167 | - public function stream_set_option($option, $arg1, $arg2) { |
|
168 | - return false; |
|
169 | - } |
|
170 | - |
|
171 | - public function stream_truncate($size) { |
|
172 | - return false; |
|
173 | - } |
|
174 | - |
|
175 | - public function stream_lock($operation) { |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - public function stream_flush() { |
|
180 | - return; //noop because readonly stream |
|
181 | - } |
|
31 | + private const PROTOCOL = 'httpseek'; |
|
32 | + |
|
33 | + private static $registered = false; |
|
34 | + |
|
35 | + /** |
|
36 | + * Registers the stream wrapper using the `httpseek://` url scheme |
|
37 | + * $return void |
|
38 | + */ |
|
39 | + private static function registerIfNeeded() { |
|
40 | + if (!self::$registered) { |
|
41 | + stream_wrapper_register( |
|
42 | + self::PROTOCOL, |
|
43 | + self::class |
|
44 | + ); |
|
45 | + self::$registered = true; |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Open a readonly-seekable http stream |
|
51 | + * |
|
52 | + * The provided callback will be called with byte range and should return an http stream for the requested range |
|
53 | + * |
|
54 | + * @param callable $callback |
|
55 | + * @return false|resource |
|
56 | + */ |
|
57 | + public static function open(callable $callback) { |
|
58 | + $context = stream_context_create([ |
|
59 | + SeekableHttpStream::PROTOCOL => [ |
|
60 | + 'callback' => $callback |
|
61 | + ], |
|
62 | + ]); |
|
63 | + |
|
64 | + SeekableHttpStream::registerIfNeeded(); |
|
65 | + return fopen(SeekableHttpStream::PROTOCOL . '://', 'r', false, $context); |
|
66 | + } |
|
67 | + |
|
68 | + /** @var resource */ |
|
69 | + public $context; |
|
70 | + |
|
71 | + /** @var callable */ |
|
72 | + private $openCallback; |
|
73 | + |
|
74 | + /** @var resource */ |
|
75 | + private $current; |
|
76 | + /** @var int */ |
|
77 | + private $offset = 0; |
|
78 | + |
|
79 | + private function reconnect(int $start) { |
|
80 | + $range = $start . '-'; |
|
81 | + if ($this->current != null) { |
|
82 | + fclose($this->current); |
|
83 | + } |
|
84 | + |
|
85 | + $this->current = ($this->openCallback)($range); |
|
86 | + |
|
87 | + if ($this->current === false) { |
|
88 | + return false; |
|
89 | + } |
|
90 | + |
|
91 | + $responseHead = stream_get_meta_data($this->current)['wrapper_data']; |
|
92 | + $rangeHeaders = array_values(array_filter($responseHead, function ($v) { |
|
93 | + return preg_match('#^content-range:#i', $v) === 1; |
|
94 | + })); |
|
95 | + if (!$rangeHeaders) { |
|
96 | + return false; |
|
97 | + } |
|
98 | + $contentRange = $rangeHeaders[0]; |
|
99 | + |
|
100 | + $content = trim(explode(':', $contentRange)[1]); |
|
101 | + $range = trim(explode(' ', $content)[1]); |
|
102 | + $begin = intval(explode('-', $range)[0]); |
|
103 | + |
|
104 | + if ($begin !== $start) { |
|
105 | + return false; |
|
106 | + } |
|
107 | + |
|
108 | + $this->offset = $begin; |
|
109 | + |
|
110 | + return true; |
|
111 | + } |
|
112 | + |
|
113 | + public function stream_open($path, $mode, $options, &$opened_path) { |
|
114 | + $options = stream_context_get_options($this->context)[self::PROTOCOL]; |
|
115 | + $this->openCallback = $options['callback']; |
|
116 | + |
|
117 | + return $this->reconnect(0); |
|
118 | + } |
|
119 | + |
|
120 | + public function stream_read($count) { |
|
121 | + if (!$this->current) { |
|
122 | + return false; |
|
123 | + } |
|
124 | + $ret = fread($this->current, $count); |
|
125 | + $this->offset += strlen($ret); |
|
126 | + return $ret; |
|
127 | + } |
|
128 | + |
|
129 | + public function stream_seek($offset, $whence = SEEK_SET) { |
|
130 | + switch ($whence) { |
|
131 | + case SEEK_SET: |
|
132 | + if ($offset === $this->offset) { |
|
133 | + return true; |
|
134 | + } |
|
135 | + return $this->reconnect($offset); |
|
136 | + case SEEK_CUR: |
|
137 | + if ($offset === 0) { |
|
138 | + return true; |
|
139 | + } |
|
140 | + return $this->reconnect($this->offset + $offset); |
|
141 | + case SEEK_END: |
|
142 | + return false; |
|
143 | + } |
|
144 | + return false; |
|
145 | + } |
|
146 | + |
|
147 | + public function stream_tell() { |
|
148 | + return $this->offset; |
|
149 | + } |
|
150 | + |
|
151 | + public function stream_stat() { |
|
152 | + return fstat($this->current); |
|
153 | + } |
|
154 | + |
|
155 | + public function stream_eof() { |
|
156 | + return feof($this->current); |
|
157 | + } |
|
158 | + |
|
159 | + public function stream_close() { |
|
160 | + fclose($this->current); |
|
161 | + } |
|
162 | + |
|
163 | + public function stream_write($data) { |
|
164 | + return false; |
|
165 | + } |
|
166 | + |
|
167 | + public function stream_set_option($option, $arg1, $arg2) { |
|
168 | + return false; |
|
169 | + } |
|
170 | + |
|
171 | + public function stream_truncate($size) { |
|
172 | + return false; |
|
173 | + } |
|
174 | + |
|
175 | + public function stream_lock($operation) { |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + public function stream_flush() { |
|
180 | + return; //noop because readonly stream |
|
181 | + } |
|
182 | 182 | } |