简体   繁体   English

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

[英]Creating CORS rules for AWS S3 via PHP SDK

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. 我一直在尝试为使用PHP SDK动态创建的存储桶更新CORS。

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). 这是我在发现的各种教程中试图共同攻克的内容:(我不确定的一件事是发送多个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. 致命错误:在验证为PutBucketCors操作提供的输入时,未捕获的异常'InvalidArgumentException'和消息'发现1错误:[CORSConfiguration]必须是一个关联数组。

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 我尝试使用此页面上的信息来提供帮助,但我知道它已经过时了: 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. 我已经通过AWS控制台验证了存储桶是否存在并已创建。 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. 编辑:我能够将我的CORS从现有存储桶中取出,这就是它给我的。 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 ))
    <?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. 您有多个AllowedMethods,没有任何意义,最后一个只是覆盖了前两个。

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 同样在文档中,您共享了它说CORSRules但是来自响应的CORSConfigurations此文档说http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETcors.html

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. 我继续将CORSRules设置为单独的数组,只是为了使代码更具可读性。 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. 从Feyyaz( http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETcors.html )共享的文档中读取,CORSConfiguration充当容器,而CORSRules是其中的关联密钥。

I hope this helps anyone who finds themselves in the same boat I was in. 我希望这对任何发现自己与我所在的船相同的人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM