Having problems uploading blob directly to s3

I have the following code:

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);

When this request goes through I get the following error:

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

I have no idea what I am doing wrong, I generate my blob like so:

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 });
            }

This is my request:

------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--

Answers


Figured out the issue, ordering for the formdata is important, you must follow the correct order in order for the data to post correctly.


Need Your Help

Android NFC prevent sending data to wrong application

android nfc nfc-p2p

I have created one simple application to send NDeF Record and display it in other device.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.