/**
  * 从xml数据中解析出CorsConfig
  *
  * @param string $strXml
  * @throws OssException
  * @return null
  */
 public function parseFromXml($strXml)
 {
     $xml = simplexml_load_string($strXml);
     if (!isset($xml->CORSRule)) {
         return;
     }
     foreach ($xml->CORSRule as $rule) {
         $corsRule = new CorsRule();
         foreach ($rule as $key => $value) {
             if ($key === self::OSS_CORS_ALLOWED_HEADER) {
                 $corsRule->addAllowedHeader(strval($value));
             } elseif ($key === self::OSS_CORS_ALLOWED_METHOD) {
                 $corsRule->addAllowedMethod(strval($value));
             } elseif ($key === self::OSS_CORS_ALLOWED_ORIGIN) {
                 $corsRule->addAllowedOrigin(strval($value));
             } elseif ($key === self::OSS_CORS_EXPOSE_HEADER) {
                 $corsRule->addExposeHeader(strval($value));
             } elseif ($key === self::OSS_CORS_MAX_AGE_SECONDS) {
                 $corsRule->setMaxAgeSeconds(strval($value));
             }
         }
         $this->addRule($corsRule);
     }
     return;
 }
/**
 * 设置bucket的cors配置
 *
 * @param OssClient $ossClient OssClient实例
 * @param string $bucket 存储空间名称
 * @return null
 */
function putBucketCors($ossClient, $bucket)
{
    $corsConfig = new CorsConfig();
    $rule = new CorsRule();
    $rule->addAllowedHeader("x-oss-header");
    $rule->addAllowedOrigin("http://www.b.com");
    $rule->addAllowedMethod("POST");
    $rule->setMaxAgeSeconds(10);
    $corsConfig->addRule($rule);
    try {
        $ossClient->putBucketCors($bucket, $corsConfig);
    } catch (OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    print __FUNCTION__ . ": OK" . "\n";
}
Example #3
0
 public function testCreateCorsConfigFromScratch()
 {
     $corsConfig = new CorsConfig();
     $rule = new CorsRule();
     $rule->addAllowedHeader("x-oss-test");
     $rule->addAllowedHeader("x-oss-test2");
     $rule->addAllowedHeader("x-oss-test2");
     $rule->addAllowedHeader("x-oss-test3");
     $rule->addAllowedOrigin("http://www.b.com");
     $rule->addAllowedOrigin("http://www.a.com");
     $rule->addAllowedOrigin("http://www.a.com");
     $rule->addAllowedMethod("GET");
     $rule->addAllowedMethod("PUT");
     $rule->addAllowedMethod("POST");
     $rule->addExposeHeader("x-oss-test1");
     $rule->addExposeHeader("x-oss-test1");
     $rule->addExposeHeader("x-oss-test2");
     $rule->setMaxAgeSeconds(10);
     $corsConfig->addRule($rule);
     $this->assertEquals($this->cleanXml($this->validXml2), $this->cleanXml($corsConfig->serializeToXml()));
     $this->assertEquals($this->cleanXml($this->validXml2), $this->cleanXml(strval($corsConfig)));
 }
Example #4
-1
 public function testBucket()
 {
     $corsConfig = new CorsConfig();
     $rule = new CorsRule();
     $rule->addAllowedHeader("x-oss-test");
     $rule->addAllowedHeader("x-oss-test2");
     $rule->addAllowedHeader("x-oss-test2");
     $rule->addAllowedHeader("x-oss-test3");
     $rule->addAllowedOrigin("http://www.b.com");
     $rule->addAllowedOrigin("http://www.a.com");
     $rule->addAllowedOrigin("http://www.a.com");
     $rule->addAllowedMethod("GET");
     $rule->addAllowedMethod("PUT");
     $rule->addAllowedMethod("POST");
     $rule->addExposeHeader("x-oss-test1");
     $rule->addExposeHeader("x-oss-test1");
     $rule->addExposeHeader("x-oss-test2");
     $rule->setMaxAgeSeconds(10);
     $corsConfig->addRule($rule);
     $rule = new CorsRule();
     $rule->addAllowedHeader("x-oss-test");
     $rule->addAllowedMethod("GET");
     $rule->addAllowedOrigin("http://www.b.com");
     $rule->addExposeHeader("x-oss-test1");
     $rule->setMaxAgeSeconds(110);
     $corsConfig->addRule($rule);
     try {
         $this->ossClient->putBucketCors($this->bucket, $corsConfig);
     } catch (OssException $e) {
         $this->assertFalse(True);
     }
     try {
         Common::waitMetaSync();
         $object = "cors/test.txt";
         $this->ossClient->putObject($this->bucket, $object, file_get_contents(__FILE__));
         $headers = $this->ossClient->optionsObject($this->bucket, $object, "http://www.a.com", "GET", "", null);
         $this->assertNotEmpty($headers);
     } catch (OssException $e) {
         var_dump($e->getMessage());
     }
     try {
         Common::waitMetaSync();
         $corsConfig2 = $this->ossClient->getBucketCors($this->bucket);
         $this->assertNotNull($corsConfig2);
         $this->assertEquals($corsConfig->serializeToXml(), $corsConfig2->serializeToXml());
     } catch (OssException $e) {
         $this->assertFalse(True);
     }
     try {
         Common::waitMetaSync();
         $this->ossClient->deleteBucketCors($this->bucket);
     } catch (OssException $e) {
         $this->assertFalse(True);
     }
     try {
         Common::waitMetaSync();
         $corsConfig3 = $this->ossClient->getBucketCors($this->bucket);
         $this->assertNotNull($corsConfig3);
         $this->assertNotEquals($corsConfig->serializeToXml(), $corsConfig3->serializeToXml());
     } catch (OssException $e) {
         $this->assertFalse(True);
     }
 }