forked from jovanialferez/yii2-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonS3.php
163 lines (142 loc) · 4.46 KB
/
AmazonS3.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* @author: Jovani F. Alferez <[email protected]>
*/
namespace ThePeach\yii2s3;
/**
* A Yii2-compatible component wrapper for Aws\S3\S3Client.
* Just add this component to your configuration providing this class,
* key, secret and bucket.
*
* ~~~
* 'components' => [
* 'storage' => [
* 'class' => '\jovanialferez\yii2s3\AmazonS3',
* 'key' => 'AWS_ACCESS_KEY_ID',
* 'secret' => 'AWS_SECRET_ACCESS_KEY',
* 'bucket' => 'YOUR_BUCKET',
* 'region' => 'eu-west-1' // this is optional, but required if your bucket is not in US
* ],
* ],
* ~~~
*
* You can then start using this component as:
*
* ```php
* $storage = \Yii::$app->storage;
* $url = $storage->uploadFile('/path/to/file', 'unique_file_name');
* ```
*/
class AmazonS3 extends \yii\base\Component
{
public $bucket;
public $key;
public $secret;
public $region = null;
private $_client;
public function init()
{
parent::init();
$options = [
'key' => $this->key,
'secret' => $this->secret
];
if ($this->region !== null) {
$options['region'] = $this->region;
}
$this->_client = \Aws\S3\S3Client::factory($options);
}
/**
* Uploads the file into S3 in that bucket.
*
* @param string $filePath Full path of the file. Can be from tmp file path.
* @param string $fileName Filename to save this file into S3. May include directories.
* @param bool $bucket Override configured bucket.
* @return bool|string The S3 generated url that is publicly-accessible.
*/
public function uploadFile($filePath, $fileName, $bucket = false)
{
if (!$bucket) {
$bucket = $this->bucket;
}
try {
$result = $this->_client->putObject([
'ACL' => 'public-read',
'Bucket' => $bucket,
'Key' => $fileName,
'SourceFile' => $filePath,
'ContentType' => \yii\helpers\FileHelper::getMimeType($filePath),
]);
} catch (\Exception $e) {
return false;
}
return $result->get('ObjectURL');
}
/**
* Deletes a file from the S3 bucket.
*
* @param string $fileName Filename to delete from the bucket. May include directories.
* @param bool $bucket Override configured bucket.
* @return bool if the delete operation completed successfully.
*/
public function deleteFile($fileName, $bucket = false)
{
if (!$bucket) {
$bucket = $this->bucket;
}
try {
$result = $this->_client->deleteObject([
'Bucket' => $bucket,
'Key' => $fileName
]);
} catch (\Exception $e) {
return false;
}
// delete is an idempotent operation
// @see https://forums.aws.amazon.com/thread.jspa?messageID=455154
return $this->doesFileExist($fileName, $bucket);
}
/**
* Checks if a file esists in the S3 bucket.
*
* @param string $fileName Fielname to check in the bucket. May include directories.
* @param bool $bucket Override configured bucket.
* @return bool if the file exists or not
*/
public function doesFileExist($fileName, $bucket = false)
{
if (!$bucket) {
$bucket = $this->bucket;
}
try {
$result = $this->_client->doesObjectExist($bucket, $fileName);
} catch (\Exception $e){
return false;
}
return $result;
}
/**
* Copies an existing file to another file
*
* @param string $fromfileName the path of the source file name
* @param string $toFileName the path name of the new file name
* @param string $bucket Overrides configured bucket
* @return string|null the new file path or null
*/
public function copyFile($fromFileName, $toFileName, $bucket = false)
{
if (!$bucket) {
$bucket = $this->bucket;
}
try {
$result = $this->_client->copyObject([
'Bucket' => $bucket,
'CopySource' => $bucket . DIRECTORY_SEPARATOR . $fromFileName,
'Key' => $toFileName
]);
} catch (\Exception $e) {
return null;
}
return ($this->doesFileExist($toFileName, $bucket)) ? $toFileName : null;
}
}