Skip to content

Instantly share code, notes, and snippets.

@battlejj
Created April 25, 2012 14:59
Show Gist options
  • Select an option

  • Save battlejj/2490364 to your computer and use it in GitHub Desktop.

Select an option

Save battlejj/2490364 to your computer and use it in GitHub Desktop.
Unzipping to amazons3 with CF
function upload(){
destinationBucket = "s3://my-bucket/my-destination-folder";
extractToS3(form.fileData,destinationBucket);
}
function extractToS3(zipFile,s3destination){
/* init our utils cfc */
var utils = new utils();
var from = "";
var to = "";
var meta = {};
var perms = [
{group="all", permission="read"},
{id="[CANONICAL_ID_GOES_HERE]", permission="full_control"}
];
/* place to store our extracted zip before uploading to s3 */
var tempDestination = replace(getTemplatePath(),"index.cfm","") & "files\temp#createUUID()#";
var s3destination = arguments.s3destination;
directoryCreate(tempDestination);
/* unzip our contents to the tempDestination */
utils.zip(action="unzip", destination=tempDestination, file=arguments.zipFile);
var extractedDirectoryList = directoryList(tempDestination,true,"query");
for(i = 1; i <= extractedDirectoryList.recordCount; i++){
if(extractedDirectoryList.type[i] EQ "File"){
from = extractedDirectoryList.directory[i] & "\" & extractedDirectoryList.name[i];
to = s3destination & replace(replace(extractedDirectoryList.directory[i],tempDestination,""),"\","/","all") & "/" & extractedDirectoryList.name[i];
fileCopy(from, to);
storeSetMetadata(to,meta);
storeSetACL(to,perms);
} else {
directoryCreate(s3destination & replace(replace(extractedDirectoryList.directory[i],tempDestination,""),"\","/","all") & extractedDirectoryList.name[i]);
}
}
}
<cfcomponent output="false">
<cffunction name="zip" access="public" returntype="any" output="false">
<cfset var result = "" />
<cfzip attributeCollection="#arguments#" />
</cffunction>
</cfcomponent>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment