amazon-web-services - 使用 Powershell 删除 s3 Bucket 中超过 14 天的文件
问题描述
我正在尝试编写一个脚本来使用 PowerShell 删除超过 14 天的文件,但我的脚本会删除在我的 s3 存储桶中创建的每个文件。
我的脚本有什么问题吗?
#set a bucket name
$bucket = "neb-migration/ms-sql/"
#set the expiration date of files
$limit_date = (Get-Date).AddDays(-14)
#get all the files
$files = aws s3 ls "$($bucket)"
#extract the file name and date
$parsed = $files | ForEach-Object { @{ date = $_.split(' ')[0] ; fname = $_.split(' ')[-1] } }
#filter files older than $limit_date
$filtred = $parsed | Where-Object { ![string]::IsNullOrEmpty($_.date) -and [datetime]::parseexact($_.date, 'yyyy-MM-dd', $null) -ge $limit_date }
# #remove filtered files
$filtred | ForEach-Object { aws s3 rm "s3://$($bucket)$($_.fname)" }
解决方案
推荐阅读
- python - 分组数据框的箱线图
- java - 有没有办法以这种方式替换此代码?
- google-apps-script - 在第二个以第一个命名后,将单元格值从一个电子表格复制到第二个
- wysiwyg - Strapi 中的文本字段缺少所见即所得复选框
- python - Pandas 列 - 将字符串转换为特定的日期格式
- python - 在python中读取由换行符分隔的json
- python - 从另一个带有参数的笔记本运行 Jupyter 笔记本
- c++ - 如何使用 AWS C++ SDK 在给定的开始和结束日期范围内列出 S3 中的文件
- angular - 带有复选框的角材料树 - 获得检查和不确定的值
- excel - 有没有一种简单的方法可以使用 case 语句存储已保存的文件?