简体   繁体   中英

expiration object s3 through php sdk

I want to set an expiration date for an object on s3 I create the object like this:

$this->s3->putObject(array(
'Bucket' => BUCKET,
'Key'=> "path",
'SourceFile'   => $fileTmp,
'Expires' => time()+2*60,
'ACL'    => 'private'
));

But when I check file on aws S3 Console the object keeps:

Expiry Date:    None
Expiration Rule:    N/A

How can I set object expiration?

Use the putBucketLifecycle method like that:

    $result = $client->putBucketLifecycleConfiguration([
        'Bucket' => '<string>', // REQUIRED
        'LifecycleConfiguration' => [
            'Rules' => [ // REQUIRED
                [
                    'Expiration' => [
                        'Date' => <integer || string || DateTime>,
                        'Days' => <integer>,
                        'ExpiredObjectDeleteMarker' => true || false,
                    ],
                    'ID' => '<string>',
                    'Prefix' => '<string>',
                    'Status' => 'Enabled|Disabled', // REQUIRED
                ],
                // ...
            ],
],
]);

Note

  1. This method will replace all the existing rules. So if you need to append a new rule I recommend you to use getBucketLifecycleConfiguration before and merge the previous rules with the new one.
  2. You can only set a Lifecycle rule on a prefix (or tag), not an object.
  3. 'prefix' is your path, excluding the file name, with a trailing slash.

Resources

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putbucketlifecycleconfiguration

Lifecycle policies are set per bucket, not per file.

Here's how to set a Lifecycle policy for a bucket:

  1. In AWS S3 console (the web version), click on the bucket
  2. Choose Properties ; expand Lifecycle
  3. Add Rule appears. Click it.
  4. Complete the wizard using your own set of rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM