Skip to content

Instantly share code, notes, and snippets.

@serargz
Created May 14, 2013 22:37
Show Gist options
  • Save serargz/5580240 to your computer and use it in GitHub Desktop.
Save serargz/5580240 to your computer and use it in GitHub Desktop.
Amazon s3 direct upload with Cordova (Phonegap)
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);
}
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