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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程