Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created December 16, 2011 19:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aaronksaunders/1487538 to your computer and use it in GitHub Desktop.
Save aaronksaunders/1487538 to your computer and use it in GitHub Desktop.
Stackmob & Appcelerator File Upload with Amazon S3
//include StackMob & credentials module
var credentials = require('credentials').C;
var stackmob = require('stackmob-module.min');
//create StackMob Client
var client = new stackmob.Client(credentials.STACKMOB_APP_NAME, credentials.STACKMOB_PUBLIC_KEY, credentials.STACKMOB_PRIVATE_KEY, credentials.STACKMOB_USER_OBJECT_NAME);
//login the user
client.login({
'username' : "aaron-B1FBD26C-89B1-49E3-B4C4-F319493324DB",
'password' : "password",
success : function(data) {
Ti.API.info("login " + JSON.stringify(data));
uploadImage("aaron-B1FBD26C-89B1-49E3-B4C4-F319493324DB");
},
error : function(data) {
Ti.API.error("ERROR:login " + JSON.stringify(JSON.parse(data.text)));
}
});
/** ----------------------------------------------------------------------
*
* take the file and set it up for getting uploaded to StackMob
*
* this file needs to be uploaded in base64 format for it to work
* correctly
*
---------------------------------------------------------------------- */
var base64FromFile = function(fn) {
var template, content, f;
f = Ti.Filesystem.getFile(fn);
content = f.read();
// for string to pass base64 encoded data
template = "Content-Type: %s\nContent-Disposition: attachment; filename=%s\nContent-Transfer-Encoding: base64\n\n%s"
// this worked for me
if(Titanium.Platform.osname == 'android') {
return String.format(template, content.mimeType, fn, content.toBase64());
} else {
return String.format(template, content.mimeType, fn, Titanium.Utils.base64encode(content));
}
};
/** ----------------------------------------------------------------------
*
* upload and image and associate it to the user, we add the user id
* to the object so we can query for the photos based on the user
* relationship
*
---------------------------------------------------------------------- */
var uploadImage = function(userData) {
var params = {
'data' : base64FromFile("3f34141.jpg"),
'user' : userData,
'caption' : "test caption " + new Date()
};
client.create({
className : 'photo',
params : params,
success : function(data) {
Ti.API.info("create photo " + JSON.stringify(JSON.parse(data.text)));
doPhotoDump(data);
},
error : function(data) {
Ti.API.error("ERROR:create photo " + JSON.stringify(JSON.parse(data.text)));
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment