我正在寻找过去 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
此代码打印以下输出:[我仅添加了一个存储桶的结果以供参考]
{
"Contents": [
{
"Key": "test2/image.png",
"LastModified": "2020-08-06T17:19:10.000Z",
"ETag": "\"xxxxxx\"",
"Size": 179008,,
"StorageClass": "STANDARD",
}
]
}
期望:
我不擅长编程,有人可以指导我吗? 预先感谢您的支持。
我制作了这个小 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”。
这效果很好,但没有考虑非当前版本对象。为此,您可以使用“list-object-versions”,然后它也会查找非当前版本对象。
#!/bin/zsh 对于 $(aws s3 ls | cut -d" " -f3) 中的 b 做 回显-n $b if [[ "$(aws s3api list-object-versions --bucket $b --max-items 1)" == "" ]] 然后 回声”<----- BUCKET EMPTY" else echo "" fi done
这是我今天写的脚本。 它不会改变任何东西,但它确实为您提供了进行更改的命令行。
#!/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 "\n\n"
echo "check the contents of these buckets which only contain old files"
for oldbuckets in ${oldbucketlist[@]};
do
echo "$oldbuckets"
done
echo -e "\n\n"
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 "\n\n"
echo "consider deleting these empty buckets"
for emptybuckets in "${emptybucketlist[@]}";
do
echo "aws s3api delete-bucket --profile $profile --bucket $emptybuckets"
done
#!/bin/bash
echo "Running script"
# Define the regions to check
REGIONS=("us-east-1" "eu-west-1" "eu-west-2")
# Get the list of all S3 buckets
ALL_S3BUCKETS=$(aws s3 ls | cut -d' ' -f3)
# Remove duplicates from the list of all S3 buckets
ALL_S3BUCKETS=($(echo "${ALL_S3BUCKETS}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
# Check each bucket in all regions
for BUCKET in "${ALL_S3BUCKETS[@]}"
do
IS_ORPHANED=true
for REGION in "${REGIONS[@]}"
do
ADD="--region $REGION"
STACKS=$(aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE --query 'StackSummaries[*].StackName' --output text $ADD)
for STACK in $STACKS
do
STACK_BUCKETS=$(aws cloudformation list-stack-resources --stack-name $STACK --query "StackResourceSummaries[?ResourceType=='AWS::S3::Bucket'].PhysicalResourceId" --output text $ADD 2>> /dev/null)
for STACK_BUCKET in $STACK_BUCKETS
do
if [[ "$STACK_BUCKET" == "$BUCKET" ]]
then
IS_ORPHANED=false
break 2
fi
done
done
done
if $IS_ORPHANED
then
echo "$BUCKET is ORPHANED in all regions"
fi
done