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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?