如何使用shell脚本列出未使用的AWS S3存储桶和空存储桶



我正在寻找过去90天未使用的s3存储桶的列表,以及空存储桶列表。

为了得到它,我尝试编写如下代码:

#/bin/sh
for bucketlist in  $(aws s3api list-buckets --query "Buckets[].Name");
do
listobjects=$(
aws s3api list-objects --bucket $bucketlist 
--query 'Contents[?contains(LastModified, `2020-08-06`)]')
done

此代码打印以下输出:[我只添加了一个bucket的结果供参考]

{
"Contents": [
{
"Key": "test2/image.png",
"LastModified": "2020-08-06T17:19:10.000Z",
"ETag": ""xxxxxx"",
"Size": 179008,,
"StorageClass": "STANDARD",
}
]
}

期望:

  1. 在上面的代码中,我只想打印在过去90天内未修改/使用的对象的bucket列表
  2. 我也在寻找空的遗愿清单

我不擅长编程,有人能指导我吗?提前感谢您的支持。

我制作了这个小bash脚本来查找我的帐户中的空存储桶:

#!/bin/zsh
for b in $(aws s3 ls | cut -d" " -f3)
do
echo -n $b
if [[ "$(aws s3api list-objects-v2 --bucket $b --max-items 1)" == "" ]]
then
echo " BUCKET EMPTY"
else
echo ""
fi
done

我使用list-objects-v2列出了对象,最大项数为1。如果没有项目,则结果为空;BUCKET EMPTY";在bucket名称旁边。

  • 注意1:您必须有权列出对象
  • 注意2:我不确定它将如何适用于带有已删除对象的版本化存储桶(看起来是空的,但实际上包含已删除对象中的旧版本(

这很好用,但不考虑非当前版本的对象。为此,您可以使用";列表对象版本";那么它也将查找非当前版本的对象。

#/bin/zsh对于b在$(aws s3-ls|cut-d"-f3(做echo-n$b如果[["$(aws s3api list object versions--bucket$b-max items 1("=="]]然后回声<-----BUCKET EMPTY";其他的回声"fi完成

这是我今天写的一个脚本。它不会更改任何内容,但它确实为您提供了进行更改的命令行。

#!/bin/bash
profile="default"
olddate="2020-01-01"
smallbucketsize=10
emptybucketlist=()
oldbucketlist=()
smallbucketlist=()
#for bucketlist in  $(aws s3api list-buckets  --profile $profile  | jq --raw-output '.Buckets[6,7,8,9].Name'); # test this script on just a few buckets
for bucketlist in  $(aws s3api list-buckets  --profile $profile  | jq --raw-output '.Buckets[].Name');
do
echo "* $bucketlist"
if [[ ! "$bucketlist" == *"shmr-logs" ]]; then
listobjects=$(
aws s3api list-objects --bucket $bucketlist 
--query 'Contents[*].Key' 
--profile $profile)
#echo "==$listobjects=="
if [[ "$listobjects" == "null" ]]; then
echo "$bucketlist is empty"
emptybucketlist+=("$bucketlist")
else
# get size
aws s3 ls --summarize  --human-readable --recursive --profile $profile s3://$bucketlist | tail -n1
# get number of files
filecount=$(echo $listobjects | jq length )
echo "contains $filecount files"
if [[ $filecount -lt $smallbucketsize ]]; then
smallbucketlist+=("$bucketlist")
fi
# get number of files older than $olddate
listoldobjects=$(
aws s3api list-objects --bucket $bucketlist 
--query "Contents[?LastModified<=`$olddate`]" 
--profile $profile)
oldfilecount=$(echo $listoldobjects | jq length )
echo "contains $oldfilecount old files"
# check if all files are old
if [[ $filecount -eq $oldfilecount ]]; then
echo "all the files are old"
oldbucketlist+=("$bucketlist")
fi
fi
fi
done
echo -e "nn"
echo "check the contents of these buckets which only contain old files"
for oldbuckets in ${oldbucketlist[@]};
do
echo "$oldbuckets"
done
echo -e "nn"
echo "check the contents of these buckets which don't have many files"
for smallbuckets in ${smallbucketlist[@]};
do
echo "aws s3api list-objects --bucket $smallbuckets --query 'Contents[*].Key' --profile $profile"
done
echo -e "nn"
echo "consider deleting these empty buckets"
for emptybuckets in "${emptybucketlist[@]}";
do
echo "aws s3api delete-bucket --profile $profile --bucket $emptybuckets"
done

最新更新