-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathS3.php
More file actions
138 lines (122 loc) · 3.7 KB
/
S3.php
File metadata and controls
138 lines (122 loc) · 3.7 KB
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
<?php namespace Codesleeve\Stapler\Storage;
use Aws\S3\S3Client;
use Codesleeve\Stapler\Attachment;
class S3 implements StorageableInterface
{
/**
* The current attachedFile object being processed.
*
* @var \Codesleeve\Stapler\Attachment
*/
public $attachedFile;
/**
* The AWS S3Client instance.
*
* @var S3Client
*/
protected $s3Client;
/**
* Boolean flag indicating if this attachment's bucket currently exists.
*
* @var array
*/
protected $bucketExists = false;
/**
* Constructor method
*
* @param Attachment $attachedFile
* @param S3Client $s3Client
*/
function __construct(Attachment $attachedFile, S3Client $s3Client)
{
$this->attachedFile = $attachedFile;
$this->s3Client = $s3Client;
}
/**
* Return the url for a file upload.
*
* @param string $styleName
* @return string
*/
public function url($styleName)
{
return $this->s3Client->getObjectUrl($this->attachedFile->s3_object_config['Bucket'], $this->path($styleName), null, ['PathStyle' => true]);
}
/**
* Return the key the uploaded file object is stored under within a bucket.
*
* @param string $styleName
* @return string
*/
public function path($styleName)
{
return $this->attachedFile->getInterpolator()->interpolate($this->attachedFile->path, $this->attachedFile, $styleName);
}
/**
* Remove an attached file.
*
* @param array $filePaths
*/
public function remove(array $filePaths)
{
if ($filePaths) {
if (defined('Aws\S3\S3Client::LATEST_API_VERSION')) {
$this->s3Client->deleteObjects(['Bucket' => $this->attachedFile->s3_object_config['Bucket'], 'Objects' => $this->getKeys($filePaths)]);
} else {
$this->s3Client->deleteObjects(['Bucket' => $this->attachedFile->s3_object_config['Bucket'], 'Delete' => ['Objects' => $this->getKeys($filePaths)]]);
}
}
}
/**
* Move an uploaded file to it's intended destination.
*
* @param string $file
* @param string $filePath
*/
public function move($file, $filePath)
{
$objectConfig = $this->attachedFile->s3_object_config;
$fileSpecificConfig = ['Key' => $filePath, 'SourceFile' => $file, 'ContentType' => $this->attachedFile->contentType()];
$mergedConfig = array_merge($objectConfig, $fileSpecificConfig);
$this->ensureBucketExists($mergedConfig['Bucket']);
$this->s3Client->putObject($mergedConfig);
}
/**
* Return an array of paths (bucket keys) for an attachment.
* There will be one path for each of the attachmetn's styles.
*
* @param $filePaths
* @return array
*/
protected function getKeys($filePaths)
{
$keys = [];
foreach ($filePaths as $filePath) {
$keys[] = ['Key' => $filePath];
}
return $keys;
}
/**
* Ensure that a given S3 bucket exists.
*
* @param string $bucketName
*/
protected function ensureBucketExists($bucketName)
{
if (!$this->bucketExists) {
$this->buildBucket($bucketName);
}
}
/**
* Attempt to build a bucket (if it doesn't already exist).
*
* @param string $bucketName
*/
protected function buildBucket($bucketName)
{
if (!$this->s3Client->doesBucketExist($bucketName, true)) {
$this->s3Client->createBucket(['ACL' => $this->attachedFile->ACL, 'Bucket' => $bucketName, 'LocationConstraint' => $this->attachedFile->region]);
}
$this->bucketExists = true;
}
}