【问题标题】:<AWS S3> How to archive files to a folder with date as the prefix?<AWS S3> 如何将文件归档到以日期为前缀的文件夹中?
【发布时间】:2021-10-26 04:29:51
【问题描述】:

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

示例文件夹结构:

a.txt
b.txt

20210826

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

20210819

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

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

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

  • 扫描所有文件的时间戳以查看哪些文件早于 1 周
  • 将匹配的文件移动到前缀下(例如 20210826/c.txt)

另一个问题是关于清除的。如果文件放在日期前缀下,我们如何在 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-services amazon-s3 aws-lambda amazon-cloudformation


    【解决方案1】:

    您可以configure a lifecycle rule 在 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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      相关资源
      最近更新 更多