Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
17002 efrain 1
<?php
2
namespace LeadersLinked\Library;
3
 
4
use Aws\S3\S3Client;
5
 
6
class S3Files
7
{
8
 
9
    /**
10
     *
11
     * @var\LeadersLinked\Library\S3Files
12
     */
13
    private static $_instance;
14
 
15
 
16
    /**
17
     *
18
     * @var \Aws\S3\S3Client
19
     */
20
    private $s3;
21
 
22
    /**
23
     *
24
     * @var string
25
     */
26
    private $bucket;
27
 
28
 
29
    /**
30
     *
31
     * @param array $config
32
     */
33
    private function __construct($config)
34
    {
35
 
36
        $access_key     = $config['leaderslinked.minio.access_key'];
37
        $secret_key     = $config['leaderslinked.minio.secret_key'];
38
        $endpoint       = $config['leaderslinked.minio.endpoint'];
39
        $this->bucket   = $config['leaderslinked.minio.bucket'];
40
 
41
 
42
        $this->s3 = new \Aws\S3\S3Client([
43
            'version' => 'latest',
44
            'region'  => 'us-east-1',
45
            'endpoint' => $endpoint,
46
            'use_path_style_endpoint' => true,
47
            'credentials' => [
48
                'key'    => $access_key,
49
                'secret' => $secret_key,
50
            ],
51
        ]);
52
        $this->s3->registerStreamWrapper();
53
 
54
 
55
 
56
    }
57
 
58
    /**
59
     *
60
     * @return string
61
     */
62
    public function getBucket()
63
    {
64
        return $this->bucket;
65
    }
66
 
67
    /**
68
     *
69
     * @param array $config
70
     * @return \LeadersLinked\Library\S3Files
71
     */
72
    public static function getInstance($config)
73
    {
74
 
75
        if(self::$_instance == null) {
76
            self::$_instance = new S3Files($config);
77
        }
78
 
79
        return self::$_instance;
80
    }
81
 
82
    /**
83
     *
84
 
85
     * @param string $key
86
     * @return boolean
87
     */
88
    public function objectExist($key)
89
    {
90
        return $this->s3->doesObjectExist($this->bucket, $key);
91
    }
92
 
93
    /**
94
     *
95
     * @param string $remoto
96
     * @return string
97
     */
98
    public function getPresignedUrl($remoto)
99
    {
100
 
101
        $command = $this->s3->getCommand('GetObject', [
102
            'Bucket' => $this->bucket,
103
            'Key'    => $remoto
104
        ]);
105
 
106
        // Create a pre-signed URL for a request with duration of 10 miniutes
107
        $presignedRequest = $this->s3->createPresignedRequest($command, '+10 minutes');
108
 
109
        // Get the actual presigned-url
110
        $presignedUrl =  (string)  $presignedRequest->getUri();
111
 
112
        return $presignedUrl;
113
    }
114
 
115
 
116
    /**
117
     *
118
     * @param string $remote
119
     * @param string $local
120
     * @return boolean
121
     */
122
    public function putObject($remote, $local)
123
    {
124
 
125
 
126
        try {
127
            $this->s3->putObject([
128
                'Bucket'        => $this->bucket,
129
                'Key'           => $remote,
130
                'SourceFile'    => $local
131
            ]);
132
            return true;
133
        } catch (\Exception $exception) {
134
            echo "No se pudo subir el archivo : $local (" . $exception->getMessage() . ")";
135
 
136
 
137
            return false;
138
        }
139
    }
140
 
141
 
142
 
143
    /**
144
     *
145
     * @param string $remoto
146
     * @return boolean
147
     */
148
    public function deleteObject($remoto)
149
    {
150
        try {
151
            $this->s3->deleteObject([
152
                'Bucket'        => $this->bucket,
153
                'Key'           => $remoto,
154
            ]);
17008 efrain 155
            return true;
17002 efrain 156
        } catch (\Exception $exception) {
17008 efrain 157
            //echo "No se pudo borrar el archivo : $remoto (" . $exception->getMessage() . ")";
17002 efrain 158
            return false;
159
        }
160
 
161
    }
162
 
163
 
164
 
165
 
166
 
167
 
168
}