amazon-web-services - 如何将文件归档到以日期为前缀的文件夹中?
问题描述
对于上次修改日期超过特定时间(例如 7 天)的文件,需要将存储桶文件夹内的文件(即放在前缀下)归档到以日期为前缀的子文件夹中:
示例文件夹结构:
a.txt
b.txt20210826
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]
解决方案
您可以配置生命周期规则,在 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]
推荐阅读
- r - 使用 str_detect 的 stringr 包 - 搜索一个单词并排除单词
- docker - 如何将 docker 启动脚本转换为 supervisord.conf 文件?
- redux - 为什么中间件转换后的 createAsyncThunk.fulfilled 操作负载没有在 Thunk 调用点转换?
- database - 防止多对多表中的“重复”行
- python - 在同一模板中使用 CreateView 和 ListView 并正确使用 form.instance
- apache-kafka - Kafka Cluster 是否具有灾难恢复优势?
- react-native - 为什么标题按钮 onPress 与常规按钮 onPress 的行为不同?
- ruby - 在 Dart 和 Ruby 中使用 reduce 计算浮点数组(双精度列表)的方差:我的 Dart 实现找不到错误
- java - 未能设置正确的 Java 环境变量
- java - java泛型观察者模式?