Completed
Push — master ( 6e7b55...86094f )
by Sebastian
03:17
created

AmazonS3v3::getUploadPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use Aws\S3\MultipartUploader;
5
use Aws\S3\S3Client;
6
use phpbu\App\Backup\Collector;
7
use phpbu\App\Backup\Path;
8
use phpbu\App\Backup\Target;
9
use phpbu\App\Result;
10
11
/**
12
 * Amazon S3 Sync
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 3.0.0
21
 */
22
class AmazonS3v3 extends AmazonS3
23
{
24
    use Cleanable;
25
26
    /**
27
     * Amazon S3 client.
28
     *
29
     * @var S3Client;
30
     */
31
    protected $client;
32
33
    /**
34
     * Configure the sync.
35
     *
36
     * @see    \phpbu\App\Backup\Sync::setup()
37
     * @param  array $config
38
     * @throws \phpbu\App\Backup\Sync\Exception
39
     * @throws \phpbu\App\Exception
40
     */
41 9
    public function setup(array $config)
42
    {
43 9
        parent::setup($config);
44
45 4
        $this->setUpCleanable($config);
46 4
    }
47
48
    /**
49
     * Execute the sync.
50
     *
51
     * @see    \phpbu\App\Backup\Sync::sync()
52
     * @param  \phpbu\App\Backup\Target $target
53
     * @param  \phpbu\App\Result        $result
54
     * @throws \phpbu\App\Backup\Sync\Exception
55
     */
56
    public function sync(Target $target, Result $result)
57
    {
58
        $this->client = new S3Client([
59
            'region'  => $this->region,
60
            'version' => '2006-03-01',
61
            'credentials' => [
62
                'key'    => $this->key,
63
                'secret' => $this->secret,
64
            ]
65
        ]);
66
67
        if (!$this->client->doesBucketExist($this->bucket)) {
68
            $result->debug('create s3 bucket');
69
            $this->createBucket($this->client);
70
        }
71
72
        try {
73
            $this->upload($target, $this->client);
74
        } catch (\Exception $e) {
75
            throw new Exception($e->getMessage(), null, $e);
76
        }
77
        // run remote cleanup
78
        $this->cleanup($target, $result);
79
        $result->debug('upload: done');
80
    }
81
82
    /**
83
     * Creates collector for Amazon S3
84
     *
85
     * @param  \phpbu\App\Backup\Target $target
86
     * @return \phpbu\App\Backup\Collector
87
     */
88
    protected function createCollector(Target $target) : Collector
89
    {
90
        $path = new Path($this->pathRaw, $this->time);
91
        return new Collector\AmazonS3v3($target, $path, $this->client, $this->bucket);
92
    }
93
94
    /**
95
     * Simulate the sync execution.
96
     *
97
     * @param \phpbu\App\Backup\Target $target
98
     * @param \phpbu\App\Result        $result
99
     */
100 1
    public function simulate(Target $target, Result $result)
101
    {
102 1
        parent::simulate($target, $result);
103
104 1
        $this->simulateRemoteCleanup($target, $result);
105 1
    }
106
107
    /**
108
     * Create a s3 bucket.
109
     *
110
     * @param \Aws\S3\S3Client $s3
111
     */
112
    private function createBucket(S3Client $s3)
113
    {
114
        $s3->createBucket([
115
            'ACL'                       => $this->acl,
116
            'Bucket'                    => $this->bucket,
117
            'CreateBucketConfiguration' => [
118
                'LocationConstraint' => $this->region,
119
            ]
120
        ]);
121
    }
122
123
    /**
124
     * Upload backup to Amazon S3 bucket.
125
     *
126
     * @param  \phpbu\App\Backup\Target $target
127
     * @param  \Aws\S3\S3Client         $s3
128
     * @throws \phpbu\App\Backup\Sync\Exception
129
     * @throws \phpbu\App\Exception
130
     */
131
    private function upload(Target $target, S3Client $s3)
132
    {
133
        if ($this->useMultiPartUpload($target)) {
134
            $this->uploadMultiPart($target, $s3);
135
        } else {
136
            $this->uploadStream($target, $s3);
137
        }
138
    }
139
140
    /**
141
     * Upload via stream wrapper.
142
     *
143
     * @param  \phpbu\App\Backup\Target $target
144
     * @param  \Aws\S3\S3Client         $s3
145
     * @throws \phpbu\App\Backup\Sync\Exception
146
     */
147
    private function uploadStream(Target $target, S3Client $s3)
148
    {
149
        $s3->registerStreamWrapper();
150
        $source = $this->getFileHandle($target->getPathname(), 'r');
151
        $stream = $this->getFileHandle('s3://' . $this->bucket . '/' . $this->getUploadPath($target), 'w');
152
        while (!feof($source)) {
153
            fwrite($stream, fread($source, 4096));
154
        }
155
        fclose($stream);
156
    }
157
158
    /**
159
     * Upload via multi part.
160
     *
161
     * @param \phpbu\App\Backup\Target $target
162
     * @param \Aws\S3\S3Client         $s3
163
     * @param \Aws\Exception\MultipartUploadException
164
     */
165
    private function uploadMultiPart(Target $target, S3Client $s3)
166
    {
167
        $uploader = new MultipartUploader($s3, $target->getPathname(), [
168
            'bucket' => $this->bucket,
169
            'key'    => $this->getUploadPath($target),
170
        ]);
171
        $uploader->upload();
172
    }
173
174
    /**
175
     * Open stream and validate it.
176
     *
177
     * @param  string $path
178
     * @param  string $mode
179
     * @return resource
180
     * @throws \phpbu\App\Backup\Sync\Exception
181
     */
182
    private function getFileHandle($path, $mode)
183
    {
184
        $handle = fopen($path, $mode);
185
        if (!is_resource($handle)) {
186
            throw new Exception('fopen failed: could not open stream ' . $path);
187
        }
188
        return $handle;
189
    }
190
191
    /**
192
     * Get the s3 upload path
193
     *
194
     * @param \phpbu\App\Backup\Target $target
195
     * @return string
196
     */
197 2
    public function getUploadPath(Target $target)
198
    {
199 2
        return (!empty($this->path) ? $this->path . '/' : '') . $target->getFilename();
200
    }
201
}
202