简体   繁体   中英

AWS - CORS not working to upload file directly to S3

I'm trying to upload a file directly to S3 using jquery.

I followed this steps to configure the CORS settings file as follow to test:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Then my jquery code:

$.ajax({
url: this.action,
type: 'POST',
dataType: 'xml',
data: $('form').serialize(),
crossDomain: true,
cache: false,
success: function(data) {
    window.alert('test');
    }
});

If I post the form directly, with no ajax, it works fine... but as I want to call a method just after the upload be completed I'm using jquery to do this job. But even after setup the CORS settings I still get the error: Origin xxxx is not allowed by Access-Control-Allow-Origin.

The request header:

Accept:application/xml, text/xml, */*; q=0.01
Cache-Control:no-cache
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Origin:http://localhost:8000
Pragma:no-cache
Referer:http://localhost:8000/upload
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17

The request status is CANCELED and the type is PENDING.

You are missing the POLICY_JSON and respective Signature to prove that the origin form was really build by the bucket owner. Check this link for details:

http://aws.amazon.com/articles/1434/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM