Created
May 14, 2013 22:37
-
-
Save serargz/5580240 to your computer and use it in GitHub Desktop.
Amazon s3 direct upload with Cordova (Phonegap)
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
function upload_media(imgURI) { | |
var options = new FileUploadOptions(); | |
options.fileKey="file"; | |
var time = new Date().getTime(); | |
var fileName = time+".jpg"; | |
options.fileName = fileName; | |
options.mimeType ="image/jpeg"; | |
options.chunkedMode = false; | |
var uri = encodeURI("http://<your bucket name>.s3.amazonaws.com/"); | |
var policyDoc = '<Base 64 policy doc>'; | |
var signature = '<Generated Signature>'; | |
var params = { | |
"key": "media/"+fileName, | |
"AWSAccessKeyId": "<Your AWS Access Key>", | |
"acl": "public-read", | |
"policy": policyDoc, | |
"signature": signature, | |
"Content-Type": "" | |
}; | |
options.params = params; | |
var ft = new FileTransfer(); | |
ft.upload(imgURI, uri, function() { | |
console.log("Success! :)"); | |
}, | |
function() { | |
console.log("Failure! :("); | |
}, options); | |
} |
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
require 'base64' | |
require 'openssl' | |
require 'digest/sha1' | |
policy_document = IO.read('policy_document') | |
policy = Base64.encode64(policy_document).gsub("\n","") | |
signature = Base64.encode64( | |
OpenSSL::HMAC.digest( | |
OpenSSL::Digest::Digest.new('sha1'), | |
'<Your Secret Key>', policy) | |
).gsub("\n","") | |
puts "Policy: #{policy}" | |
puts "Signature: #{signature}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment