使用phonegap将图像上传到amazon s3

时间:2014-08-13 10:03:44

标签: javascript cordova amazon-web-services amazon-s3

我正在尝试将图片从我的phonegap应用程序直接上传到s3存储桶。

我正在按照亚马逊文档中的步骤进行操作,但它仍然会返回错误。 亚马逊凭证是对的,我已经检查过了。此外,我已设置CORS权限以接受发布和获取请求。

请帮我做一些帮助。

我的代码是:

this.upload = function(imageURI, fileName) {
var s3URI = encodeURI("http://BUCKET_NAME.s3.amazonaws.com/"),
        awsKey = 'AWS_KEY_HERE',
        acl = "public-read",
        secret = "SECRET_HERE",
        policy,
        policyBase64,
        signature;
policy = {
    "expiration": "2020-12-31T12:00:00.000Z",
    "conditions": [
        {"bucket": "BUCKET_NAME"},
        ["starts-with", "$key", ""],
        {"acl": 'public-read'},
        ["starts-with", "$Content-Type", ""],
        ["content-length-range", 0, 524288000]
    ]
};

policyBase64 = btoa(JSON.stringify(policy));
signature = btoa(CryptoJS.HmacSHA1(policyBase64, secret));
    var 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,
        "Content-Type": "image/jpeg"
    };
    ft.upload(imageURI, s3URI,
            function(e) {
                console.log("success")
            },
            function(e) {
                console.log("error")
            }, options);


}

对于此请求,我的错误回复为SignatureDoesNotMatch

问候。

0 个答案:

没有答案