Search code examples
phpamazon-s3aws-php-sdk

set up expiration for putObject in s3 php sdk


This is working codes for creating bucket and putObject:

$bucket = uniqid("php-sdk-sample-", true);
echo "Creating bucket named {$bucket}\n";
$result = $client->createBucket(array(
'Bucket' => $bucket
));
// Wait until the bucket is created
$key = 'hello_world.txt';
echo "Creating a new object with key {$key}\n";
$result = $client->createObject(array(
'Bucket' => $bucket,
'Key'    => $key,
'Body'   => "Hello World!" 
));

How can I set up expiration date for this just created object ? For ex. I want to see disappear the uploaded file 3 days later.


Solution

  • You need to read and learn about S3 object lifecycle management first, then you can use the putBucketLifecycle operation to configure it.