|
| 1 | +<?php |
| 2 | +require_once __DIR__ . '/Common.php'; |
| 3 | + |
| 4 | +use OSS\OssClient; |
| 5 | +use OSS\Core\OssException; |
| 6 | +use OSS\Model\ReplicationConfig; |
| 7 | +use OSS\Model\ReplicationOssBucketDestination; |
| 8 | +use OSS\Model\ReplicationSourceSelectionCriteria; |
| 9 | + |
| 10 | +$bucket = Common::getBucketName(); |
| 11 | +$ossClient = Common::getOssClient(); |
| 12 | +if (is_null($ossClient)) exit(1); |
| 13 | + |
| 14 | +//******************************* Simple Usage *************************************************************** |
| 15 | + |
| 16 | +// Set Bucket Replication rule |
| 17 | + |
| 18 | +$replicationConfig = new ReplicationConfig(); |
| 19 | +// Set the prefix of the object to be copied |
| 20 | +$replicationConfig->addPrefixSet('prefix_3'); |
| 21 | +$replicationConfig->addPrefixSet('prefix_4'); |
| 22 | +// Specify the operations that can be copied to the target bucket (ALL or PUT) |
| 23 | +$replicationConfig->addAction('ALL'); |
| 24 | +// Specify whether to copy historical data |
| 25 | +$replicationConfig->addHistoricalObjectReplication('enabled'); |
| 26 | +// Which role is authorized for OSS to use for data replication |
| 27 | +$replicationConfig->addSyncRole('aliyunramrole'); |
| 28 | +$replicationOssBucketDestination = new ReplicationOssBucketDestination(); |
| 29 | +// Specify the target bucket to which the data will be copied。 |
| 30 | +$replicationOssBucketDestination->addBucket('test-demo4'); |
| 31 | +// The region where the target bucket is located。 |
| 32 | +$replicationOssBucketDestination->addLocation('oss-cn-shanghai'); |
| 33 | +// Specify the data transmission link used when data is copied. internal (default) oss_acc: transmission acceleration link |
| 34 | +$replicationOssBucketDestination->addTransferType('internal'); |
| 35 | +$replicationConfig->addDestination($replicationOssBucketDestination); |
| 36 | + |
| 37 | +$replicationSourceSelectionCriteria = new ReplicationSourceSelectionCriteria(); |
| 38 | +// Specify whether OSS copies objects created through SSE-KMS encryption |
| 39 | +$replicationSourceSelectionCriteria->addStatus("Enabled"); |
| 40 | +// Specify SSE-KMS key ID. If you specify Status as Enabled, you must specify this element |
| 41 | +$replicationSourceSelectionCriteria->addReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d"); |
| 42 | +$replicationConfig->addSourceSelectionCriteria($replicationSourceSelectionCriteria); |
| 43 | +$ossClient->putBucketReplication($bucket,$replicationConfig); |
| 44 | +Common::println("Bucket replication has created"); |
| 45 | + |
| 46 | + |
| 47 | +// Get bucket Replication rule |
| 48 | +$replicationResult = $ossClient->getBucketReplication($bucket); |
| 49 | +Common::println("===Replication Rule start ==="); |
| 50 | +foreach ($replicationResult as $info) { |
| 51 | + Common::println("Replication Id:". $info->getId().PHP_EOL); |
| 52 | + if ($info->getPrefixSet()['Prefix']){ |
| 53 | + foreach ($info->getPrefixSet()['Prefix'] as $prefix){ |
| 54 | + Common::println("Replication Prefix: ".$prefix.PHP_EOL); |
| 55 | + } |
| 56 | + } |
| 57 | + Common::println("Replication Action:". $info->getAction().PHP_EOL); |
| 58 | + Common::println("Replication Target Bucket: ". $info->getDestination()['Bucket'].PHP_EOL); |
| 59 | + Common::println("Replication Target Bucket Location: ". $info->getDestination()['Location'].PHP_EOL); |
| 60 | + if(isset($info->getDestination()['TransferType'])){ |
| 61 | + Common::println("Replication Target Bucket TransferType: ". $info->getDestination()['TransferType'].PHP_EOL); |
| 62 | + } |
| 63 | + |
| 64 | + Common::println("Replication HistoricalObjectReplication:". $info->getHistoricalObjectReplication().PHP_EOL); |
| 65 | + Common::println("Replication SyncRole: ". $info->getSyncRole().PHP_EOL); |
| 66 | + Common::println("Replication Status: ". $info->getStatus().PHP_EOL); |
| 67 | +} |
| 68 | +Common::println("===Replication Rule End ==="); |
| 69 | + |
| 70 | +// Get Bucket Replication Location |
| 71 | + |
| 72 | +$replicationResult = $ossClient->getBucketReplicationLocation($bucket); |
| 73 | + |
| 74 | +if($replicationResult){ |
| 75 | + Common::println("=====================Bucket replication location start=================================".PHP_EOL); |
| 76 | + if ($replicationResult->getLocations()){ |
| 77 | + foreach ($replicationResult->getLocations() as $location){ |
| 78 | + Common::println("Bucket replication location is ".$location.PHP_EOL); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if ($replicationResult->getLocationTransferTypes()){ |
| 83 | + foreach ($replicationResult->getLocationTransferTypes() as $type){ |
| 84 | + Common::println("Bucket replication location LocationTransferType location is: ".$type['location'].PHP_EOL); |
| 85 | + Common::println("Bucket replication location LocationTransferType type is: ".$type['type'].PHP_EOL); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Common::println("========================Bucket replication location end ============================".PHP_EOL); |
| 90 | +} |
| 91 | + |
| 92 | +// Get Bucket Replication Progress |
| 93 | + |
| 94 | + |
| 95 | +$replicationProcessResult = $ossClient->getBucketReplicationProgress($bucket,'test-replication-id'); |
| 96 | + |
| 97 | +if($replicationProcessResult){ |
| 98 | + Common::println("=====================Bucket replication process start=================================".PHP_EOL); |
| 99 | + Common::println("Bucket replication process id is ".$replicationProcessResult->getId().PHP_EOL); |
| 100 | + if($replicationProcessResult->getPrefixSet()){ |
| 101 | + foreach ($replicationProcessResult->getPrefixSet()['Prefix'] as $prefix){ |
| 102 | + Common::println("Bucket replication process prefix is: ".$prefix.PHP_EOL); |
| 103 | + } |
| 104 | + } |
| 105 | + Common::println("Bucket replication process action is ".$replicationProcessResult->getAction().PHP_EOL); |
| 106 | + if($replicationProcessResult->getDestination()){ |
| 107 | + Common::println("Bucket replication process bucket name is: ".$replicationProcessResult->getDestination()['Bucket'].PHP_EOL); |
| 108 | + Common::println("Bucket replication process bucket location is: ".$replicationProcessResult->getDestination()['Location'].PHP_EOL); |
| 109 | + Common::println("Bucket replication process Prefix transfer type is: ".$replicationProcessResult->getDestination()['TransferType'].PHP_EOL); |
| 110 | + } |
| 111 | + Common::println("Bucket replication process status is ".$replicationProcessResult->getStatus().PHP_EOL); |
| 112 | + Common::println("Bucket replication process historicalObjectReplication is: ".$replicationProcessResult->getHistoricalObjectReplication().PHP_EOL); |
| 113 | + |
| 114 | + if($replicationProcessResult->getProgress()){ |
| 115 | + Common::println("Bucket replication process HistoricalObject is: ".$replicationProcessResult->getProgress()['HistoricalObject'].PHP_EOL); |
| 116 | + Common::println("Bucket replication process NewObject is: ".$replicationProcessResult->getProgress()['NewObject'].PHP_EOL); |
| 117 | + } |
| 118 | + Common::println("========================Bucket replication process end ============================".PHP_EOL); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +// Delete Bucket replication by ID |
| 123 | +$ossClient->deleteBucketReplication($bucket,"test_replication_1"); |
| 124 | +Common::println("Bucket replication test_replication_1 has deleted"); |
| 125 | + |
| 126 | + |
| 127 | +//******************************* For complete usage, see the following functions **************************************************** |
| 128 | + |
| 129 | +putBucketReplication($ossClient, $bucket); |
| 130 | +getBucketReplication($ossClient, $bucket); |
| 131 | +deleteReplication($ossClient, $bucket); |
| 132 | +getBucketReplicationLocation($ossClient, $bucket); |
| 133 | +getBucketReplicationProgress($ossClient, $bucket); |
| 134 | + |
| 135 | + |
| 136 | +/** |
| 137 | + * Sets bucket replication rule |
| 138 | + * |
| 139 | + * @param $ossClient OssClient |
| 140 | + * @param $bucket string bucket name |
| 141 | + * @return null |
| 142 | + */ |
| 143 | +function putBucketReplication($ossClient, $bucket) |
| 144 | +{ |
| 145 | + try { |
| 146 | + $replicationConfig = new ReplicationConfig(); |
| 147 | + // Set the prefix of the object to be copied |
| 148 | + $replicationConfig->addPrefixSet('prefix_3'); |
| 149 | + $replicationConfig->addPrefixSet('prefix_4'); |
| 150 | + // Specify the operations that can be copied to the target bucket (ALL or PUT) |
| 151 | + $replicationConfig->addAction('ALL'); |
| 152 | + // Specify whether to copy historical data |
| 153 | + $replicationConfig->addHistoricalObjectReplication('enabled'); |
| 154 | + // Which role is authorized for OSS to use for data replication |
| 155 | + $replicationConfig->addSyncRole('aliyunramrole'); |
| 156 | + $replicationOssBucketDestination = new ReplicationOssBucketDestination(); |
| 157 | + // Specify the target bucket to which the data will be copied。 |
| 158 | + $replicationOssBucketDestination->addBucket('test-demo4'); |
| 159 | + // The region where the target bucket is located。 |
| 160 | + $replicationOssBucketDestination->addLocation('oss-cn-shanghai'); |
| 161 | + // Specify the data transmission link used when data is copied. internal (default) oss_acc: transmission acceleration link |
| 162 | + $replicationOssBucketDestination->addTransferType('internal'); |
| 163 | + $replicationConfig->addDestination($replicationOssBucketDestination); |
| 164 | + |
| 165 | + $replicationSourceSelectionCriteria = new ReplicationSourceSelectionCriteria(); |
| 166 | + // Specify whether OSS copies objects created through SSE-KMS encryption |
| 167 | + $replicationSourceSelectionCriteria->addStatus("Enabled"); |
| 168 | + // Specify SSE-KMS key ID. If you specify Status as Enabled, you must specify this element |
| 169 | + $replicationSourceSelectionCriteria->addReplicaKmsKeyID("c4d49f85-ee30-426b-a5ed-95e9139d"); |
| 170 | + $replicationConfig->addSourceSelectionCriteria($replicationSourceSelectionCriteria); |
| 171 | + $ossClient->putBucketReplication($bucket,$replicationConfig); |
| 172 | + }catch (OssException $e) { |
| 173 | + printf(__FUNCTION__ . ": FAILED\n"); |
| 174 | + printf($e->getMessage() . "\n"); |
| 175 | + return; |
| 176 | + } |
| 177 | + print(__FUNCTION__ . ": OK" . "\n"); |
| 178 | + |
| 179 | + printf("Bucket replication has created"); |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +/** |
| 184 | + * Get bucket replication rule |
| 185 | + * |
| 186 | + * @param OssClient $ossClient OssClient instance |
| 187 | + * @param string $bucket bucket name |
| 188 | + * @return null |
| 189 | + */ |
| 190 | +function getBucketReplication($ossClient, $bucket) |
| 191 | +{ |
| 192 | + |
| 193 | + |
| 194 | + try { |
| 195 | + $replicationResult = $ossClient->getBucketReplication($bucket); |
| 196 | + } catch (OssException $e) { |
| 197 | + printf(__FUNCTION__ . ": FAILED\n"); |
| 198 | + printf($e->getMessage() . "\n"); |
| 199 | + return; |
| 200 | + } |
| 201 | + print(__FUNCTION__ . ": OK" . "\n"); |
| 202 | + printf("===Replication Rule start ==="); |
| 203 | + foreach ($replicationResult as $info) { |
| 204 | + printf("Replication Id:". $info->getId().PHP_EOL); |
| 205 | + if ($info->getPrefixSet()['Prefix']){ |
| 206 | + foreach ($info->getPrefixSet()['Prefix'] as $prefix){ |
| 207 | + printf("Replication Prefix: ".$prefix.PHP_EOL); |
| 208 | + } |
| 209 | + } |
| 210 | + printf("Replication Action:". $info->getAction().PHP_EOL); |
| 211 | + printf("Replication Target Bucket: ". $info->getDestination()['Bucket'].PHP_EOL); |
| 212 | + printf("Replication Target Bucket Location: ". $info->getDestination()['Location'].PHP_EOL); |
| 213 | + if(isset($info->getDestination()['TransferType'])){ |
| 214 | + printf("Replication Target Bucket TransferType: ". $info->getDestination()['TransferType'].PHP_EOL); |
| 215 | + } |
| 216 | + |
| 217 | + printf("Replication HistoricalObjectReplication:". $info->getHistoricalObjectReplication().PHP_EOL); |
| 218 | + printf("Replication SyncRole: ". $info->getSyncRole()); |
| 219 | + if($info->getSourceSelectionCriteria()){ |
| 220 | + printf("Replication SourceSelectionCriteria Status: ". $info->getSourceSelectionCriteria()['SseKmsEncryptedObjects']['Status'].PHP_EOL); |
| 221 | + if($info->getSourceSelectionCriteria()['SseKmsEncryptedObjects']['Status'] == "Enabled"){ |
| 222 | + printf("Replication EncryptionConfiguration ReplicaKmsKeyID: ".$info->getEncryptionConfiguration().PHP_EOL); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + printf("===Replication Rule End ==="); |
| 227 | +} |
| 228 | + |
| 229 | + |
| 230 | +/** |
| 231 | + * Get bucket replication location |
| 232 | + * |
| 233 | + * @param OssClient $ossClient OssClient instance |
| 234 | + * @param string $bucket bucket name |
| 235 | + * @return null |
| 236 | + */ |
| 237 | +function getBucketReplicationLocation($ossClient, $bucket) |
| 238 | +{ |
| 239 | + try { |
| 240 | + $replicationResult = $ossClient->getBucketReplicationLocation($bucket); |
| 241 | + } catch (OssException $e) { |
| 242 | + printf(__FUNCTION__ . ": FAILED\n"); |
| 243 | + printf($e->getMessage() . "\n"); |
| 244 | + return; |
| 245 | + } |
| 246 | + print(__FUNCTION__ . ": OK" . "\n"); |
| 247 | + printf("=====================Bucket replication location start=================================".PHP_EOL); |
| 248 | + if ($replicationResult->getLocations()){ |
| 249 | + foreach ($replicationResult->getLocations() as $location){ |
| 250 | + printf("Bucket replication location is ".$location.PHP_EOL); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + if ($replicationResult->getLocationTransferTypes()){ |
| 255 | + foreach ($replicationResult->getLocationTransferTypes() as $type){ |
| 256 | + printf("Bucket replication location LocationTransferType location is: ".$type['location'].PHP_EOL); |
| 257 | + printf("Bucket replication location LocationTransferType type is: ".$type['type'].PHP_EOL); |
| 258 | + } |
| 259 | + } |
| 260 | + printf("========================Bucket replication location end ============================".PHP_EOL); |
| 261 | +} |
| 262 | + |
| 263 | + |
| 264 | +/** |
| 265 | + * Get bucket replication location |
| 266 | + * |
| 267 | + * @param OssClient $ossClient OssClient instance |
| 268 | + * @param string $bucket bucket name |
| 269 | + * @return null |
| 270 | + */ |
| 271 | +function getBucketReplicationProgress($ossClient, $bucket) |
| 272 | +{ |
| 273 | + try { |
| 274 | + $replicationProcessResult = $ossClient->getBucketReplicationProgress($bucket,"test-replication-id"); |
| 275 | + } catch (OssException $e) { |
| 276 | + printf(__FUNCTION__ . ": FAILED\n"); |
| 277 | + printf($e->getMessage() . "\n"); |
| 278 | + return; |
| 279 | + } |
| 280 | + print(__FUNCTION__ . ": OK" . "\n"); |
| 281 | + printf("=====================Bucket replication progress start=================================".PHP_EOL); |
| 282 | + printf("Bucket replication process id is ".$replicationProcessResult->getId().PHP_EOL); |
| 283 | + if($replicationProcessResult->getPrefixSet()){ |
| 284 | + foreach ($replicationProcessResult->getPrefixSet()['Prefix'] as $prefix){ |
| 285 | + Common::println("Bucket replication process prefix is: ".$prefix.PHP_EOL); |
| 286 | + } |
| 287 | + } |
| 288 | + Common::println("Bucket replication process action is ".$replicationProcessResult->getAction().PHP_EOL); |
| 289 | + if($replicationProcessResult->getDestination()){ |
| 290 | + printf("Bucket replication process bucket name is: ".$replicationProcessResult->getDestination()['Bucket'].PHP_EOL); |
| 291 | + printf("Bucket replication process bucket location is: ".$replicationProcessResult->getDestination()['Location'].PHP_EOL); |
| 292 | + printf("Bucket replication process Prefix transfer type is: ".$replicationProcessResult->getDestination()['TransferType'].PHP_EOL); |
| 293 | + } |
| 294 | + printf("Bucket replication process status is ".$replicationProcessResult->getStatus().PHP_EOL); |
| 295 | + printf("Bucket replication process historicalObjectReplication is: ".$replicationProcessResult->getHistoricalObjectReplication().PHP_EOL); |
| 296 | + |
| 297 | + if($replicationProcessResult->getProgress()){ |
| 298 | + printf("Bucket replication process HistoricalObject is: ".$replicationProcessResult->getProgress()['HistoricalObject'].PHP_EOL); |
| 299 | + printf("Bucket replication process NewObject is: ".$replicationProcessResult->getProgress()['NewObject'].PHP_EOL); |
| 300 | + } |
| 301 | + printf("========================Bucket replication progress end ============================".PHP_EOL); |
| 302 | +} |
| 303 | + |
| 304 | +/** |
| 305 | + * Delete bucket replication rule |
| 306 | + * @param OssClient $ossClient OssClient instance |
| 307 | + * @param string $bucket bucket name |
| 308 | + * @return null |
| 309 | + */ |
| 310 | +function deleteBucketReplication($ossClient, $bucket) |
| 311 | +{ |
| 312 | + $ruleId = 'test_replication_id'; |
| 313 | + try { |
| 314 | + $ossClient->deleteBucketReplication($bucket,$ruleId); |
| 315 | + } catch (OssException $e) { |
| 316 | + printf(__FUNCTION__ . ": FAILED\n"); |
| 317 | + printf($e->getMessage() . "\n"); |
| 318 | + return; |
| 319 | + } |
| 320 | + print(__FUNCTION__ . ": OK" . "\n"); |
| 321 | + printf("$bucket replication rule has deleted"); |
| 322 | +} |
| 323 | + |
0 commit comments