1
+ <?php
2
+ /*
3
+ {
4
+ "require": {
5
+ "aws/aws-sdk-php": "^3.55"
6
+ }
7
+ }
8
+ */
9
+ header ("Access-Control-Allow-Origin: * " );
10
+ require_once __DIR__ ."/vendor/autoload.php " ;
11
+ // You can call the following to erase all pending multipart uploads.
12
+ // It's a good idea to set your bucket to do this automatically (via console)
13
+ // or set this in a cronjob for every 24-48 hours
14
+ // echo abortPendingUploads(bucket());
15
+
16
+ if (file_exists ("keys.php " ))
17
+ require_once "keys.php " ;
18
+ else
19
+ {
20
+ function aws_key (){
21
+ return 'YOUR_AWS_KEY ' ;
22
+ }
23
+ function aws_secret (){
24
+ return 'YOUR_AWS_SECRET ' ;
25
+ }
26
+ function bucket () {
27
+ return "YOUR_S3_BUCKET " ;
28
+ }
29
+ }
30
+ /**
31
+ * The key perfix in the bucket to put all uploads in
32
+ * @return string
33
+ */
34
+ function prefix () {
35
+ return "upload/ " ;
36
+ }
37
+ /**
38
+ * Easy wrapper around S3 API
39
+ * @param string $command the function to call
40
+ * @param mixed $args variable args to pass
41
+ * @return mixed
42
+ */
43
+ function s3 ($ command =null ,$ args =null )
44
+ {
45
+ static $ s3 =null ;
46
+ if ($ s3 ===null )
47
+ $ s3 = new Aws \S3 \S3Client ([
48
+ 'version ' => 'latest ' ,
49
+ 'region ' => 'us-east-1 ' ,
50
+ 'signature_version ' => 'v4 ' ,
51
+ 'credentials ' => [
52
+ 'key ' => aws_key (),
53
+ 'secret ' => aws_secret (),
54
+ ]
55
+ ]);
56
+ if ($ command ===null )
57
+ return $ s3 ;
58
+ $ args =func_get_args ();
59
+ array_shift ($ args );
60
+ try {
61
+ $ res =call_user_func_array ([$ s3 ,$ command ],$ args );
62
+ return $ res ;
63
+ }
64
+ catch (AwsException $ e )
65
+ {
66
+ echo $ e ->getMessage (),PHP_EOL ;
67
+ }
68
+ return null ;
69
+ }
70
+ /**
71
+ * Output data as json with proper header
72
+ * @param mixed $data
73
+ */
74
+ function json_output ($ data )
75
+ {
76
+ header ('Content-Type: application/json ' );
77
+ die (json_encode ($ data ));
78
+ }
79
+ /**
80
+ * Deletes all multipart uploads that are not completed.
81
+ *
82
+ * Useful to clear up the clutter from your bucket
83
+ * You can also set the bucket to delete them every day
84
+ * @return integer number of deleted objects
85
+ */
86
+ function abortPendingUploads ($ bucket )
87
+ {
88
+ $ count =0 ;
89
+ $ res =s3 ("listMultipartUploads " ,["Bucket " =>bucket ()]);
90
+ if (is_array ($ res ["Uploads " ]))
91
+ foreach ($ res ["Uploads " ] as $ item )
92
+ {
93
+
94
+ $ r =s3 ("abortMultipartUpload " ,[
95
+ "Bucket " =>$ bucket ,
96
+ "Key " =>$ item ["Key " ],
97
+ "UploadId " =>$ item ["UploadId " ],
98
+ ]);
99
+ $ count ++;
100
+ }
101
+ return $ count ;
102
+ }
103
+ /**
104
+ * Enables CORS on bucket
105
+ *
106
+ * This needs to be called exactly once on a bucket before browser uploads.
107
+ * @param string $bucket
108
+ */
109
+ function setCORS ($ bucket )
110
+ {
111
+ $ res =s3 ("getBucketCors " ,["Bucket " =>$ bucket ]);
112
+ $ res =s3 ("putBucketCors " ,
113
+ [
114
+ "Bucket " =>$ bucket ,
115
+ "CORSConfiguration " =>[
116
+ "CORSRules " =>[
117
+ [
118
+ 'AllowedHeaders ' =>['* ' ],
119
+ 'AllowedMethods ' => ['POST ' ,'GET ' ,'HEAD ' ,'PUT ' ],
120
+ "AllowedOrigins " =>["localhost " ,"* " ],
121
+ ],
122
+ ],
123
+ ],
124
+ ]);
125
+ }
126
+
127
+ if (isset ($ _POST ['command ' ]))
128
+ {
129
+ $ command =$ _POST ['command ' ];
130
+ if ($ command =="create " )
131
+ {
132
+ $ res =s3 ("createMultipartUpload " ,[
133
+ 'Bucket ' => bucket (),
134
+ 'Key ' => prefix ().$ _POST ['fileInfo ' ]['name ' ],
135
+ 'ContentType ' => $ _REQUEST ['fileInfo ' ]['type ' ],
136
+ 'Metadata ' => $ _REQUEST ['fileInfo ' ]
137
+ ]);
138
+ json_output (array (
139
+ 'uploadId ' => $ res ->get ('UploadId ' ),
140
+ 'key ' => $ res ->get ('Key ' ),
141
+ ));
142
+ }
143
+
144
+ if ($ command =="part " )
145
+ {
146
+ $ command =s3 ("getCommand " ,"UploadPart " ,[
147
+ 'Bucket ' => bucket (),
148
+ 'Key ' => $ _REQUEST ['sendBackData ' ]['key ' ],
149
+ 'UploadId ' => $ _REQUEST ['sendBackData ' ]['uploadId ' ],
150
+ 'PartNumber ' => $ _REQUEST ['partNumber ' ],
151
+ 'ContentLength ' => $ _REQUEST ['contentLength ' ]
152
+ ]);
153
+
154
+ // Give it at least 24 hours for large uploads
155
+ $ request =s3 ("createPresignedRequest " ,$ command ,"+48 hours " );
156
+ json_output ([
157
+ 'url ' => (string )$ request ->getUri (),
158
+ ]);
159
+ }
160
+
161
+ if ($ command =="complete " )
162
+ {
163
+ $ partsModel = s3 ("listParts " ,[
164
+ 'Bucket ' => bucket (),
165
+ 'Key ' => $ _REQUEST ['sendBackData ' ]['key ' ],
166
+ 'UploadId ' => $ _REQUEST ['sendBackData ' ]['uploadId ' ],
167
+ ]);
168
+ $ model = s3 ("completeMultipartUpload " ,[
169
+ 'Bucket ' => bucket (),
170
+ 'Key ' => $ _REQUEST ['sendBackData ' ]['key ' ],
171
+ 'UploadId ' => $ _REQUEST ['sendBackData ' ]['uploadId ' ],
172
+ 'MultipartUpload ' => [
173
+ "Parts " =>$ partsModel ["Parts " ],
174
+ ],
175
+ ]);
176
+ json_output ([
177
+ 'success ' => true
178
+ ]);
179
+ }
180
+ if ($ command =="abort " )
181
+ {
182
+ $ model = s3 ("abortMultipartUpload " ,[
183
+ 'Bucket ' => bucket (),
184
+ 'Key ' => $ _REQUEST ['sendBackData ' ]['key ' ],
185
+ 'UploadId ' => $ _REQUEST ['sendBackData ' ]['uploadId ' ]
186
+ ]);
187
+ json_output ([
188
+ 'success ' => true
189
+ ]);
190
+ }
191
+
192
+ exit (0 );
193
+ }
194
+
195
+
196
+ include "page.htm " ;
0 commit comments