diff --git a/samples/BucketInventory.php b/samples/BucketInventory.php new file mode 100644 index 00000000..e99faad6 --- /dev/null +++ b/samples/BucketInventory.php @@ -0,0 +1,359 @@ +'; +$roleArn = ''; +$bucketName = 'acs:oss:::destbucket'; +$prefix = 'prefix'; +$configDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucketName,$prefix); +// If you need to use kms encryption list, please refer to the following settings +//$kmsId = "kms_key_id"; +//$ossBucketDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucketName,$prefix,null,$kmsId); +// If you need to use the OSS server encryption list, please refer to the following settings +//$ossId = "oss_key_id"; +//$ossBucketDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucketName,$prefix,$ossId); + +// Set inventory configuration +$inventoryConfig = new InventoryConfig($id,$isEnabled,$frequency,$version,$configDestination,$configFilter,$files); +$ossClient->putBucketInventory($bucket,$inventoryConfig); +Common::println("bucket $bucket Inventory created:" . $inventoryConfig->serializeToXml()); + + +// Get inventory configuration + +$inventoryConfigId = 'report2'; +$result = $ossClient->getBucketInventory($bucket,$inventoryConfigId); +Common::println("===Inventory configuration==="); +Common::println("Inventory Id: ".$result->getId()); +Common::println("Is Enabled: ".$result->getIsEnabled()); +Common::println("Included Versions: ".$result->getIncludedObjectVersions()); +Common::println("Schedule Frequency: ".$result->getSchedule()); + +if ($result->getFilter()){ + Common::println("Filter Prefix: ".$result->getFilter()->getPrefix()); + Common::println("Filter Last Modify Begin Time Stamp: ".$result->getFilter()->getLastModifyBeginTimeStamp()); + Common::println("Filter Last Modify End Time Stamp: ".$result->getFilter()->getLastModifyEndTimeStamp()); + Common::println("Filter Lower Size Bound: ".$result->getFilter()->getLowerSizeBound()); + Common::println("Filter Upper Size Bound: ".$result->getFilter()->getUpperSizeBound()); + Common::println("Filter Storage Class: ".$result->getFilter()->getStorageClass()); +} + +if ($result->getOptionalFields()){ + foreach ($result->getOptionalFields() as $field){ + Common::println("Optional Fields Filed: ".$field->getFiled()); + } +} +Common::println("===bucket destination config==="); + +if ($result->getDestination()){ + Common::println("OSS Bucket Destination Format: ".$result->getDestination()->getFormat()); + Common::println("OSS Bucket Destination AccountId: ".$result->getDestination()->getAccountId()); + Common::println("OSS Bucket Destination Role Arn: ".$result->getDestination()->getRoleArn()); + Common::println("OSS Bucket Destination Bucket: ".$result->getDestination()->getBucket()); + Common::println("OSS Bucket Destination Prefix: ".$result->getDestination()->getPrefix()); + + if ($result->getDestination()->getOssId()){ + Common::println("Server Side Encryption OSS Key Id: ".$result->getDestination()->getOssId()); + } + if ($result->getDestination()->getKmsId()){ + Common::println("Server Side Encryption Kms Key Id: ".$result->getDestination()->getKmsId()); + } +} + + +// list inventory configuration + +$option = array( + OssClient::OSS_CONTINUATION_TOKEN => null +); +$bool = true; +while ($bool) { + $list = $ossClient->listBucketInventory($bucket); + Common::println("=======List bucket inventory configuration======="); + Common::println("Is Truncated: " . $list->getIsTruncated()); + Common::println("Next Continuation Token: " . $list->getNextContinuationToken()); + foreach ($list->getInventoryList() as $key => $result) { + Common::println("Inventory Id: " . $result->getId()); + Common::println("Is Enabled: " . $result->getIsEnabled()); + Common::println("Included Versions: " . $result->getIncludedObjectVersions()); + Common::println("Schedule Frequency: " . $result->getSchedule()); + + if ($result->getFilter()) { + Common::println("Filter Prefix: " . $result->getFilter()->getPrefix()); + Common::println("Filter Last Modify Begin Time Stamp: " . $result->getFilter()->getLastModifyBeginTimeStamp()); + Common::println("Filter Last Modify End Time Stamp: " . $result->getFilter()->getLastModifyEndTimeStamp()); + Common::println("Filter Lower Size Bound: " . $result->getFilter()->getLowerSizeBound()); + Common::println("Filter Upper Size Bound: " . $result->getFilter()->getUpperSizeBound()); + Common::println("Filter Storage Class: " . $result->getFilter()->getStorageClass()); + } + + if ($result->getOptionalFields()) { + foreach ($result->getOptionalFields() as $field) { + Common::println("Optional Fields Filed: " . $field->getFiled()); + } + } + Common::println("===bucket destination config==="); + + if ($result->getDestination()) { + Common::println("OSS Bucket Destination Format: " . $result->getDestination()->getFormat()); + Common::println("OSS Bucket Destination AccountId: " . $result->getDestination()->getAccountId()); + Common::println("OSS Bucket Destination Role Arn: " . $result->getDestination()->getRoleArn()); + Common::println("OSS Bucket Destination Bucket: " . $result->getDestination()->getBucket()); + Common::println("OSS Bucket Destination Prefix: " . $result->getDestination()->getPrefix()); + + if ($result->getDestination()->getOssId()) { + Common::println("Server Side Encryption OSS Key Id: " . $result->getDestination()->getOssId()); + } + if ($result->getDestination()->getKmsId()) { + Common::println("Server Side Encryption Kms Key Id: " . $result->getDestination()->getKmsId()); + } + } + } + if ($list->getIsTruncated() === 'true') { + $option[OssClient::OSS_CONTINUATION_TOKEN] = $list->getNextContinuationToken(); + } else { + $bool = false; + } +} + +// delete inventory configuration +$inventoryConfigId = 'report2'; +$ossClient->deleteBucketInventory($bucket,$inventoryConfigId); +printf('delete Inventory %s Success'. "\n",$inventoryConfigId); + + +putBucketInventory($ossClient,$bucket); +getBucketInventory($ossClient,$bucket); +listBucketInventory($ossClient,$bucket); +deleteBucketInventory($ossClient,$bucket); + + +/** + * put bucket inventory configuration + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + */ +function putBucketInventory($ossClient,$bucket){ + try { + $id = "report2"; + $isEnabled = InventoryConfig::IS_ENABLED_TRUE; + $filterPrefix = "filterPrefix"; + $version = InventoryConfig::OBJECT_VERSION_ALL; + $frequency = InventoryConfig::FREQUENCY_DAILY; + $configFilter = new InventoryConfigFilter($filterPrefix); + $files = array( + new InventoryConfigOptionalFields(InventoryConfig::FIELD_SIZE), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_LAST_MODIFIED_DATE), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_IS_MULTIPART_UPLOADED), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_ETAG), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_STORAGE_CLASS), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_ENCRYPTION_STATUS), + ); + + $format = InventoryConfigOssBucketDestination::DEST_FORMAT; + $accountId = ''; + $roleArn = ''; + $bucketName = 'acs:oss:::destbucket'; + $prefix = 'prefix'; + $configDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucketName,$prefix); + $inventoryConfig = new InventoryConfig($id,$isEnabled,$frequency,$version,$configDestination,$configFilter,$files); + $ossClient->putBucketInventory($bucket,$inventoryConfig); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + + +/** + * get bucket inventory configuration + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + * @throws RequestCore_Exception + */ +function getBucketInventory($ossClient,$bucket){ + $inventoryConfigId = 'report2'; + try { + $result = $ossClient->getBucketInventory($bucket,$inventoryConfigId); + Common::println("===Inventory configuration==="); + Common::println("Inventory Id: ".$result->getId()); + Common::println("Is Enabled: ".$result->getIsEnabled()); + Common::println("Included Versions: ".$result->getIncludedObjectVersions()); + Common::println("Schedule Frequency: ".$result->getSchedule()); + + if ($result->getFilter()){ + Common::println("Filter Prefix: ".$result->getFilter()->getPrefix()); + Common::println("Filter Last Modify Begin Time Stamp: ".$result->getFilter()->getLastModifyBeginTimeStamp()); + Common::println("Filter Last Modify End Time Stamp: ".$result->getFilter()->getLastModifyEndTimeStamp()); + Common::println("Filter Lower Size Bound: ".$result->getFilter()->getLowerSizeBound()); + Common::println("Filter Upper Size Bound: ".$result->getFilter()->getUpperSizeBound()); + Common::println("Filter Storage Class: ".$result->getFilter()->getStorageClass()); + } + + if ($result->getOptionalFields()){ + foreach ($result->getOptionalFields() as $field){ + Common::println("Optional Fields Filed: ".$field->getFiled()); + } + } + Common::println("===bucket destination config==="); + + if ($result->getDestination()){ + Common::println("OSS Bucket Destination Format: ".$result->getDestination()->getFormat()); + Common::println("OSS Bucket Destination AccountId: ".$result->getDestination()->getAccountId()); + Common::println("OSS Bucket Destination Role Arn: ".$result->getDestination()->getRoleArn()); + Common::println("OSS Bucket Destination Bucket: ".$result->getDestination()->getBucket()); + Common::println("OSS Bucket Destination Prefix: ".$result->getDestination()->getPrefix()); + + if ($result->getDestination()->getOssId()){ + Common::println("Server Side Encryption OSS Key Id: ".$result->getDestination()->getOssId()); + } + if ($result->getDestination()->getKmsId()){ + Common::println("Server Side Encryption Kms Key Id: ".$result->getDestination()->getKmsId()); + } + } + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + + +/** + * list bucket inventory configuration + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + * @throws RequestCore_Exception + */ +function listBucketInventory($ossClient,$bucket){ + try { + $option = array( + OssClient::OSS_CONTINUATION_TOKEN => null + ); + $bool = true; + while ($bool) { + $list = $ossClient->listBucketInventory($bucket, $option); + Common::println("=======List bucket inventory configuration======="); + Common::println("Is Truncated: " . $list->getIsTruncated()); + Common::println("Next Continuation Token: " . $list->getNextContinuationToken()); + foreach ($list->getInventoryList() as $key => $result) { + Common::println("Inventory Id: " . $result->getId()); + Common::println("Is Enabled: " . $result->getIsEnabled()); + Common::println("Included Versions: " . $result->getIncludedObjectVersions()); + Common::println("Schedule Frequency: " . $result->getSchedule()); + + if ($result->getFilter()) { + Common::println("Filter Prefix: " . $result->getFilter()->getPrefix()); + Common::println("Filter Last Modify Begin Time Stamp: " . $result->getFilter()->getLastModifyBeginTimeStamp()); + Common::println("Filter Last Modify End Time Stamp: " . $result->getFilter()->getLastModifyEndTimeStamp()); + Common::println("Filter Lower Size Bound: " . $result->getFilter()->getLowerSizeBound()); + Common::println("Filter Upper Size Bound: " . $result->getFilter()->getUpperSizeBound()); + Common::println("Filter Storage Class: " . $result->getFilter()->getStorageClass()); + } + + if ($result->getOptionalFields()) { + foreach ($result->getOptionalFields() as $field) { + Common::println("Optional Fields Filed: " . $field->getFiled()); + } + } + Common::println("===bucket destination config==="); + + if ($result->getDestination()) { + Common::println("OSS Bucket Destination Format: " . $result->getDestination()->getFormat()); + Common::println("OSS Bucket Destination AccountId: " . $result->getDestination()->getAccountId()); + Common::println("OSS Bucket Destination Role Arn: " . $result->getDestination()->getRoleArn()); + Common::println("OSS Bucket Destination Bucket: " . $result->getDestination()->getBucket()); + Common::println("OSS Bucket Destination Prefix: " . $result->getDestination()->getPrefix()); + + if ($result->getDestination()->getOssId()) { + Common::println("Server Side Encryption OSS Key Id: " . $result->getDestination()->getOssId()); + } + if ($result->getDestination()->getKmsId()) { + Common::println("Server Side Encryption Kms Key Id: " . $result->getDestination()->getKmsId()); + } + } + } + + if ($list->getIsTruncated() === 'true') { + $option[OssClient::OSS_CONTINUATION_TOKEN] = $list->getNextContinuationToken(); + } else { + $bool = false; + } + } + + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} + + +/** + * delete bucket inventory configuration + * + * @param OssClient $ossClient OssClient instance + * @param string $bucket bucket name + * @return null + */ +function deleteBucketInventory($ossClient,$bucket){ + $inventoryConfigId = 'report2'; + try { + $ossClient->deleteBucketInventory($bucket,$inventoryConfigId); + } catch (OssException $e) { + printf(__FUNCTION__ . ": FAILED\n"); + printf($e->getMessage() . "\n"); + return; + } + print(__FUNCTION__ . ": OK" . "\n"); +} \ No newline at end of file diff --git a/src/OSS/Model/InventoryConfig.php b/src/OSS/Model/InventoryConfig.php new file mode 100644 index 00000000..98e2cc8a --- /dev/null +++ b/src/OSS/Model/InventoryConfig.php @@ -0,0 +1,359 @@ +id = $id; + $this->isEnabled = $isEnabled; + $this->filter = $filter; + $this->destination = $destination; + $this->schedule = $schedule; + $this->includedObjectVersions = $includedObjectVersions; + $this->optionalFields = $optionalFields; + } + + /** + * @param $destination InventoryConfigOssBucketDestination + */ + public function addDestination($destination) + { + $this->destination = $destination; + } + + /** + * @param $filter InventoryConfigFilter + */ + public function addFilter($filter) + { + $this->filter = $filter; + } + + + /** + * @param $optionalFields InventoryConfigOptionalFields[] + */ + public function addOptionalFields($optionalFields){ + $this->optionalFields = $optionalFields; + } + + /** + * @param $schedule string + * Schedule>Frequency + */ + public function addSchedule($schedule) + { + $this->schedule = $schedule; + } + + /** + * @param $includedObjectVersions string + */ + public function addIncludedObjectVersions($includedObjectVersions) + { + $this->includedObjectVersions = $includedObjectVersions; + } + + /** + * Parse the xml into this object. + */ + public function parseFromXml($strXml) + { + $xml = simplexml_load_string($strXml); + $this->parseFromXmlObj($xml); + } + + + /** + * @param $xml \SimpleXMLElement + */ + public function parseFromXmlObj($xml) + { + if (!isset($xml->Id) && !isset($xml->IsEnabled) && !isset($xml->Destination) && !isset($xml->Schedule)&& !isset($xml->Filter)&& !isset($xml->IncludedObjectVersions)&& !isset($xml->OptionalFields)) return; + if (isset($xml->Id)){ + $this->id = strval($xml->Id); + } + if (isset($xml->IsEnabled)){ + $this->isEnabled = strval($xml->IsEnabled); + } + if (isset($xml->Destination)){ + $this->parseDestination($xml->Destination); + } + if (isset($xml->Schedule)){ + $this->parseSchedule($xml->Schedule); + } + if (isset($xml->Filter)){ + $this->parseFilter($xml->Filter); + } + if (isset($xml->IncludedObjectVersions)){ + $this->includedObjectVersions = strval($xml->IncludedObjectVersions); + } + if (isset($xml->OptionalFields)){ + $this->parseOptionalFields($xml->OptionalFields); + } + } + /** + * @param $xmlDestination \SimpleXMLElement + */ + private function parseDestination($xmlDestination){ + if (isset($xmlDestination)){ + $ossBucketDestination = $xmlDestination->OSSBucketDestination; + if ($ossBucketDestination->Format){ + $format = strval($ossBucketDestination->Format); + } + if ($ossBucketDestination->AccountId){ + $accountId = strval($ossBucketDestination->AccountId); + } + if ($ossBucketDestination->RoleArn){ + $roleArn = strval($ossBucketDestination->RoleArn); + } + if ($ossBucketDestination->Bucket){ + $bucket = strval($ossBucketDestination->Bucket); + } + if ($ossBucketDestination->Prefix){ + $prefix = strval($ossBucketDestination->Prefix); + } + if ($ossBucketDestination->Encryption){ + $encryption = $ossBucketDestination->Encryption; + if ($encryption->xpath('SSE-OSS') !== null){ + $tmp = $encryption->xpath('SSE-OSS'); + $ossKeyId = strval($tmp[0]->KeyId); + } + if ($encryption->xpath('SSE-KMS') !== null){ + $tmp = $encryption->xpath('SSE-KMS'); + $kmsKeyId = ($tmp[0]->KeyId); + } + } + + $configOssBucketDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucket,$prefix,$ossKeyId,$kmsKeyId); + $this->addDestination($configOssBucketDestination); + } + } + + /** + * @param $xmlSchedule \SimpleXMLElement + */ + private function parseSchedule($xmlSchedule){ + if (isset($xmlSchedule)){ + if (isset($xmlSchedule->Frequency)){ + $this->addSchedule(strval($xmlSchedule->Frequency)); + } + } + } + + /** + * @param $xmlFilter \SimpleXMLElement + */ + private function parseFilter($xmlFilter){ + if (isset($xmlFilter)){ + if (isset($xmlFilter->Prefix)){ + $prefix = strval($xmlFilter->Prefix); + } + if (isset($xmlFilter->LastModifyBeginTimeStamp)){ + $lastModifyBeginTimeStamp = strval($xmlFilter->LastModifyBeginTimeStamp); + } + if (isset($xmlFilter->LastModifyEndTimeStamp)){ + $lastModifyEndTimeStamp = strval($xmlFilter->LastModifyEndTimeStamp); + } + if (isset($xmlFilter->LowerSizeBound)){ + $lowerSizeBound = strval($xmlFilter->LowerSizeBound); + } + if (isset($xmlFilter->UpperSizeBound)){ + $upperSizeBound = strval($xmlFilter->UpperSizeBound); + } + if (isset($xmlFilter->StorageClass)){ + $storageClass = strval($xmlFilter->StorageClass); + } + $configFilter = new InventoryConfigFilter($prefix,$lastModifyBeginTimeStamp,$lastModifyEndTimeStamp,$lowerSizeBound,$upperSizeBound,$storageClass); + $this->addFilter($configFilter); + } + } + + /** + * @param $xmlOptionalFields \SimpleXMLElement + */ + private function parseOptionalFields($xmlOptionalFields){ + if (isset($xmlOptionalFields)){ + $configFields = array(); + foreach ($xmlOptionalFields->Field as $field){ + $configFields[] = new InventoryConfigOptionalFields(strval($field)); + } + $this->addOptionalFields($configFields); + } + } + + /** + * Serialize the object to xml + * + * @return string + */ + public function serializeToXml() + { + $xml = new \SimpleXMLElement(''); + if (isset($this->id)){ + $xml->addChild("Id",$this->id); + } + if (isset($this->isEnabled)){ + $xml->addChild("IsEnabled",$this->isEnabled); + } + if (isset($this->destination)){ + $xmlDestination = $xml->addChild("Destination"); + $xmlOSSBucketDestination = $xmlDestination->addChild("OSSBucketDestination"); + $this->destination->appendToXml($xmlOSSBucketDestination); + } + if (isset($this->schedule)){ + $xmlSchedule = $xml->addChild("Schedule"); + $xmlSchedule->addChild("Frequency",$this->schedule); + } + if (isset($this->filter)){ + $xmlFilter = $xml->addChild("Filter"); + $this->filter->appendToXml($xmlFilter); + } + if (isset($this->includedObjectVersions)){ + $xml->addChild("IncludedObjectVersions",$this->includedObjectVersions); + } + if (isset($this->optionalFields)){ + $xmlOptionalFields = $xml->addChild("OptionalFields"); + foreach ($this->optionalFields as $field){ + $field->appendToXml($xmlOptionalFields); + } + + } + return $xml->asXML(); + } + + /** + * Serialize the object into xml string. + * + * @return string + */ + public function __toString() + { + return $this->serializeToXml(); + } + + + /** + * @return string|null + */ + public function getId(){ + return $this->id; + } + + /** + * @return string + */ + public function getIsEnabled(){ + return $this->isEnabled; + } + + /** + * @return InventoryConfigOssBucketDestination|null + */ + public function getDestination(){ + return $this->destination; + } + + /** + * @return string + */ + public function getSchedule(){ + return $this->schedule; + } + + /** + * @return InventoryConfigFilter + */ + public function getFilter(){ + return $this->filter; + } + + /** + * @return string + */ + public function getIncludedObjectVersions(){ + return $this->includedObjectVersions; + } + + /** + * @return InventoryConfigOptionalFields[]|null + */ + public function getOptionalFields(){ + return $this->optionalFields; + } +} + + diff --git a/src/OSS/Model/InventoryConfigDestination.php b/src/OSS/Model/InventoryConfigDestination.php new file mode 100644 index 00000000..169d4530 --- /dev/null +++ b/src/OSS/Model/InventoryConfigDestination.php @@ -0,0 +1,42 @@ +ossBucketDestination = $ossBucketDestination; + } + + /** + * @param $xmlDestination \SimpleXMLElement + */ + public function appendToXml(&$xmlDestination){ + if (isset($this->ossBucketDestination)){ + $xmlOSSBucketDestination = $xmlDestination->addChild("OSSBucketDestination"); + $this->ossBucketDestination->appendToXml($xmlOSSBucketDestination); + } + } +} + + diff --git a/src/OSS/Model/InventoryConfigFilter.php b/src/OSS/Model/InventoryConfigFilter.php new file mode 100644 index 00000000..3e6d0a6a --- /dev/null +++ b/src/OSS/Model/InventoryConfigFilter.php @@ -0,0 +1,126 @@ +prefix = $prefix; + $this->lastModifyBeginTimeStamp = $lastModifyBeginTimeStamp; + $this->lastModifyEndTimeStamp = $lastModifyEndTimeStamp; + $this->lowerSizeBound = $lowerSizeBound; + $this->upperSizeBound = $upperSizeBound; + $this->storageClass = $storageClass; + } + + /** + * @return string|null + */ + public function getPrefix(){ + return $this->prefix; + } + + /** + * @return string|null + */ + public function getLastModifyBeginTimeStamp(){ + return $this->lastModifyBeginTimeStamp; + } + + /** + * @return string|null + */ + public function getLastModifyEndTimeStamp(){ + return $this->lastModifyEndTimeStamp; + } + + /** + * @return string|null + */ + public function getLowerSizeBound(){ + return $this->lowerSizeBound; + } + + /** + * @return string|null + */ + public function getUpperSizeBound(){ + return $this->upperSizeBound; + } + + /** + * @return string|null + */ + public function getStorageClass(){ + return $this->storageClass; + } + + + + /** + * @param $xmlFilter \SimpleXMLElement + */ + public function appendToXml(&$xmlFilter){ + if ($this->prefix){ + $xmlFilter->addChild("Prefix",$this->prefix); + } + if ($this->lastModifyBeginTimeStamp){ + $xmlFilter->addChild("LastModifyBeginTimeStamp",$this->lastModifyBeginTimeStamp); + } + if ($this->lastModifyEndTimeStamp){ + $xmlFilter->addChild("LastModifyEndTimeStamp",$this->lastModifyEndTimeStamp); + } + if ($this->lowerSizeBound){ + $xmlFilter->addChild("LowerSizeBound",$this->lowerSizeBound); + } + if ($this->upperSizeBound){ + $xmlFilter->addChild("UpperSizeBound",$this->upperSizeBound); + } + if ($this->storageClass){ + $xmlFilter->addChild("StorageClass",$this->storageClass); + } + } +} + + diff --git a/src/OSS/Model/InventoryConfigOptionalFields.php b/src/OSS/Model/InventoryConfigOptionalFields.php new file mode 100644 index 00000000..bc6382af --- /dev/null +++ b/src/OSS/Model/InventoryConfigOptionalFields.php @@ -0,0 +1,50 @@ +field = $field; + } + + /** + * @return string|null + */ + public function getFiled(){ + return $this->field; + } + + /** + * @param $xmlOptionalFields \SimpleXMLElement + */ + public function appendToXml(&$xmlOptionalFields){ + if (isset($this->field)){ + $xmlOptionalFields->addChild("Field",$this->field); + } + } + + +} + + diff --git a/src/OSS/Model/InventoryConfigOssBucketDestination.php b/src/OSS/Model/InventoryConfigOssBucketDestination.php new file mode 100644 index 00000000..e1455350 --- /dev/null +++ b/src/OSS/Model/InventoryConfigOssBucketDestination.php @@ -0,0 +1,157 @@ +format = $format; + $this->accountId = $accountId; + $this->roleArn = $roleArn; + $this->bucket = $bucket; + $this->prefix = $prefix; + $this->ossKeyId = $ossKeyId; + $this->kmsKeyId = $kmsKeyId; + } + + /** + * @return string|null + */ + public function getFormat(){ + return $this->format; + } + + /** + * @return string|null + */ + public function getAccountId(){ + return $this->accountId; + } + + /** + * @return string|null + */ + public function getPrefix(){ + return $this->prefix; + } + + /** + * @return string|null + */ + public function getRoleArn(){ + return $this->roleArn; + } + + /** + * @return string|null + */ + public function getBucket(){ + return $this->bucket; + } + + /** + * @return string|null + */ + public function getKmsId(){ + return $this->kmsKeyId; + } + + /** + * @return string|null + */ + public function getOssId(){ + return $this->ossKeyId; + } + + /** + * @param $keyId string + */ + public function addKmsId($keyId) + { + $this->kmsKeyId = $keyId; + } + + /** + * @param $keyId string + */ + public function addOssId($keyId) + { + $this->ossKeyId = $keyId; + } + + /** + * @param $xmlOSSBucketDestination \SimpleXMLElement + */ + public function appendToXml(&$xmlOSSBucketDestination){ + if ($this->format){ + $xmlOSSBucketDestination->addChild("Format",$this->format); + } + if ($this->accountId){ + $xmlOSSBucketDestination->addChild("AccountId",$this->accountId); + } + if ($this->roleArn){ + $xmlOSSBucketDestination->addChild("RoleArn",$this->roleArn); + } + if ($this->bucket){ + $xmlOSSBucketDestination->addChild("Bucket",$this->bucket); + } + if ($this->prefix){ + $xmlOSSBucketDestination->addChild("Prefix",$this->prefix); + } + $xmlEncryption = $xmlOSSBucketDestination->addChild("Encryption"); + if ($this->ossKeyId || $this->kmsKeyId){ + if ($this->ossKeyId){ + $xmlSse = $xmlEncryption->addChild("SSE-OSS"); + $xmlSse->addChild("KeyId",$this->ossKeyId); + } + if ($this->kmsKeyId){ + $xmlSse = $xmlEncryption->addChild("SSE-KMS"); + $xmlSse->addChild("KeyId",$this->kmsKeyId); + } + }else{ + $xmlEncryption->addChild("SSE-OSS"); + } + } + +} + + diff --git a/src/OSS/Model/ListInventoryConfig.php b/src/OSS/Model/ListInventoryConfig.php new file mode 100644 index 00000000..7b6d262e --- /dev/null +++ b/src/OSS/Model/ListInventoryConfig.php @@ -0,0 +1,90 @@ +isTruncated; + } + + /** + * get nextContinuationToken + * @return string + */ + public function getNextContinuationToken() + { + return $this->nextContinuationToken; + } + + + /** + * get the inventoryInfo list + * @return InventoryConfig[] + */ + public function getInventoryList() + { + return $this->inventoryList; + } + + + /** + * Parse the xml into this object. + */ + public function parseFromXml($strXml) + { + $xml = simplexml_load_string($strXml); + if (!isset($xml->IsTruncated) && !isset($xml->NextContinuationToken) && !isset($xml->InventoryConfiguration) && !isset($xml->Schedule)&& !isset($xml->Filter)&& !isset($xml->IncludedObjectVersions)&& !isset($xml->OptionalFields)) return; + if (isset($xml->IsTruncated)){ + $this->isTruncated = strval($xml->IsTruncated); + } + if (isset($xml->NextContinuationToken)){ + $this->nextContinuationToken = strval($xml->NextContinuationToken); + } + if (isset($xml->InventoryConfiguration)){ + $this->parseInventoryConfig($xml->InventoryConfiguration); + } + } + + + /** + * @param $xmlInventoryConfiguration + */ + private function parseInventoryConfig($xmlInventoryConfiguration){ + if ($xmlInventoryConfiguration){ + foreach ($xmlInventoryConfiguration as $config){ + $inventoryConfiguration = new InventoryConfig(); + $inventoryConfiguration->parseFromXmlObj($config); + $this->inventoryList[] = $inventoryConfiguration; + } + } + } + +} \ No newline at end of file diff --git a/src/OSS/OssClient.php b/src/OSS/OssClient.php index 0922a0b7..984a54c0 100644 --- a/src/OSS/OssClient.php +++ b/src/OSS/OssClient.php @@ -8,6 +8,8 @@ use OSS\Http\ResponseCore; use OSS\Model\CorsConfig; use OSS\Model\CnameConfig; +use OSS\Model\InventoryConfig; +use OSS\Model\ListInventoryConfig; use OSS\Model\LoggingConfig; use OSS\Model\LiveChannelConfig; use OSS\Model\LiveChannelInfo; @@ -15,6 +17,7 @@ use OSS\Model\StorageCapacityConfig; use OSS\Result\AclResult; use OSS\Result\BodyResult; +use OSS\Result\GetBucketInventoryResult; use OSS\Result\GetCorsResult; use OSS\Result\GetLifecycleResult; use OSS\Result\GetLocationResult; @@ -25,6 +28,7 @@ use OSS\Result\GetCnameResult; use OSS\Result\HeaderResult; use OSS\Result\InitiateMultipartUploadResult; +use OSS\Result\ListBucketInventoryResult; use OSS\Result\ListBucketsResult; use OSS\Result\ListMultipartUploadResult; use OSS\Model\ListMultipartUploadInfo; @@ -1493,6 +1497,94 @@ public function getBucketWorm($bucket, $options = NULL) return $result->getData(); } + + /** + * Create Bucket Inventory + * @param string $bucket bucket name + * @param InventoryConfig $inventoryConfig + * @param null $options + * @return null + * @throws OssException|RequestCore_Exception + */ + public function putBucketInventory($bucket, $inventoryConfig, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_PUT; + $options[self::OSS_OBJECT] = '/'; + $options[self::OSS_SUB_RESOURCE] = 'inventory&inventoryId='.$inventoryConfig->getId(); + $options[self::OSS_CONTENT_TYPE] = 'application/xml'; + $options[self::OSS_CONTENT] = $inventoryConfig->serializeToXml(); + $response = $this->auth($options); + $result = new HeaderResult($response); + return $result->getData(); + } + + + /** + * Get Inventory by InventoryId + * @param string $bucket bucket name + * @param string $inventoryConfigId inventory id + * @param null $options + * @return InventoryConfig|null + * @throws OssException + * @throws RequestCore_Exception + */ + public function getBucketInventory($bucket, $inventoryConfigId, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_GET; + $options[self::OSS_OBJECT] = '/'; + $options[self::OSS_SUB_RESOURCE] = 'inventory&inventoryId='.$inventoryConfigId; + $options[self::OSS_CONTENT_TYPE] = 'application/xml'; + $response = $this->auth($options); + $result = new GetBucketInventoryResult($response); + return $result->getData(); + } + + + /** + * List Inventory + * @param string $bucket bucket name + * @param null $options + * @return ListInventoryConfig|null + * @throws OssException|RequestCore_Exception + */ + public function listBucketInventory($bucket, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_GET; + $options[self::OSS_OBJECT] = '/'; + $options[self::OSS_SUB_RESOURCE] = 'inventory'; + $options[self::OSS_CONTENT_TYPE] = 'text/plain'; + $response = $this->auth($options); + $result = new ListBucketInventoryResult($response); + return $result->getData(); + } + + + /** + * Delete Inventory by InventoryId + * @param string $bucket bucket name + * @param string $inventoryConfigId inventory id + * @param null $options + * @return null + * @throws OssException|RequestCore_Exception|RequestCore_Exception + */ + public function deleteBucketInventory($bucket,$inventoryConfigId, $options = NULL) + { + $this->precheckCommon($bucket, NULL, $options, false); + $options[self::OSS_BUCKET] = $bucket; + $options[self::OSS_METHOD] = self::OSS_HTTP_DELETE; + $options[self::OSS_OBJECT] = '/'; + $options[self::OSS_SUB_RESOURCE] = 'inventory&inventoryId='.$inventoryConfigId; + $response = $this->auth($options); + $result = new HeaderResult($response); + return $result->getData(); + } + /** * Lists the bucket's object list (in ObjectListInfo) * @@ -3463,6 +3555,7 @@ public function setConnectTimeout($connectTimeout) const OSS_VERSION_ID_MARKER = 'version-id-marker'; const OSS_VERSION_ID = 'versionId'; const OSS_HEADER_VERSION_ID = 'x-oss-version-id'; + const OSS_CONTINUATION_TOKEN = 'continuation-token'; //private URLs const OSS_URL_ACCESS_KEY_ID = 'OSSAccessKeyId'; diff --git a/src/OSS/Result/GetBucketInventoryResult.php b/src/OSS/Result/GetBucketInventoryResult.php new file mode 100644 index 00000000..9267a62f --- /dev/null +++ b/src/OSS/Result/GetBucketInventoryResult.php @@ -0,0 +1,29 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + $config = new InventoryConfig(); + $config->parseFromXml($content); + return $config; + + } +} diff --git a/src/OSS/Result/ListBucketInventoryResult.php b/src/OSS/Result/ListBucketInventoryResult.php new file mode 100644 index 00000000..f2dba0c7 --- /dev/null +++ b/src/OSS/Result/ListBucketInventoryResult.php @@ -0,0 +1,29 @@ +rawResponse->body; + if (empty($content)) { + throw new OssException("body is null"); + } + $config = new ListInventoryConfig(); + $config->parseFromXml($content); + return $config; + } + +} \ No newline at end of file diff --git a/tests/OSS/Tests/GetBucketInventoryResultTest.php b/tests/OSS/Tests/GetBucketInventoryResultTest.php new file mode 100644 index 00000000..276d1ffe --- /dev/null +++ b/tests/OSS/Tests/GetBucketInventoryResultTest.php @@ -0,0 +1,90 @@ + + +report1 +true + + +CSV +1000000000000000 +acs:ram::1000000000000000:role/AliyunOSSRole +acs:oss:::bucket_0001 +prefix1 + + + + + + +Daily + + +myprefix/ + +All + +Size +LastModifiedDate +ETag +StorageClass +IsMultipartUploaded +EncryptionStatus + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXml() + { + $response = new ResponseCore(array(), $this->validXml, 200); + $result = new GetBucketInventoryResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + $info = $result->getData(); + $this->assertEquals("report1",$info->getId()); + $this->assertEquals("true",$info->getIsEnabled()); + $this->assertEquals("All",$info->getIncludedObjectVersions()); + $this->assertEquals("Daily",$info->getSchedule()); + $destination = $info->getDestination(); + $this->assertEquals("CSV",$destination->getFormat()); + $this->assertEquals("acs:oss:::bucket_0001",$destination->getBucket()); + + foreach ($info->getOptionalFields() as $key=> $field) { + if ($key == 5){ + $this->assertEquals("EncryptionStatus",$field->getFiled()); + } + } + } + + public function testInvalidXml() + { + $response = new ResponseCore(array(), $this->invalidXml, 200); + $result = new GetBucketInventoryResult($response); + $this->assertTrue($result->isOK()); + $this->assertNotNull($result->getData()); + $this->assertNotNull($result->getRawResponse()); + $this->assertNotNull($result->getRawResponse()->body); + } + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +} diff --git a/tests/OSS/Tests/InventoryConfigTest.php b/tests/OSS/Tests/InventoryConfigTest.php new file mode 100644 index 00000000..7f2f1a6e --- /dev/null +++ b/tests/OSS/Tests/InventoryConfigTest.php @@ -0,0 +1,116 @@ + + +report1 +true + + +CSV +1000000000000000 +acs:ram::1000000000000000:role/AliyunOSSRole +acs:oss:::destination-bucket +prefix1 + + +keyId + + + + + +Daily + + +filterPrefix/ +1637883649 +1638347592 +1024 +1048576 +Standard,IA + +All + +Size +LastModifiedDate +ETag +StorageClass +IsMultipartUploaded +EncryptionStatus + + +BBBB; + private $invalidXml = << + +BBBB; + + public function testValidXml() + { + $id = "report1"; + $isEnabled = InventoryConfig::IS_ENABLED_TRUE; + $filterPrefix = "filterPrefix/"; + $version = InventoryConfig::OBJECT_VERSION_ALL; + $frequency = InventoryConfig::FREQUENCY_DAILY; + $kmsId = "keyId"; + + $LastModifyBeginTimeStamp = "1637883649"; + $LastModifyEndTimeStamp = "1638347592"; + $LowerSizeBound = "1024"; + $UpperSizeBound = "1048576"; + $StorageClass = "Standard,IA"; + $configFilter = new InventoryConfigFilter($filterPrefix,$LastModifyBeginTimeStamp,$LastModifyEndTimeStamp,$LowerSizeBound,$UpperSizeBound,$StorageClass); + $files = array( + new InventoryConfigOptionalFields(InventoryConfig::FIELD_SIZE), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_LAST_MODIFIED_DATE), + + new InventoryConfigOptionalFields(InventoryConfig::FIELD_ETAG), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_STORAGE_CLASS), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_IS_MULTIPART_UPLOADED), + new InventoryConfigOptionalFields(InventoryConfig::FIELD_ENCRYPTION_STATUS), + ); + + $format = InventoryConfigOssBucketDestination::DEST_FORMAT; + $accountId = '1000000000000000'; + $roleArn = 'acs:ram::1000000000000000:role/AliyunOSSRole'; + $bucketName = 'acs:oss:::destination-bucket'; + $prefix = 'prefix1'; + $configDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucketName,$prefix,null,$kmsId); + + + $inventoryConfig = new InventoryConfig($id,$isEnabled,$frequency,$version,$configDestination,$configFilter,$files); + $this->assertEquals($this->cleanXml($inventoryConfig->serializeToXml()), $this->cleanXml($this->validXml)); + } + + public function testInvalidXml() + { + $inventoryConfig = new InventoryConfig(); + $inventoryConfig->parseFromXml($this->cleanXml($this->invalidXml)); + $this->assertEquals($this->cleanXml($this->invalidXml), $this->cleanXml($inventoryConfig->serializeToXml())); + } + + public function testInvalidXmlOne() + { + $inventoryConfig = new InventoryConfig(); + $inventoryConfig->parseFromXml($this->cleanXml($this->validXml)); + $this->assertEquals($this->cleanXml($this->validXml), $this->cleanXml($inventoryConfig->serializeToXml())); + } + + + + private function cleanXml($xml) + { + return str_replace("\n", "", str_replace("\r", "", $xml)); + } +} diff --git a/tests/OSS/Tests/ListBucketInventoryResultTest.php b/tests/OSS/Tests/ListBucketInventoryResultTest.php new file mode 100644 index 00000000..36fea138 --- /dev/null +++ b/tests/OSS/Tests/ListBucketInventoryResultTest.php @@ -0,0 +1,138 @@ + + + + report1 + true + + + CSV + 1000000000000000 + acs:ram::1000000000000000:role/AliyunOSSRole + acs:oss:::destination-bucket + prefix1 + + + + Daily + + + prefix/One + + All + + Size + LastModifiedDate + ETag + StorageClass + IsMultipartUploaded + EncryptionStatus + + + + report2 + true + + + CSV + 1000000000000000 + acs:ram::1000000000000000:role/AliyunOSSRole + acs:oss:::destination-bucket + prefix2 + + oss-key-id + + + + + Daily + + + prefix/Two + + All + + Size + LastModifiedDate + ETag + StorageClass + IsMultipartUploaded + EncryptionStatus + + + + report3 + true + + + CSV + 1000000000000000 + acs:ram::1000000000000000:role/AliyunOSSRole + acs:oss:::destination-bucket + prefix3 + + kms-key-id + + + + + Daily + + + prefix/Three + + All + + Size + LastModifiedDate + ETag + StorageClass + IsMultipartUploaded + EncryptionStatus + + + true + 11111 + +BBBB; + + public function testValidXml() + { + $config = new ListInventoryConfig(); + $config->parseFromXml($this->validXml); + + + + $config1 = $config->getInventoryList(); + + $this->assertEquals("true",$config->getIsTruncated()); + $this->assertEquals("11111",$config->getNextContinuationToken()); + $this->assertEquals("report1",$config1[0]->getId()); + $this->assertEquals("true",$config1[0]->getIsEnabled()); + $this->assertEquals("Daily",$config1[0]->getSchedule()); + $this->assertEquals("1000000000000000",$config1[0]->getDestination()->getAccountId()); + $this->assertEquals("acs:ram::1000000000000000:role/AliyunOSSRole",$config1[0]->getDestination()->getRoleArn()); + $this->assertEquals("acs:oss:::destination-bucket",$config1[0]->getDestination()->getBucket()); + $this->assertNull($config1[0]->getDestination()->getOssId()); + $this->assertEquals("oss-key-id",$config1[1]->getDestination()->getOssId()); + + $this->assertEquals("report2",$config1[1]->getId()); + $this->assertEquals("true",$config1[1]->getIsEnabled()); + $this->assertEquals("Daily",$config1[1]->getSchedule()); + $this->assertEquals("1000000000000000",$config1[1]->getDestination()->getAccountId()); + $this->assertEquals("acs:ram::1000000000000000:role/AliyunOSSRole",$config1[1]->getDestination()->getRoleArn()); + $this->assertEquals("acs:oss:::destination-bucket",$config1[1]->getDestination()->getBucket()); + + $this->assertEquals("oss-key-id",$config1[1]->getDestination()->getOssId()); + $this->assertEquals("kms-key-id",$config1[2]->getDestination()->getKmsId()); + } +} diff --git a/tests/OSS/Tests/OssClientBucketInventoryTest.php b/tests/OSS/Tests/OssClientBucketInventoryTest.php new file mode 100644 index 00000000..144993b3 --- /dev/null +++ b/tests/OSS/Tests/OssClientBucketInventoryTest.php @@ -0,0 +1,81 @@ +bucket; + $prefix = 'prefix1'; + $configDestination = new InventoryConfigOssBucketDestination($format,$accountId,$roleArn,$bucketName,$prefix); + + $inventoryConfig = new InventoryConfig($id,$isEnabled,$frequency,$version,$configDestination,$configFilter,$files); + try{ + $this->ossClient->putBucketInventory($this->bucket,$inventoryConfig); + }catch (OssException $e){ + $this->assertTrue(false); + } + + try{ + Common::waitMetaSync(); + $result = $this->ossClient->getBucketInventory($this->bucket,"report2"); + var_dump($result); + }catch (OssException $e){ + $this->assertTrue(false); + } + + try{ + Common::waitMetaSync(); + $result = $this->ossClient->listBucketInventory($this->bucket); + var_dump($result); + }catch (OssException $e){ + $this->assertTrue(false); + } + + try{ + Common::waitMetaSync(); + $result = $this->ossClient->deleteBucketInventory($this->bucket,'report2'); + var_dump($result); + }catch (OssException $e){ + $this->assertTrue(false); + } + + } + +}