Allow AJAX GETs from Amazon S3? (Access-Control-Allow-Origin)

后端 未结 11 1868
灰色年华
灰色年华 2020-12-23 16:34

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\':         


        
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 17:35

    For my case, I want to GET some resources from a bucket "myBucket".

    On the S3 client side, AWS S3 now support JSON to configure CORS policies [ref]

    Bucket Policy:

    [
        {
            "AllowedHeaders": [
                "*",
                "x-amz-*"
            ],
            "AllowedMethods": [
                "GET",
                "HEAD"
            ],
            "AllowedOrigins": [
                "*"
            ],
            "ExposeHeaders": []
        }
    ]
    

    On server-side / client side (JQUERY Ajax)

    $.ajax({
        type: "GET", 
        url: "https://myBucket/myObject",
    });​​​​​​​​​​​​​​​
    

    Try it!, hope it helps!

提交回复
热议问题