How to upload a file to S3?

Hi guys,

I’m trying to upload a video capture to my S3 Bucket, but I can’t do it.

Maybe I need to configure my bucket, I already allowed POST / PUT / GET in my CORS configuration. I don’t know if my policy need to be the same as the policy of my bucket.

Here my code:

‘use strict’;

app.controller(‘VideoCtrl’, function($scope, $cordovaCapture, $state, $cordovaFileTransfer) {

$scope.record = function(){
var options = { limit: 1, duration: 8 };
$cordovaCapture.captureVideo(options).then(
function(videoData) {
var i, path, len;
var pathtogo;
var pathtogostring;
for (i = 0, len = videoData.length; i < len; i += 1) {
path = videoData[i].fullPath;
pathtogo = path.toString();
$scope.videos.$add(pathtogo);
};
},
$state.go(‘tab.photo-detail’),

  function(err) {
  }

);
var options = new FileUploadOptions();
options.fileKey=“file”;
var fileName = “capturedvideo.MOV”;
options.fileName = fileName;
options.mimeType =“video/mov”;
options.chunkedMode = false;

  var uri = encodeURI("https://<my bucket>.amazonaws.com/");
  var policyDoc = "js/signing-util.js";
  var signature = "js/signing-util.js";
  var params = {
    "key": "file",
    "AWSAccessKeyId": "<MY AWS Key>",
    "acl": "public-read",
    "policy": policyDoc,
    "signature": signature,
    "Content-Type": "video/mov"
  };
  options.params = params;
  $cordovaFileTransfer.upload(videoData[i].fullPath, uri, options)
            .then(function(result) {
                console.log("SUCCESS: " + JSON.stringify(result.response));
            }, function(err) {
                console.log("ERROR: " + JSON.stringify(err));
            }, function(progress) {
                // constant progress updates
            });

};
});

My signature / policy doc is this:

var crypto = require(‘crypto’),
secret = “”,
policy,
policyBase64,
signature;

policy = {
“expiration”: “2050-12-31T12:00:00.000Z”,
“conditions”: [
{“bucket”: “”},
[“starts-with”, “$key”, “”],
{“acl”: ‘public-read’},
[“starts-with”, “$Content-Type”, “”],
[“content-length-range”, 0, 5242880000]
]
};

policyBase64 = new Buffer(JSON.stringify(policy), ‘utf8’).toString(‘base64’);
console.log(“Policy Base64:”);
console.log(policyBase64);

signature = crypto.createHmac(‘sha1’, secret).update(policyBase64).digest(‘base64’);
console.log(“Signature:”);
console.log(signature);

is something wrong with my code?

Thank for your patience guys.