允许AJAX从Amazon S3 GET的? (访问控制 - 允许 - 产地)产地、访问控制、Amazon、AJAX

2023-09-11 08:50:21 作者:最后空手而归

我存储在Amazon S3中JSON对象,我想在Javascript中直接加载数据S3。我的GET看起来pretty的通用的:

I'm storing JSON objects in Amazon S3, and I'd like to load that data directly from S3 from Javascript. My GET looks pretty generic:

$.ajax({
    'type':'GET',
    'url':'http://s3.amazonaws.com/mybucketname/'+id,
    'dataType':'text',
    'success':function(msg) {
        alert(msg);
    }
});

我收到以下错误:

I get the following error:

XMLHttpRequest cannot load http://s3.amazonaws.com/whatever/whatever. Origin http://mylocalhostname:9000 is not allowed by Access-Control-Allow-Origin.

我可以使用curl,或者从我的浏览器导航到那里直接得到S3的URL。难道我真的要通过自己的服务器上有代理所有这些要求?

I can get that URL from S3 using curl, or by navigating there directly from my browser. Am I really going to have to proxy all of these requests through my own servers?

推荐答案

S3现在支持使用CORS文件跨域请求

S3 now supports Cross Domain Requests using CORS file

您可以在这里找到更多的信息

You can find more information here

http://docs.amazonwebservices.com/AmazonS3/latest/dev/cors.html#how-do-i-enable-cors

http://aws.typepad.com/aws/2012/08/amazon-s3-cross-origin-resource-sharing.html