使用JavaScript API设置S3对象到期(删除)

时间:2022-07-07 23:02:30

I am using the Node.js JavaScript API for Amazon AWS S3, and would like to set objects to expire a specified number of days after the objects are created. That is, if I create and upload a new object, I want it to automatically delete itself 100 days or so from now. Is this possible to set expiration for deletion on a per-object basis?

我正在为Amazon AWS S3使用Node.js JavaScript API,并希望将对象设置为在创建对象后的指定天数到期。也就是说,如果我创建并上传新对象,我希望它从现在起100天左右自动删除。是否可以在每个对象的基础上设置删除的到期时间?

The documentation indicates this may be possible:

文档表明这可能是:

Amazon S3 provides an Expiration action that you can specify in your lifecycle configuration to expire objects.

Amazon S3提供了一个Expiration操作,您可以在生命周期配置中指定该操作使对象失效。

When an object reaches the end of its lifetime, Amazon S3 queues it for removal and removes it asynchronously. There may be a lag between the expiration date and the date at which Amazon S3 removes an object. You are not charged for storage time associated with an object that has expired.

当对象到达其生命周期的末尾时,Amazon S3将其排队以进行删除并异步删除它。到期日期与Amazon S3删除对象的日期之间可能存在延迟。您不需要为与已过期的对象关联的存储时间付费。

However, it seems that I would have to set this expiration in the bucket configuration, and not per-object when I upload/create them.

但是,似乎我必须在存储桶配置中设置此过期,而不是在我上传/创建它时设置每个对象。

The JavaScript SDK documentation indicates that I can set an Expires parameter when creating an object, but this seems to be for the Expires HTTP header when S3 returns the object for subsequent GET requests.

JavaScript SDK文档表明我可以在创建对象时设置Expires参数,但是当S3返回后续GET请求的对象时,这似乎是针对Expires HTTP标头。

Is there a way to set the expiration date of an object when creating it?

有没有办法在创建对象时设置对象的到期日期?

s3.putObject({
  Bucket: config.s3.bucketName, 
  Key: s3Key, 
  Body: objBuffer,
  ACL: 'public-read',
  ContentType: 'image/jpeg',
  StorageClass: 'REDUCED_REDUNDANCY',
  // Some option here for setting expiration/deletion date?
}, function () {
  console.log(arguments);
});

1 个解决方案

#1


8  

You can not set expiration rules on each object individually. To define object expiration rules, you have to define a bucket lifecycle configuration.

您无法单独为每个对象设置过期规则。要定义对象过期规则,您必须定义存储桶生命周期配置。

To do this with the node.js API, see the putBucketLifecycle call. You can also check out the REST API docs for the bucket lifecycle PUT operation.

要使用node.js API执行此操作,请参阅putBucketLifecycle调用。您还可以查看存储桶生命周期PUT操作的REST API文档。

#1


8  

You can not set expiration rules on each object individually. To define object expiration rules, you have to define a bucket lifecycle configuration.

您无法单独为每个对象设置过期规则。要定义对象过期规则,您必须定义存储桶生命周期配置。

To do this with the node.js API, see the putBucketLifecycle call. You can also check out the REST API docs for the bucket lifecycle PUT operation.

要使用node.js API执行此操作,请参阅putBucketLifecycle调用。您还可以查看存储桶生命周期PUT操作的REST API文档。