asafdav / ng-s3upload

Upload to S3 using AngularJS
MIT License
190 stars 83 forks source link

Error in setting headers #28

Closed mayankbatra closed 9 years ago

mayankbatra commented 10 years ago

Hey,

I am getting the following error when the request goes for S3.

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Invalid according to Policy: Policy Condition failed: ["starts-with", "$Content-Length", ""]</Message>
<RequestId>12F4B56163EF2740</RequestId>
<HostId>SHiiZPupIS+0JxFHt2Jj2ksgis/X6cJzUIKeriLUOqtjKRBCZ0jx09bN9HUgLa7F</HostId>
</Error>

The code that I am using is as follows.

div(s3-upload bucket="'BUCKET" dynamic-name="field" ng-model="field.value" s3-upload-options="{getOptionsUri: '/s3options', folder: 'images/'}")

On the server I am using a library to generate the policy. Policy Generator

app.get('/s3options', function(req, res) {

        var p = policy({
                secret: 'FhfMGu32+hqnwtIpgT4WSNlE3ieyN9Vn8bAr+k',
                length: 10 * 1024 * 1024,
                bucket: 'tfiapp',
                key: 'AKIAIDFSRY33SEKOFMBA',
                expires: new Date(Date.now() + 60000),
                acl: 'public-read',
                type: ""               
        }); 
p.key = "AKIAIDFSRY33SEKOFMBA"; console.log("Key:" + p.key);
    res.send(p);
});
asafdav commented 10 years ago

Your server should return a json in this format : { "policy": "XXXX", "signature": "YYY", "key": "ZZZ" }

I think that "signature" attribute is missing

jseppi commented 10 years ago

Another problem is that node-s3-policy sets a Content-Length condition, but ng-s3upload does not include Content-Length in its posted FormData.

asafdav commented 10 years ago

Have you figure it out ?

khakiout commented 10 years ago

@jseppi so the solution should be that ng-s3upload must add a Content-Length property to its FormData? Something like fd.append('Content-Length', length)?

jseppi commented 10 years ago

@khakiout That should work. Or you could remove the Content-Length condition from the policy generated by node-s3-policy.

ravindranpandu commented 8 years ago

@jseppi & @khakiout you both saved my day, was breaking my head on this issue with CordovaFileTransfer plugin and node-s3-policy, was getting this error Invalid according to Policy: Policy Condition failed: ["starts-with", "$Content-Length", ""] now from your solution of appending the ('Content-Length', "") to the body really helped me to fix this issue, thanks a ton :)