<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// snippet-start:[s3.php.put_bucket_lifecycle.complete]
// snippet-start:[s3.php.put_bucket_lifecycle.import]

require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
// snippet-end:[s3.php.put_bucket_lifecycle.import]

/**
 * Put bucket lifecycle
 *
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

$bucketName = 'BUCKET_NAME';

// snippet-start:[s3.php.put_bucket_lifecycle.main]
$client = new S3Client([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2006-03-01'
]);

try {
    $result = $client->putBucketLifecycle(
        [
            'Bucket' => $bucketName, // REQUIRED
            'LifecycleConfiguration' => [
                'Rules' => [ // REQUIRED
                    [
                        'AbortIncompleteMultipartUpload' => [
                            'DaysAfterInitiation' => 30,
                        ],
                        'ID' => 'unique_id',
                        'Status' => 'Enabled', // REQUIRED
                        'Prefix' => ''
                    ],
                ],
            ],
        ]
    );
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}

// snippet-end:[s3.php.put_bucket_lifecycle.main]
// snippet-end:[s3.php.put_bucket_lifecycle.complete]
