@@ -74,6 +74,10 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | + /** |
|
| 78 | + * @param string $urn |
|
| 79 | + * @param resource $stream |
|
| 80 | + */ |
|
| 77 | 81 | protected function singlePartUpload($urn, $stream) { |
| 78 | 82 | $this->getConnection()->putObject([ |
| 79 | 83 | 'Bucket' => $this->bucket, |
@@ -82,6 +86,10 @@ discard block |
||
| 82 | 86 | ]); |
| 83 | 87 | } |
| 84 | 88 | |
| 89 | + /** |
|
| 90 | + * @param string $urn |
|
| 91 | + * @param resource $stream |
|
| 92 | + */ |
|
| 85 | 93 | protected function multiPartUpload($urn, $stream) { |
| 86 | 94 | $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
| 87 | 95 | 'bucket' => $this->bucket, |
@@ -29,95 +29,95 @@ |
||
| 29 | 29 | const S3_UPLOAD_PART_SIZE = 5368709120; |
| 30 | 30 | |
| 31 | 31 | trait S3ObjectTrait { |
| 32 | - /** |
|
| 33 | - * Returns the connection |
|
| 34 | - * |
|
| 35 | - * @return S3Client connected client |
|
| 36 | - * @throws \Exception if connection could not be made |
|
| 37 | - */ |
|
| 38 | - abstract protected function getConnection(); |
|
| 32 | + /** |
|
| 33 | + * Returns the connection |
|
| 34 | + * |
|
| 35 | + * @return S3Client connected client |
|
| 36 | + * @throws \Exception if connection could not be made |
|
| 37 | + */ |
|
| 38 | + abstract protected function getConnection(); |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param string $urn the unified resource name used to identify the object |
|
| 42 | - * @return resource stream with the read data |
|
| 43 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 44 | - * @since 7.0.0 |
|
| 45 | - */ |
|
| 46 | - function readObject($urn) { |
|
| 47 | - $client = $this->getConnection(); |
|
| 48 | - $command = $client->getCommand('GetObject', [ |
|
| 49 | - 'Bucket' => $this->bucket, |
|
| 50 | - 'Key' => $urn |
|
| 51 | - ]); |
|
| 52 | - $command['@http']['stream'] = true; |
|
| 53 | - $result = $client->execute($command); |
|
| 54 | - /** @var StreamInterface $body */ |
|
| 55 | - $body = $result['Body']; |
|
| 40 | + /** |
|
| 41 | + * @param string $urn the unified resource name used to identify the object |
|
| 42 | + * @return resource stream with the read data |
|
| 43 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 44 | + * @since 7.0.0 |
|
| 45 | + */ |
|
| 46 | + function readObject($urn) { |
|
| 47 | + $client = $this->getConnection(); |
|
| 48 | + $command = $client->getCommand('GetObject', [ |
|
| 49 | + 'Bucket' => $this->bucket, |
|
| 50 | + 'Key' => $urn |
|
| 51 | + ]); |
|
| 52 | + $command['@http']['stream'] = true; |
|
| 53 | + $result = $client->execute($command); |
|
| 54 | + /** @var StreamInterface $body */ |
|
| 55 | + $body = $result['Body']; |
|
| 56 | 56 | |
| 57 | - return $body->detach(); |
|
| 58 | - } |
|
| 57 | + return $body->detach(); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @param string $urn the unified resource name used to identify the object |
|
| 62 | - * @param resource $stream stream with the data to write |
|
| 63 | - * @throws \Exception when something goes wrong, message will be logged |
|
| 64 | - * @since 7.0.0 |
|
| 65 | - */ |
|
| 66 | - function writeObject($urn, $stream) { |
|
| 67 | - $stat = fstat($stream); |
|
| 60 | + /** |
|
| 61 | + * @param string $urn the unified resource name used to identify the object |
|
| 62 | + * @param resource $stream stream with the data to write |
|
| 63 | + * @throws \Exception when something goes wrong, message will be logged |
|
| 64 | + * @since 7.0.0 |
|
| 65 | + */ |
|
| 66 | + function writeObject($urn, $stream) { |
|
| 67 | + $stat = fstat($stream); |
|
| 68 | 68 | |
| 69 | - if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) { |
|
| 70 | - $this->singlePartUpload($urn, $stream); |
|
| 71 | - } else { |
|
| 72 | - $this->multiPartUpload($urn, $stream); |
|
| 73 | - } |
|
| 69 | + if ($stat['size'] && $stat['size'] < S3_UPLOAD_PART_SIZE) { |
|
| 70 | + $this->singlePartUpload($urn, $stream); |
|
| 71 | + } else { |
|
| 72 | + $this->multiPartUpload($urn, $stream); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - protected function singlePartUpload($urn, $stream) { |
|
| 78 | - $this->getConnection()->putObject([ |
|
| 79 | - 'Bucket' => $this->bucket, |
|
| 80 | - 'Key' => $urn, |
|
| 81 | - 'Body' => $stream |
|
| 82 | - ]); |
|
| 83 | - } |
|
| 77 | + protected function singlePartUpload($urn, $stream) { |
|
| 78 | + $this->getConnection()->putObject([ |
|
| 79 | + 'Bucket' => $this->bucket, |
|
| 80 | + 'Key' => $urn, |
|
| 81 | + 'Body' => $stream |
|
| 82 | + ]); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - protected function multiPartUpload($urn, $stream) { |
|
| 86 | - $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
| 87 | - 'bucket' => $this->bucket, |
|
| 88 | - 'key' => $urn, |
|
| 89 | - ]); |
|
| 85 | + protected function multiPartUpload($urn, $stream) { |
|
| 86 | + $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
| 87 | + 'bucket' => $this->bucket, |
|
| 88 | + 'key' => $urn, |
|
| 89 | + ]); |
|
| 90 | 90 | |
| 91 | - $tries = 0; |
|
| 91 | + $tries = 0; |
|
| 92 | 92 | |
| 93 | - do { |
|
| 94 | - try { |
|
| 95 | - $result = $uploader->upload(); |
|
| 96 | - } catch (MultipartUploadException $e) { |
|
| 97 | - rewind($stream); |
|
| 98 | - $tries++; |
|
| 93 | + do { |
|
| 94 | + try { |
|
| 95 | + $result = $uploader->upload(); |
|
| 96 | + } catch (MultipartUploadException $e) { |
|
| 97 | + rewind($stream); |
|
| 98 | + $tries++; |
|
| 99 | 99 | |
| 100 | - if ($tries < 5) { |
|
| 101 | - $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
| 102 | - 'state' => $e->getState() |
|
| 103 | - ]); |
|
| 104 | - } else { |
|
| 105 | - $this->getConnection()->abortMultipartUpload($e->getState()->getId()); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } while (!isset($result) && $tries < 5); |
|
| 109 | - } |
|
| 100 | + if ($tries < 5) { |
|
| 101 | + $uploader = new MultipartUploader($this->getConnection(), $stream, [ |
|
| 102 | + 'state' => $e->getState() |
|
| 103 | + ]); |
|
| 104 | + } else { |
|
| 105 | + $this->getConnection()->abortMultipartUpload($e->getState()->getId()); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } while (!isset($result) && $tries < 5); |
|
| 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 | } |