简体   繁体   English

通过 php sdk 过期对象 s3

[英]expiration object s3 through php sdk

I want to set an expiration date for an object on s3 I create the object like this:我想在 s3 上为对象设置到期日期我创建这样的对象:

$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:但是当我在 aws S3 Console 上检查文件时,对象会保留:

Expiry Date:    None
Expiration Rule:    N/A

How can I set object expiration?如何设置对象过期时间?

Use the putBucketLifecycle method like that:像这样使用putBucketLifecycle方法:

    $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.因此,如果您需要附加新规则,我建议您先使用getBucketLifecycleConfiguration并将以前的规则与新规则合并。
  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. 'prefix' 是您的路径,不包括文件名,尾部带有斜杠。

Resources资源

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#putbucketlifecycleconfiguration 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在 AWS S3 控制台(网络版)中,单击存储桶
  2. Choose Properties ;选择属性 expand Lifecycle展开生命周期
  3. Add Rule appears.添加规则出现。 Click it.点击它。
  4. Complete the wizard using your own set of rules使用您自己的规则集完成向导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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