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

通过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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题