danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.6k forks source link

s3 POST to presigned url with content-type fails #2054

Open wholeinsoul opened 6 years ago

wholeinsoul commented 6 years ago

case 1: FAILED when I use Upload.http like this:

var header = data.fields; // data contains presigned_url information
 header['Content-Type'] = file.type;
 upload = Upload.http({
                       url: data.url,
                       method: 'POST',
                       headers: header,
                        data: file,
                     });

And I get error 412: Precondition Failed. Bucket POST must be of the enclosure-type multipart/form-data.

case 2: FAILED And when I use Upload.upload like this:

 data.fields.file = file;
 data.fields['Content-Type'] = file.type;
  upload = Upload.upload({
                        url: data.url,
                        method: 'POST',
                        data: data.fields,
                        file: file
    });

I get error 403: Invalid according to Policy: Policy Condition failed: ["eq", "$Content-Type", "application/pdf"]

case 3: SUCCESS upload but incorrect content-type Upload.load without content-type works but then in s3 it does not have the right content-type.

upload = Upload.upload({
                        url: data.url,
                        method: 'POST',
                        data: data.fields,
                        file: file
    });

I'm uploading a pdf file (application/pdf)

'conditions': [
                        {"acl": "private"},
                        # ["content-length-range", 1024, 268435456],
                        {'success_action_status': '201'},
                        ['eq', '$Content-Type', 'application/pdf'],
                        # ['starts-with', '$Content-Type', ''], // tried this too
                    ]

presigned url is generated using: boto3.client.generate_presigned_post

<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader> // it used to be Authorization earlier
</CORSRule>
</CORSConfiguration>

Any pointers ?