-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3.cfc
499 lines (398 loc) · 20.9 KB
/
s3.cfc
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<cfcomponent name="s3" displayname="Amazon S3 REST Wrapper v1.8">
<!---
Amazon S3 REST Wrapper
Written by Joe Danziger ([email protected]) with much help from
dorioo on the Amazon S3 Forums. See the readme for more
details on usage and methods.
Thanks to Steve Hicks for the bucket ACL updates.
Thanks to Carlos Gallupa for the EU storage location updates.
Thanks to Joel Greutman for the fix on the getObject link.
Thanks to Jerad Sloan for the Cache Control headers.
Version 1.8 - Released: July 27, 2010
Version 1.9 - Released: January 6, 2011
--->
<cfset variables.accessKeyId = "">
<cfset variables.secretAccessKey = "">
<cffunction name="init" access="public" returnType="s3" output="false"
hint="Returns an instance of the CFC initialized.">
<cfargument name="accessKeyId" type="string" required="true" hint="Amazon S3 Access Key ID.">
<cfargument name="secretAccessKey" type="string" required="true" hint="Amazon S3 Secret Access Key.">
<cfset variables.accessKeyId = arguments.accessKeyId>
<cfset variables.secretAccessKey = arguments.secretAccessKey>
<cfreturn this>
</cffunction>
<cffunction name="HMAC_SHA1" returntype="binary" access="private" output="false" hint="NSA SHA-1 Algorithm">
<cfargument name="signKey" type="string" required="true" />
<cfargument name="signMessage" type="string" required="true" />
<cfset var jMsg = JavaCast("string",arguments.signMessage).getBytes("iso-8859-1") />
<cfset var jKey = JavaCast("string",arguments.signKey).getBytes("iso-8859-1") />
<cfset var key = createObject("java","javax.crypto.spec.SecretKeySpec") />
<cfset var mac = createObject("java","javax.crypto.Mac") />
<cfset key = key.init(jKey,"HmacSHA1") />
<cfset mac = mac.getInstance(key.getAlgorithm()) />
<cfset mac.init(key) />
<cfset mac.update(jMsg) />
<cfreturn mac.doFinal() />
</cffunction>
<cffunction name="createSignature" returntype="string" access="public" output="false">
<cfargument name="stringIn" type="string" required="true" />
<!--- Replace "\n" with "chr(10) to get a correct digest --->
<cfset var fixedData = replace(arguments.stringIn,"\n","#chr(10)#","all")>
<!--- Calculate the hash of the information --->
<cfset var digest = HMAC_SHA1(variables.secretAccessKey,fixedData)>
<!--- fix the returned data to be a proper signature --->
<cfset var signature = ToBase64("#digest#")>
<cfreturn signature>
</cffunction>
<cffunction name="getBuckets" access="public" output="false" returntype="array"
description="List all available buckets.">
<cfset var data = "">
<cfset var bucket = "">
<cfset var buckets = "">
<cfset var thisBucket = "">
<cfset var allBuckets = "">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send --->
<cfset var cs = "GET\n\n\n#dateTimeString#\n/">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- get all buckets via REST --->
<cfhttp method="GET" url="http://s3.amazonaws.com">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfset data = xmlParse(cfhttp.FileContent)>
<cfset buckets = xmlSearch(data, "//:Bucket")>
<!--- create array and insert values from XML --->
<cfset allBuckets = arrayNew(1)>
<cfloop index="x" from="1" to="#arrayLen(buckets)#">
<cfset bucket = buckets[x]>
<cfset thisBucket = structNew()>
<cfset thisBucket.Name = bucket.Name.xmlText>
<cfset thisBucket.CreationDate = bucket.CreationDate.xmlText>
<cfset arrayAppend(allBuckets, thisBucket)>
</cfloop>
<cfreturn allBuckets>
</cffunction>
<cffunction name="putBucket" access="public" output="false" returntype="boolean"
description="Creates a bucket.">
<cfargument name="bucketName" type="string" required="true">
<cfargument name="acl" type="string" required="false" default="public-read">
<cfargument name="storageLocation" type="string" required="false" default="">
<cfset var strXML = "">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send based on operation requested --->
<cfset var cs = "PUT\n\ntext/html\n#dateTimeString#\nx-amz-acl:#arguments.acl#\n/#arguments.bucketName#">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<cfif compare(arguments.storageLocation,'')>
<cfsavecontent variable="strXML">
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><LocationConstraint>#arguments.storageLocation#</LocationConstraint></CreateBucketConfiguration>
</cfsavecontent>
<cfelse>
<cfset strXML = "">
</cfif>
<!--- put the bucket via REST --->
<cfhttp method="PUT" url="http://s3.amazonaws.com/#arguments.bucketName#" charset="utf-8">
<cfhttpparam type="header" name="Content-Type" value="text/html">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="x-amz-acl" value="#arguments.acl#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
<cfhttpparam type="body" value="#trim(strXML)#">
</cfhttp>
<cfreturn true>
</cffunction>
<cffunction name="getBucket" access="public" output="false" returntype="array"
description="Creates a bucket.">
<cfargument name="bucketName" type="string" required="yes">
<cfargument name="prefix" type="string" required="false" default="">
<cfargument name="marker" type="string" required="false" default="">
<cfargument name="maxKeys" type="string" required="false" default="">
<cfargument name="showVersions" type="boolean" required="false" default="false">
<cfset var cs = "">
<cfset var data = "">
<cfset var content = "">
<cfset var contents = "">
<cfset var version = "">
<cfset var versions = "">
<cfset var signature = "">
<cfset var versioning = "">
<cfset var prefixString = "">
<cfset var markerString = "">
<cfset var maxKeysString = "">
<cfset var thisContent = "">
<cfset var allContents = "">
<cfset var thisVersion = "">
<cfset var allVersions = "">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- add proper versioning call if requested --->
<cfif arguments.showVersions>
<cfset versioning = "?versions">
</cfif>
<!--- Create a canonical string to send --->
<cfset cs = "GET\n\n\n#dateTimeString#\n/#arguments.bucketName##versioning#">
<!--- Create a proper signature --->
<cfset signature = createSignature(cs)>
<!--- get the bucket via REST --->
<cfif arguments.showVersions>
<cfif compare(arguments.prefix,'')>
<cfset prefixString = "&prefix=#arguments.prefix#">
</cfif>
<cfif compare(arguments.marker,'')>
<cfset markerString = "&marker=#arguments.marker#">
</cfif>
<cfif isNumeric(arguments.maxKeys)>
<cfset maxKeysString = "&max-keys=#arguments.maxKeys#">
</cfif>
<cfhttp method="GET" url="http://s3.amazonaws.com/#arguments.bucketName#?versions#prefixString##markerString##maxKeysString#">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfelse>
<cfhttp method="GET" url="http://s3.amazonaws.com/#arguments.bucketName#">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
<cfif compare(arguments.prefix,'')>
<cfhttpparam type="URL" name="prefix" value="#arguments.prefix#">
</cfif>
<cfif compare(arguments.marker,'')>
<cfhttpparam type="URL" name="marker" value="#arguments.marker#">
</cfif>
<cfif isNumeric(arguments.maxKeys)>
<cfhttpparam type="URL" name="max-keys" value="#arguments.maxKeys#">
</cfif>
</cfhttp>
</cfif>
<cfset data = xmlParse(cfhttp.FileContent)>
<cfif arguments.showVersions>
<cfset versions = xmlSearch(data, "//:Version")>
<!--- create array and insert values from XML --->
<cfset allVersions = arrayNew(1)>
<cfloop index="x" from="1" to="#arrayLen(versions)#">
<cfset version = versions[x]>
<cfset thisVersion = structNew()>
<cfset thisVersion.Key = version.Key.xmlText>
<cfset thisVersion.VersionID = version.VersionID.xmlText>
<cfset thisVersion.isLatest = version.IsLatest.xmlText>
<cfset thisVersion.LastModified = version.LastModified.xmlText>
<cfset thisVersion.Size = version.Size.xmlText>
<cfset thisVersion.StorageClass = version.StorageClass.xmlText>
<cfset arrayAppend(allVersions, thisVersion)>
</cfloop>
<cfreturn allVersions>
<cfelse>
<cfset contents = xmlSearch(data, "//:Contents")>
<!--- create array and insert values from XML --->
<cfset allContents = arrayNew(1)>
<cfloop index="x" from="1" to="#arrayLen(contents)#">
<cfset content = contents[x]>
<cfset thisContent = structNew()>
<cfset thisContent.Key = content.Key.xmlText>
<cfset thisContent.LastModified = content.LastModified.xmlText>
<cfset thisContent.Size = content.Size.xmlText>
<cfset thisContent.StorageClass = content.StorageClass.xmlText>
<cfset arrayAppend(allContents, thisContent)>
</cfloop>
<cfreturn allContents>
</cfif>
</cffunction>
<cffunction name="deleteBucket" access="public" output="false" returntype="boolean"
description="Deletes a bucket.">
<cfargument name="bucketName" type="string" required="yes">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send based on operation requested --->
<cfset var cs = "DELETE\n\n\n#dateTimeString#\n/#arguments.bucketName#">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- delete the bucket via REST --->
<cfhttp method="DELETE" url="http://s3.amazonaws.com/#arguments.bucketName#" charset="utf-8">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfreturn true>
</cffunction>
<cffunction name="putObject" access="public" output="false" returntype="string"
description="Puts an object into a bucket.">
<cfargument name="bucketName" type="string" required="yes">
<cfargument name="fileKey" type="string" required="yes">
<cfargument name="contentType" type="string" required="yes">
<cfargument name="HTTPtimeout" type="numeric" required="no" default="300">
<cfargument name="cacheControl" type="boolean" required="false" default="true">
<cfargument name="cacheDays" type="numeric" required="false" default="30">
<cfargument name="acl" type="string" required="no" default="public-read">
<cfargument name="storageClass" type="string" required="no" default="STANDARD">
<cfargument name="keyName" type="string" required="no" default="#arguments.fileKey#">
<cfargument name="uploadDir" type="string" required="no" default="#ExpandPath('./')#">
<cfset var versionID = "">
<cfset var binaryFileData = "">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<cfset var cs = "">
<cfset var signature = "">
<!--- if keyName submitted blank, use fileKey --->
<cfif not compare(arguments.keyName,'')>
<cfset arguments.keyName = arguments.fileKey>
</cfif>
<!--- Create a canonical string to send --->
<cfset cs = "PUT\n\n#arguments.contentType#\n#dateTimeString#\nx-amz-acl:#arguments.acl#\nx-amz-storage-class:#arguments.storageClass#\n/#arguments.bucketName#/#arguments.keyName#">
<!--- Create a proper signature --->
<cfset signature = createSignature(cs)>
<!--- Read the image data into a variable --->
<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
<!--- Send the file to amazon. The "X-amz-acl" controls the access properties of the file --->
<cfhttp method="PUT" url="http://s3.amazonaws.com/#arguments.bucketName#/#arguments.keyName#" timeout="#arguments.HTTPtimeout#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
<cfhttpparam type="header" name="Content-Type" value="#arguments.contentType#">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="x-amz-acl" value="#arguments.acl#">
<cfhttpparam type="header" name="x-amz-storage-class" value="#arguments.storageClass#">
<cfhttpparam type="body" value="#binaryFileData#">
<cfif arguments.cacheControl>
<cfhttpparam type="header" name="Cache-Control" value="max-age=2592000">
<cfhttpparam type="header" name="Expires" value="#DateFormat(now()+arguments.cacheDays,'ddd, dd mmm yyyy')# #TimeFormat(now(),'H:MM:SS')# GMT">
</cfif>
</cfhttp>
<cftry>
<cfset versionID = cfhttp.responseHeader['x-amz-version-id']>
<cfcatch></cfcatch>
</cftry>
<cfreturn versionID>
</cffunction>
<cffunction name="getObject" access="public" output="false" returntype="string"
description="Returns a link to an object.">
<cfargument name="bucketName" type="string" required="yes">
<cfargument name="fileKey" type="string" required="yes">
<cfargument name="minutesValid" type="string" required="false" default="60">
<cfset var timedAmazonLink = "">
<cfset var epochTime = DateDiff("s", DateConvert("utc2Local", "January 1 1970 00:00"), now()) + (arguments.minutesValid * 60)>
<!--- Create a canonical string to send --->
<cfset var cs = "GET\n\n\n#epochTime#\n/#arguments.bucketName#/#arguments.fileKey#">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- Create the timed link for the image --->
<cfset timedAmazonLink = "http://s3.amazonaws.com/#arguments.bucketName#/#arguments.fileKey#?AWSAccessKeyId=#URLEncodedFormat(variables.accessKeyId)#&Expires=#epochTime#&Signature=#URLEncodedFormat(signature)#">
<cfreturn timedAmazonLink>
</cffunction>
<cffunction name="deleteObject" access="public" output="false" returntype="boolean"
description="Deletes an object.">
<cfargument name="bucketName" type="string" required="yes">
<cfargument name="fileKey" type="string" required="yes">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send based on operation requested --->
<cfset var cs = "DELETE\n\n\n#dateTimeString#\n/#arguments.bucketName#/#arguments.fileKey#">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- delete the object via REST --->
<cfhttp method="DELETE" url="http://s3.amazonaws.com/#arguments.bucketName#/#arguments.fileKey#">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfreturn true>
</cffunction>
<cffunction name="copyObject" access="public" output="false" returntype="boolean"
description="Copies an object.">
<cfargument name="oldBucketName" type="string" required="yes">
<cfargument name="oldFileKey" type="string" required="yes">
<cfargument name="newBucketName" type="string" required="yes">
<cfargument name="newFileKey" type="string" required="yes">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send based on operation requested --->
<cfset var cs = "PUT\n\napplication/octet-stream\n#dateTimeString#\nx-amz-copy-source:/#arguments.oldBucketName#/#arguments.oldFileKey#\n/#arguments.newBucketName#/#arguments.newFileKey#">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<cfif compare(arguments.oldBucketName,arguments.newBucketName) or compare(arguments.oldFileKey,arguments.newFileKey)>
<!--- delete the object via REST --->
<cfhttp method="PUT" url="http://s3.amazonaws.com/#arguments.newBucketName#/#arguments.newFileKey#">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="x-amz-copy-source" value="/#arguments.oldBucketName#/#arguments.oldFileKey#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="renameObject" access="public" output="false" returntype="boolean"
description="Renames an object by copying then deleting original.">
<cfargument name="oldBucketName" type="string" required="yes">
<cfargument name="oldFileKey" type="string" required="yes">
<cfargument name="newBucketName" type="string" required="yes">
<cfargument name="newFileKey" type="string" required="yes">
<cfif compare(arguments.oldBucketName,arguments.newBucketName) or compare(arguments.oldFileKey,arguments.newFileKey)>
<cfset copyObject(arguments.oldBucketName,arguments.oldFileKey,arguments.newBucketName,arguments.newFileKey)>
<cfset deleteObject(arguments.oldBucketName,arguments.oldFileKey)>
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="objectExists" access="public" output="false" returntype="boolean"
description="Tests if object exists in bucket.">
<cfargument name="bucketName" type="string" required="yes">
<cfargument name="fileKey" type="string" required="yes">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<cfset var result = "">
<cfset var found = 0>
<!--- Create a canonical string to send --->
<cfset cs = "HEAD\n\n\n#dateTimeString#\n/#arguments.bucketName#/#arguments.fileKey#">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- HEAD the object via REST --->
<cfhttp method="HEAD" url="http://s3.amazonaws.com/#arguments.bucketName#/#arguments.fileKey#" result="result">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfswitch expression="#Left(result.statusCode,3)#">
<cfcase value="200">
<cfset found = 1>
</cfcase>
<cfcase value="404">
<cfset found = 0>
</cfcase>
</cfswitch>
<cfreturn found>
</cffunction>
<cffunction name="getBucketVersioning" access="public" output="false" returntype="string"
description="Determines versioning setting for a bucket.">
<cfargument name="bucketName" type="string" required="yes">
<cfset var data = "">
<cfset var result = "">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send --->
<cfset var cs = "GET\n\n\n#dateTimeString#\n/#arguments.bucketName#?versioning">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<!--- get the bucket via REST --->
<cfhttp method="GET" url="http://s3.amazonaws.com/#arguments.bucketName#?versioning">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
</cfhttp>
<cfset data = xmlParse(cfhttp.FileContent)>
<cftry>
<cfset result = data.VersioningConfiguration.Status.xmlText>
<cfcatch><cfset result = "Disabled"></cfcatch>
</cftry>
<cfreturn result>
</cffunction>
<cffunction name="setBucketVersioning" access="public" output="false" returntype="boolean"
description="Sets versioning on a bucket.">
<cfargument name="bucketName" type="string" required="true">
<cfargument name="versioning" type="string" required="false" default="Enabled">
<cfset var strXML = "">
<cfset var dateTimeString = GetHTTPTimeString(Now())>
<!--- Create a canonical string to send based on operation requested --->
<cfset var cs = "PUT\n\ntext/html\n#dateTimeString#\n/#arguments.bucketName#?versioning">
<!--- Create a proper signature --->
<cfset var signature = createSignature(cs)>
<cfsavecontent variable="strXML">
<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status><cfoutput>#arguments.versioning#</cfoutput></Status></VersioningConfiguration>
</cfsavecontent>
<!--- put the bucket via REST --->
<cfhttp method="PUT" url="http://s3.amazonaws.com/#arguments.bucketName#?versioning" charset="utf-8">
<cfhttpparam type="header" name="Content-Type" value="text/html">
<cfhttpparam type="header" name="Date" value="#dateTimeString#">
<cfhttpparam type="header" name="Authorization" value="AWS #variables.accessKeyId#:#signature#">
<cfhttpparam type="body" value="#trim(strXML)#">
</cfhttp>
<cfreturn true>
</cffunction>
</cfcomponent>