将blob直接上传到s3时遇到问题

时间:2013-05-03 00:34:58

标签: javascript html5 amazon-s3

我有以下代码:

var fd = new FormData();

        var key = "events/" + (new Date).getTime() + '-';

        fd.append('key', key);
        fd.append('acl', Acl);
        fd.append('Content-Type', "image/jpeg");
        fd.append('AWSAccessKeyId', AWSAccessKeyId);
        fd.append('policy', Policy);
        fd.append('name', "Policy13492345");
        fd.append('success_action_status', "201");
        fd.append('signature', Signature);          
        fd.append("file",  blob);
        fd.append("filename", fileName + ".jpg");
        var xhr = new XMLHttpRequest();



        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);

        xhr.open('POST', 'https://s3.amazonaws.com/' + Bucket + '/', true); 

        xhr.send(fd);

当此请求通过时,我收到以下错误:

<Code>AccessDenied</Code><Message>Invalid according to Policy: Policy Condition failed: ["starts-with", "$Filename", ""]</Message>

我不知道我做错了什么,我生成了这样的blob:

function dataURItoBlob(dataURI) {
                var binary = atob(dataURI.split(',')[1]);
                var array = [];
                for (var i = 0; i < binary.length; i++) {
                    array.push(binary.charCodeAt(i));
                }
                var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
                return new Blob([new Uint8Array(array)], { type: mimeString });
            }

这是我的要求:

------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="key"

events/1367541109750-
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="acl"

private
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="Content-Type"

image/jpeg
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="AWSAccessKeyId"

asdfasdfFASDFSDFAADSFHHVDQ
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="policy"

FsnY29udFuZ2UnLCAwLCAxMDAwMDAwMDBdLAogICAgICasdfasdfAgIFsgJ3N0YXJ0cy13aXRoJywgJyRrZXknLCAnJyBdLAogICAgICAgIFsgJ3N0YXJ0cy13aXRoJywgJyRDb250ZW50LVR5cGUnLCAnasdfJyBdLAo
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="name"

Policy134722343242345
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="success_action_status"

201
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="signature"

basdfasdftwa/9asdfasdfx3/zasdfadsft6g=
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: image/jpeg


------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="filename"

C:\fakepath\495845894.jpg
------WebKitFormBoundaryxh8thnHAmDhZQuXE--

1 个答案:

答案 0 :(得分:7)

想出问题,formdata的排序非常重要,您必须遵循正确的顺序才能使数据正确发布。