如何将文件归档到以日期为前缀的文件夹中?,amazon-web-services,amazon-s3,aws-lambda,amazon-cloudformation"/>

首页 > 解决方案 > 如何将文件归档到以日期为前缀的文件夹中?

问题描述

对于上次修改日期超过特定时间(例如 7 天)的文件,需要将存储桶文件夹内的文件(即放在前缀下)归档到以日期为前缀的子文件夹中:

示例文件夹结构:

a.txt
b.txt

20210826

c.txt(最后修改日期超过 1 周)

20210819

d.txt(最后修改日期超过 2 周)

知道如何实现吗?似乎没有现成的归档策略来实现这一点。

我能想到的唯一方法是通过 lambda 函数(带有调度程序触发器)来:

另一个问题是关于净化的。如果文件放在日期前缀下,我们如何在 CloudFormation 模板中配置 LifecycleConfiguration 规则?

  LifecycleConfiguration:
    Rules:
      - Id: DeletionRule
        Prefix: '' (how to set it to cater for different dates as the key)
        Status: Enabled
        ExpirationInDays: !FindInMap [EnvironmentsMap, !Ref env, S3FileRetentionIndays]

标签: amazon-web-servicesamazon-s3aws-lambdaamazon-cloudformation

解决方案


您可以配置生命周期规则,在 x 时间后将对象转换到不同的存储类,然后使用基于调用的 CloudTrail API 的事件桥捕获该操作。https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-ct-api-tutorial.html

最后,Event Bridge 触发一个 Lambda 函数,将 S3 对象移动到带有日期前缀的子文件夹。

关于过期配置,您需要为所有清除的文件创建一个父文件夹并将其设置为前缀。

您的 Cloudformation 模板将如下所示:

LifecycleConfiguration:
Rules:
  - Id: DeletionRule
    Prefix: 'purged-files/'
    Status: Enabled
    ExpirationInDays: !FindInMap [EnvironmentsMap, !Ref env, S3FileRetentionIndays]

推荐阅读