Last active
January 6, 2016 17:55
-
-
Save ashusath/25a9ec31e55d99b77c68 to your computer and use it in GitHub Desktop.
Simple module to upload image from Parse.com cloud code to Amazon S3
This file contains hidden or 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
/* | |
* Thanks to icaliman for aws signer | |
* you can download aws signer from https://github.com/icaliman/parse-aws-sign | |
*/ | |
var image = request.object.get('image');//base 64 encoded image | |
Parse.Cloud.run('awsUpload', { image: image }, { | |
success: function(resp) { | |
console.log('aws success url'+resp); | |
request.object.set("snappImageUrl" ,resp);//save image to object | |
response.success(); | |
}, | |
error: function(error) { | |
console.log('aws error'+error); | |
response.success(); | |
} | |
}); | |
//========== Cloud Function For amazon S3 Upload =================// | |
Parse.Cloud.define("awsUpload", function(request, response) { | |
var Buffer = require('buffer').Buffer; | |
var AwsSign = require('cloud/modules/aws-sign.js');//include aws signer in modules folder | |
var signer = new AwsSign({ | |
accessKeyId: 'AWS ACCESS KEY', | |
secretAccessKey: 'AWS SECRET ACCESS KEY' | |
}); | |
buf = new Buffer(request.params.image,'base64'); | |
var d = new Date(); | |
var imageName = 'IMG_'+d.getFullYear()+d.getMonth()+d.getDate()+'_'+d.getHours()+d.getMinutes()+d.getSeconds(); | |
var imageUrl = "https://BUCKETNAME.s3.amazonaws.com/FOLDERNAME/"+imageName; | |
Parse.Cloud.httpRequest(signer.sign({ | |
method: 'PUT', | |
url: imageUrl, | |
headers: { | |
'x-amz-acl': 'public-read', | |
'Content-Type':'image' | |
}, | |
// Other request options, ignored by AwsSign. | |
body: buf, | |
success: function(httpResponse) { | |
//console.log('uploaded'+JSON.stringify(httpResponse)); | |
console.log('uploaded url'+imageUrl); | |
response.success(imageUrl); | |
}, | |
error: function(httpResponse) { | |
console.error('Request failed for upload code ' + JSON.stringify(httpResponse)); | |
response.error('Request failed for upload code ' + JSON.stringify(httpResponse)); | |
} | |
})); | |
}); | |
//========== Cloud Function For amazon S3 Upload =================// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment