diff --git a/sample/deleteObjectTagging.php b/sample/deleteObjectTagging.php new file mode 100644 index 00000000..df6a2292 --- /dev/null +++ b/sample/deleteObjectTagging.php @@ -0,0 +1,25 @@ + $region, + 'schema' => 'https', //协议头部,默认为http + 'credentials'=> array( + 'secretId' => $secretId , + 'secretKey' => $secretKey))); +try { + $result = $cosClient->deleteObjectTagging(array( + 'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID + 'Key' => 'exampleobject', + )); + // 请求成功 + print_r($result); +} catch (\Exception $e) { + // 请求失败 + echo($e); +} \ No newline at end of file diff --git a/sample/getObjectTagging.php b/sample/getObjectTagging.php new file mode 100644 index 00000000..ad8df15a --- /dev/null +++ b/sample/getObjectTagging.php @@ -0,0 +1,25 @@ + $region, + 'schema' => 'https', //协议头部,默认为http + 'credentials' => array( + 'secretId' => $secretId, + 'secretKey' => $secretKey))); +try { + $result = $cosClient->getObjectTagging(array( + 'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID + 'Key' => 'exampleobject' + )); + // 请求成功 + print_r($result); +} catch (\Exception $e) { + // 请求失败 + echo($e); +} + diff --git a/sample/putObjectTagging.php b/sample/putObjectTagging.php new file mode 100644 index 00000000..6112ca44 --- /dev/null +++ b/sample/putObjectTagging.php @@ -0,0 +1,33 @@ + $region, + 'schema' => 'https', //协议头部,默认为http + 'credentials'=> array( + 'secretId' => $secretId , + 'secretKey' => $secretKey))); +try { + $result = $cosClient->putObjectTagging(array( + 'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID + 'Key' => 'exampleobject', + 'TagSet' => array( + array('Key'=>'key1', + 'Value'=>'value1', + ), + array('Key'=>'key2', + 'Value'=>'value2', + ), + ), + )); + // 请求成功 + print_r($result); +} catch (\Exception $e) { + // 请求失败 + echo "$e\n"; +} \ No newline at end of file diff --git a/src/Qcloud/Cos/Service.php b/src/Qcloud/Cos/Service.php index 3ea33dcd..c1a6f210 100644 --- a/src/Qcloud/Cos/Service.php +++ b/src/Qcloud/Cos/Service.php @@ -637,6 +637,104 @@ public static function getService() { ), ), ), + // 配置对象标签的方法. + 'PutObjectTagging' => array( + 'httpMethod' => 'PUT', + 'uri' => '/{Bucket}{/Key*}?tagging', + 'class' => 'Qcloud\\Cos\\Command', + 'responseClass' => 'PutObjectTaggingOutput', + 'responseType' => 'model', + 'data' => array( + 'xmlRoot' => array( + 'name' => 'Tagging', + ), + 'contentMd5' => true, + ), + 'parameters' => array( + 'Bucket' => array( + 'required' => true, + 'type' => 'string', + 'location' => 'uri', + ), + 'Key' => array( + 'required' => true, + 'type' => 'string', + 'location' => 'uri', + 'minLength' => 1, + 'filters' => array( + 'Qcloud\\Cos\\Client::explodeKey' + ) + ), + 'TagSet' => array( + 'required' => true, + 'type' => 'array', + 'location' => 'xml', + 'items' => array( + 'name' => 'TagRule', + 'type' => 'object', + 'sentAs' => 'Tag', + 'properties' => array( + 'Key' => array( + 'required' => true, + 'type' => 'string', + ), + 'Value' => array( + 'required' => true, + 'type' => 'string', + ), + ), + ), + ), + ), + ), + // 获取对象标签信息的方法 + 'GetObjectTagging' => array( + 'httpMethod' => 'GET', + 'uri' => '/{Bucket}{/Key*}?tagging', + 'class' => 'Qcloud\\Cos\\Command', + 'responseClass' => 'GetObjectTaggingOutput', + 'responseType' => 'model', + 'parameters' => array( + 'Bucket' => array( + 'required' => true, + 'type' => 'string', + 'location' => 'uri', + ), + 'Key' => array( + 'required' => true, + 'type' => 'string', + 'location' => 'uri', + 'minLength' => 1, + 'filters' => array( + 'Qcloud\\Cos\\Client::explodeKey' + ) + ) + ), + ), + // 删除对象标签的方法 + 'DeleteObjectTagging' => array( + 'httpMethod' => 'DELETE', + 'uri' => '/{Bucket}{/Key*}?tagging', + 'class' => 'Qcloud\\Cos\\Command', + 'responseClass' => 'DeleteObjectTaggingOutput', + 'responseType' => 'model', + 'parameters' => array( + 'Bucket' => array( + 'required' => true, + 'type' => 'string', + 'location' => 'uri' + ), + 'Key' => array( + 'required' => true, + 'type' => 'string', + 'location' => 'uri', + 'minLength' => 1, + 'filters' => array( + 'Qcloud\\Cos\\Client::explodeKey' + ) + ) + ) + ), // 下载对象的方法. 'GetObject' => array( 'httpMethod' => 'GET', @@ -1019,7 +1117,7 @@ public static function getService() { ), 'ContentMD5' => array( 'type' => array( - 'string', +// 'string', 'boolean' ), 'location' => 'header', @@ -1134,7 +1232,7 @@ public static function getService() { ), 'ContentMD5' => array( 'type' => array( - 'string', +// 'string', 'boolean' ), 'location' => 'header', @@ -3550,6 +3648,55 @@ public static function getService() { ), ), ), + //设置对象标签 + 'PutObjectTaggingOutput' => array( + 'type' => 'object', + 'additionalProperties' => true, + 'properties' => array( + 'RequestId' => array( + 'location' => 'header', + 'sentAs' => 'x-cos-request-id', + ), + ), + ), + //查询对象标签 + 'GetObjectTaggingOutput' => array( + 'type' => 'object', + 'additionalProperties' => true, + 'properties' => array( + 'TagSet' => array( + 'type' => 'array', + 'location' => 'xml', + 'items' => array( + 'sentAs' => 'Tag', + 'type' => 'object', + 'properties' => array( + 'Key' => array( + 'type' => 'string', + ), + 'Value' => array( + 'type' => 'string', + ), + ), + ), + ), + 'RequestId' => array( + 'location' => 'header', + 'sentAs' => 'x-cos-request-id', + ), + ), + ), + //删除对象标签 + 'DeleteObjectTaggingOutput' => array( + 'type' => 'object', + 'additionalProperties' => true, + 'properties' => array( + 'RequestId' => array( + 'location' => 'header', + 'sentAs' => 'x-cos-request-id' + ) + ) + ), 'GetObjectOutput' => array( 'type' => 'object', 'additionalProperties' => true, diff --git a/src/Qcloud/Cos/Tests/COSTest.php b/src/Qcloud/Cos/Tests/COSTest.php index 3d535c5d..1fb5ee16 100644 --- a/src/Qcloud/Cos/Tests/COSTest.php +++ b/src/Qcloud/Cos/Tests/COSTest.php @@ -10,11 +10,21 @@ class COSTest extends \PHPUnit\Framework\TestCase private $cosClient; private $bucket; private $region; + private $object2; + private $testTaggingKeys; + private $testTaggingValues; protected function setUp(): void { $this->bucket = getenv('COS_BUCKET'); $this->region = getenv('COS_REGION'); $this->bucket2 = "tmp".$this->bucket; + $this->object2 = "key"; + $this->testTaggingKeys = array( + 'key1', 'key2' + ); + $this->testTaggingValues = array( + 'value1', 'value2' + ); $this->cosClient = new Client(array('region' => $this->region, 'credentials' => array( 'secretId' => getenv('COS_KEY'), @@ -1797,4 +1807,136 @@ public function testSelectObjectContent() } } + /* + * 正常put对象标签 + * 200 + */ + public function testPutObjectTagging() + { + $key = '你好.txt'; + $tagSet = array( + array('Key'=> $this->testTaggingKeys[0], + 'Value'=> $this->testTaggingValues[0], + ), + array('Key'=> $this->testTaggingKeys[1], + 'Value'=> $this->testTaggingValues[1], + ), + ); + try { + $this->cosClient->putObjectTagging(array( + // Bucket is required + 'Bucket' => $this->bucket, + // Key(Object) is required + 'Key' => $key, + // tagging(key-value) is required + 'TagSet' => $tagSet + )); + $rt = $this->cosClient->getObjectTagging(array( + // Bucket is required + 'Bucket' => $this->bucket, + // Key(Object) is required + 'Key' => $key + )); + $this->assertEquals($rt['TagSet'], $tagSet); + $this->assertTrue(True); + } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); + } + } + + /* + * 正常get对象标签 + * 200 + */ + public function testGetObjectTagging() + { + $key = '你好.txt'; + $tagSet = array( + array('Key'=> $this->testTaggingKeys[0], + 'Value'=> $this->testTaggingValues[0], + ), + array('Key'=> $this->testTaggingKeys[1], + 'Value'=> $this->testTaggingValues[1], + ), + ); + try { + $this->cosClient->putObjectTagging(array( + 'Bucket' => $this->bucket, + 'Key' => $key, + 'TagSet' => $tagSet + )); + $this->cosClient->getObjectTagging(array( + // Bucket is required + 'Bucket' => $this->bucket, + // Key(Object) is required + 'Key' => $key + )); + $this->assertTrue(True); + } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); + } + } + + /* + * 正常delete对象标签 + * 200 + */ + public function testDeleteObjectTagging() + { + $key = '你好.txt'; + $tagSet = array( + array('Key'=> $this->testTaggingKeys[0], + 'Value'=> $this->testTaggingValues[0], + ), + array('Key'=> $this->testTaggingKeys[1], + 'Value'=> $this->testTaggingValues[1], + ), + ); + try { + $this->cosClient->putObjectTagging(array( + 'Bucket' => $this->bucket, + 'Key' => $key, + 'TagSet' => $tagSet + )); + $this->cosClient->deleteObjectTagging(array( + 'Bucket' => $this->bucket, + 'Key' => $key + )); + $this->assertTrue(True); + } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); + } + } + + /* + * get object tagging,object不存在 + * NoSuchKey + * 404 + */ + public function testPutObjectTaggingObjectNonExisted() + { + $tagSet = array( + array('Key'=> $this->testTaggingKeys[0], + 'Value'=> $this->testTaggingValues[0], + ), + array('Key'=> $this->testTaggingKeys[1], + 'Value'=> $this->testTaggingValues[1], + ), + ); + try { + $this->cosClient->putObjectTagging( + array( + 'Bucket' => $this->bucket, + 'Key' => $this->object2, + 'TagSet' => $tagSet + ) + ); + $this->assertTrue(false); + } catch (ServiceResponseException $e) { + $this->assertTrue($e->getExceptionCode() === 'NoSuchKey' && $e->getStatusCode() === 404); + } + } }