简体   繁体   中英

Javascript/JQuery AJAX and Amazon S3 GET request. CORS

There was a project up on Heroku that we were using for a proxy to do some calls to our Amazon S3 storage to a bucket. The project has been retired, hence, our calls do not work. We are using a jQuery AJAX request to Amazon passing in our bucket name but of course we get the CORS error in the browser console. I see that Amazon implemented some CORS Header permissions lately but I can't seem to figure out if it is CORS for Amazon to do PULLS and PUTS from or if it is to allow GET requests from a domain.

Our current JS/jQuery call is:

 $.ajax({
    //url:'https://cors-anywhere.herokuapp.com/https://s3.amazonaws.com/MyBucketName',
    url:'https://s3.amazonaws.com/MyBucketName/',
    type: 'GET',
     beforeSend: function(xhr){xhr.setRequestHeader('origin', 'x-requested-with');},
}).done(function(xml) {
 //Show the data on the page
 });

I was trying to use Cloudberry to set the request headers via their interface. But I can't seem to find the combination of it so I was going to go directly to Amazon S3 and set the request GET set up. But again, I don't know if that is to allow GET requests from domains to a bucket or not. I want to lock it to our domain of course to only allow those GET requests. If anyone has any pointers on the Amazon S3 request header JSON to allow GET requests that would be very helpful. OR do I actually need to build a whole proxy to do the CORS request. This is what I thought Amazon implementing CORS on their side was supposed to accomplish.

If anyone else is interested, this is how to solve this issue. Simple solution.

Javascript

$.ajax({
url:'https://s3.amazonaws.com/MyBucketName/',
type: 'GET',
// beforeSend: function(xhr){xhr.setRequestHeader('origin', 'x-requested-with');},// Remove this.  
 }).done(function(xml) {
  //Show the data on the page
  // build HTML list of elements or whatever you want to do
});

Go to this page: https://docs.aws.amazon.com/AmazonS3/latest/userguide/enabling-cors-examples.html

On step 5 from the user guide in your Amazon S3 console enter this JSON:

[
 {
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "GET"
    ],
    "AllowedOrigins": [
        "https://www.yourdomain.com"
    ],
    "ExposeHeaders": []
  }
 ]

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