Cordova / Phonegap通过javascript将图像上传到亚马逊S3

时间:2014-11-27 08:37:06

标签: javascript cordova amazon-s3 amazon

请帮我解决这个问题。我似乎无法找到问题所在。看起来它一直都是(获取图像,策略,签名等),直到它上传文件。它确实被亚马逊拒绝了...任何想法为什么?这是我的上传代码:

POLICY_JSON = { "expiration": "2020-08-06T12:00:00.000Z",
          "conditions": [
                         {"bucket": "gloportalmobile"},
                         {"acl": "public-read"}                   
                       ]
                     };
var policyEncBase64 = btoa(JSON.stringify(POLICY_JSON));
var secret = "--My--Secret--Key--Here--";
var encodedSignature = b64_hmac_sha1(secret, policyEncBase64);
b64_hmac_sha1(secret, policyEncBase64);
  var s3URI = encodeURI("https://gloportalmobile.s3.amazonaws.com/"),
      policyBase64 = policyEncBase64,
      signature = encodedSignature,
      awsKey = 'My--AWS--Key--here--',
      acl = "public-read";

function upload(imageURI, fileName) {


    var deferred = $.Deferred(),
        ft = new FileTransfer(),
        options = new FileUploadOptions();

    options.fileKey = "file";
    options.fileName = fileName;
    options.mimeType = "image/jpeg";
    options.chunkedMode = false;
    options.params = {
        "key": fileName,
        "AWSAccessKeyId": awsKey,
        "acl": acl,
        "policy": policyBase64,
        "signature": signature
    };
    alert('encodedPolicy: '+policyBase64);
    alert('encodedSignature: '+signature);
    alert('imageURI '+imageURI);
    alert('s3URI '+s3URI);
    ft.upload(imageURI, s3URI,
        function (e) {
        alert('cool');
            deferred.resolve(e);
        },
        function (e) {
            alert('rejected');
            deferred.reject(e);             
        }, options);

    return deferred.promise();

}

return {
    upload: upload
}

因此,在那些警报消息中,我可以看到策略,签名,imageURI和S3URI及其值,但仍然会抛出警告“被拒绝”消息。

这是因为我的政策或签名编码错了吗?或者我是否需要在Amazon S3的gloportal桶上设置一些东西来接受它?或者某种方式亚马逊可以告诉我他们为什么拒绝它?

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为它应该是options.headers而不是options.params