Created
October 18, 2017 12:56
-
-
Save GaryStanton/68813ba70f797360929ec7d18fb6c9bc to your computer and use it in GitHub Desktop.
s3_putObject.cfc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="S3_PutObject" access="public" returntype="struct"> | |
<cfargument name="Bucket" type="string" required="true" hint="The name of the bucket to which you would like to upload the file" /> | |
<cfargument name="FilePath" type="string" required="true" hint="Full path of the file you would like to upload" /> | |
<cfargument name="BucketPath" type="string" required="true" hint="Path within your bucket, to which you would like this file to be added" /> | |
<cfargument name="AccessControl" type="string" default="Private" hint="Access level to grant to this file (Private / PublicRead)" /> | |
<cfscript> | |
try { | |
// Check file exists | |
if (NOT fileExists(Arguments.FilePath)) { | |
throw('File not found: #Arguments.FilePath#'); | |
} | |
// Get file name | |
Local.FileName = GetFileFromPath(Arguments.FilePath); | |
// Read file | |
Local.FileObject = createObject("java", "java.io.File").init(Arguments.FilePath); | |
// Create put object | |
Local.PutObject = createObject("java","com.amazonaws.services.s3.model.PutObjectRequest").init(Arguments.Bucket, Arguments.BucketPath & '/' & Local.FileName, Local.FileObject); | |
// Set meta data | |
Local.MetaData = createObject("java","com.amazonaws.services.s3.model.ObjectMetadata"); | |
Local.MetaData.setContentType(fileGetMimeType(Local.FileObject)); | |
Local.MetaData.setContentDisposition("inline; filename=#Local.FileName#"); | |
Local.PutObject.setMetadata(Local.MetaData); | |
// Set ACL | |
Local.ACL = createObject("java","com.amazonaws.services.s3.model.CannedAccessControlList")[Arguments.AccessControl]; | |
Local.PutObject.setCannedAcl(Local.ACL); | |
// Create S3 client | |
Local.Credentials = createObject("java","com.amazonaws.auth.BasicAWSCredentials").init(Variables.AWS.Key, Variables.AWS.Secret); | |
Local.S3Client = createObject("java","com.amazonaws.services.s3.AmazonS3Client").init(Local.Credentials); | |
// Upload file | |
Local.S3Object = Local.S3Client.putObject(Local.PutObject); | |
// Get URL | |
return Local.S3Client.getResourceUrl(Arguments.Bucket, Arguments.BucketPath & '/' & Local.FileName); | |
} | |
catch(any error) { | |
writedump(error) | |
} | |
</cfscript> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment