drgwsx8405
2016-01-21 20:36
浏览 145
已采纳

通过PHP SDK为AWS S3创建CORS规则

This has been driving me crazy for several days, and I know that I'm missing something simple. I've been attempting to update CORS for a bucket that I create on the fly using the PHP SDK.

This is what I've attempted to hack together out of the various tutorials that I've found: (one thing I'm not sure about is the prefered method of sending multiple AllowedMethods).

    $result = $s3Client->putBucketCors(array(
        'Bucket' => $bucket,
        'CORSConfiguration' => array(
        array(
            'AllowedOrigins' => array('AllowedOrigin' => '*'),
            'AllowedMethods' => array('AllowedMethods' => 'POST'),
            'AllowedMethods' => array('AllowedMethods' => 'GET'),
            'AllowedMethods' => array('AllowedMethods' => 'PUT'),
        'MaxAgeSeconds' => array('MaxAgeSeconds' => '3000'),
        'AllowedHeader' => array('AllowedHeader' => '*')
        )
    )
));

The above returns the following error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Found 1 error while validating the input provided for the PutBucketCors operation: [CORSConfiguration] must be an associative array.

I've tried to use the information from this page to help, but I know it is out of date: http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_putBucketCors

I've verified that the bucket exists and is created, through the AWS console. Any help would be greatly appreciated. Thank you!

Edit: I was able to pull my CORS off of an existing bucket, and this is what it gave me. Still trying to figure out how to set up my arrays to create:

 data:Aws\Result:private] => Array ( 

[CORSRules] => Array ( [0] =>


 Array ( [AllowedHeaders] => Array ( [0] => * )

 [AllowedMethods] => Array ( [0] => GET [1] => POST [2] => PUT )
 [AllowedOrigins] => Array ( [0] => * )[
 MaxAgeSeconds] => 3000 ))
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dousou1967 2016-01-22 03:11
    已采纳

    Ok, so I ended up figuring this out after a fair amount of trial and error. I went ahead and set the CORSRules as a seperate array, just to make the code a bit more readable. Here's what I came up with, posting in case anyone's in the same boat as me down the line:

        $cors = array(array(
            'AllowedOrigins' => array('*'),
            'AllowedMethods' => array('POST', 'GET', 'PUT'),
            'MaxAgeSeconds' => 3000,
            'AllowedHeaders' => array('*')
        ));
    
       $result = $s3Client->putBucketCors(array(
            'Bucket' => $bucket,
            'CORSConfiguration' => array('CORSRules' => $cors)
        ));
    

    Reading from the doc shared by Feyyaz ( http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETcors.html ), CORSConfiguration acts as a container, with CORSRules being the associative key inside.

    I hope this helps anyone who finds themselves in the same boat I was in.

    已采纳该答案
    打赏 评论
  • douying0108 2016-01-21 20:47
        <?php
        $result = $s3Client->putBucketCors(array(
            'Bucket' => $bucket,
            'CORSRules' => array(
            array(
                'AllowedOrigins' => array('*'),
                'AllowedMethods' => array('POST', 'GET', 'PUT'),
                'MaxAgeSeconds' => 3000,
                'AllowedHeaders' => array('*')
            )
        )
    ));
    

    It may work could you please try it ? You have multiple AllowedMethods, it doesn't make any sense the last one just overrides the previous two of them.

    Also in doc you shared it says CORSRules but CORSConfigurations coming from response this doc says http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETcors.html

    打赏 评论

相关推荐 更多相似问题