简体   繁体   English

Cordova / Phonegap通过javascript将图像上传到Amazon S3

[英]Cordova/Phonegap upload image to amazon S3 through javascript

Please help me on this one. 请帮我这个。 I can't seem to find what the problem is. 我似乎找不到问题所在。 It looks like it goes all the way (fetching image, policy, signature etc.) until where it uploads the file. 看起来它一直都在运行(获取图像,策略,签名等),直到将其上传到文件中为止。 It does get rejected by Amazon... any idea why? 它确实遭到了亚马逊的拒绝...为什么知道呢? Here's my upload code: 这是我的上传代码:

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
}

So in those alert messages I can see the policy,signature,imageURI and S3URI with their values but is still throwing the alert "rejected" message. 因此,在那些警报消息中,我可以看到策略,签名,imageURI和S3URI及其值,但仍在抛出警报“已拒绝”消息。

Is this because my policy or signature is encoded wrong? 这是因为我的策略或签名编码错误吗? or do i need to setup something on my gloportal bucket in Amazon S3 to accept it? 还是我需要在Amazon S3的全局存储桶中设置某些东西才能接受它? Or some way amazon can tell me why they reject it? 还是亚马逊可以告诉我为什么他们拒绝它的某种方式?

Thanks! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM