Path: blob/master/code_examples/php_examples/S3examples/s3-deleting-multi-objects-versioned.php
4084 views
<?php1// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.2// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE )34require 'vendor/autoload.php';56use Aws\S3\S3Client;78$bucket = '*** Your Bucket Name ***';9$keyname = '*** Your Object Key ***';1011$s3 = new S3Client([12'version' => 'latest',13'region' => 'us-east-1'14]);1516// 1. Enable object versioning for the bucket.17$s3->putBucketVersioning([18'Bucket' => $bucket,19'Status' => 'Enabled',20]);2122// 2. Create a few versions of an object.23for ($i = 1; $i <= 3; $i++) {24$s3->putObject([25'Bucket' => $bucket,26'Key' => $keyname,27'Body' => "content {$i}",28]);29}3031// 3. List the objects versions and get the keys and version IDs.32$versions = $s3->listObjectVersions(['Bucket' => $bucket])33->getPath('Versions');3435// 4. Delete the object versions.36$s3->deleteObjects([37'Bucket' => $bucket,38'Delete' => [39'Objects' => array_map(function ($version) {40return [41'Key' => $version['Key'],42'VersionId' => $version['VersionId']43}, $versions),44],45]);4647echo "The following objects were deleted successfully:". PHP_EOL;48foreach ($result['Deleted'] as $object) {49echo "Key: {$object['Key']}, VersionId: {$object['VersionId']}" . PHP_EOL;50}5152echo PHP_EOL . "The following objects could not be deleted:" . PHP_EOL;53foreach ($result['Errors'] as $object) {54echo "Key: {$object['Key']}, VersionId: {$object['VersionId']}" . PHP_EOL;55}5657// 5. Suspend object versioning for the bucket.58$s3->putBucketVersioning([59'Bucket' => $bucket,60'Status' => 'Suspended',61]);626364