File size: 1,400 Bytes
a4a352b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace app\common\extend\upload;

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

class S3
{
    public $name = 'S3';
    public $ver = '1.0';
    private $config = [];

    public function __construct($config = []) {
        $this->config = $config;
    }

    public function submit($file_path)
    {
        $bucket = $GLOBALS['config']['upload']['api']['s3']['bucket'];
        $accessKey = $GLOBALS['config']['upload']['api']['s3']['accesskey'];
        $secretKey = $GLOBALS['config']['upload']['api']['s3']['secretkey'];
        $region = $GLOBALS['config']['upload']['api']['s3']['region'];

        require_once ROOT_PATH . 'extend/aws/autoload.php';
        $s3 = new S3Client([
            'region'  => $region,
            'version' => '2006-03-01',
            'credentials' => [
                'key'    => $accessKey,
                'secret' => $secretKey
            ]
        ]);
        try {
            $filePath = ROOT_PATH . $file_path;
            $result = $s3->putObject([
                'Bucket' => $bucket,
                'Key'    => $file_path,
                'Body'   => fopen($filePath, 'r'),
                'ACL'    => 'public-read'
            ]);
        } catch (AwsException $e) {
            echo $e->getMessage() . "\n";
        }

        empty($this->config['keep_local']) && @unlink($filePath);
        return $result['ObjectURL'];
    }
}