如何查找AWS S3存储桶或文件夹的总大小?

时间:2015-08-24 21:54:54

标签: amazon-web-services amazon-s3

亚马逊是否提供了一种简单的方法来查看我的S3存储桶或文件夹使用了多少存储空间?这是我可以计算我的成本等。

13 个答案:

答案 0 :(得分:18)

亚马逊已更改了网络界面,因此现在您可以在“更多”菜单下找到“获取大小”。

答案 1 :(得分:15)

截至2015年7月28日,您可以通过CloudWatch获取此信息。

$q

答案 2 :(得分:14)

两种方式,

使用aws cli

aws s3 ls --summarize --human-readable --recursive s3://bucket/folder/*

如果我们最后省略/,它将获取以您的文件夹名称开头的所有文件夹,并给出所有文件夹的总大小。

aws s3 ls --summarize --human-readable --recursive s3://bucket/folder

使用boto3 api

import boto3

def get_folder_size(bucket, prefix):
    total_size = 0
    for obj in boto3.resource('s3').Bucket(bucket).objects.filter(Prefix=prefix):
        total_size += obj.size
    return total_size

答案 3 :(得分:11)

2021 年的答案更新:)

在您的 AWS 控制台中,在 S3 存储桶下,找到存储桶或其中的文件夹,然后单击 Calculate total size

enter image description here

答案 4 :(得分:5)

调整至2020年的答案: 进入您的存储桶,选择所有文件夹,文件,然后单击“操作”->“获取总大小” enter image description here

答案 5 :(得分:4)

我使用s3cmd du s3://BUCKET/ --human-readable查看S3中文件夹的大小。它以非常易读的形式提供了有关存储桶中总物体及其大小的详细信息。

答案 6 :(得分:2)

最近和最简单的方法是转到“指标”选项卡。 它可以清楚地了解桶的大小和其中的对象数量。

Metrics

答案 7 :(得分:2)

如果有人需要字节精度:

aws s3 ls --summarize --recursive s3://path | tail -1 | awk '{print $3}'

答案 8 :(得分:1)

作为替代方案,您可以尝试s3cmd,它具有像Unix一样的 du 命令。

答案 9 :(得分:1)

找到here

aws s3api list-objects --bucket cyclops-images --output json --query "[sum(Contents[].Size), length(Contents[])]" | awk 'NR!=2 {print $0;next} NR==2 {print $0/1024/1024/1024" GB"}'

答案 10 :(得分:1)

使用AWS Web Console和Cloudwatch:

  1. 转到CloudWatch
  2. 从屏幕左侧点击指标
  3. 点击S3
  4. 点击存储
  5. 您将看到所有存储桶的列表。请注意,这里有两个可能的混淆点:

    a。您只会看到存储桶中至少有一个对象的存储桶。
    b。您可能看不到在其他区域创建的存储桶,并且可能需要使用右上角的下拉菜单切换区域以查看其他存储桶

  6. 在表明“搜索任何指标,维度或资源ID”的区域中搜索“ StandardStorage”一词

  7. 选择要计算其总大小的存储桶(或所有存储桶,并在单词“全部”下方的左侧选中复选框)
  8. 从时间栏到屏幕右上方至少选择3天(3天)或更长时间

您现在将看到一个图表,显示在选定时间段内所有选定桶的每日(或其他单位)列表的大小。

答案 11 :(得分:0)

s3cmd du --human-readable --recursive s3:// Bucket_Name /

答案 12 :(得分:0)

如果您不需要确切的字节数或存储桶很大(在TB或数百万个对象中),则使用CloudWatch指标是最快的方法,因为它不需要遍历所有对象。如果使用CLI命令,可能会占用大量CPU并可能导致超时或网络错误。

基于SO上其他一些运行aws cloudwatch get-metric-statistics命令的示例,我将其包装在一个有用的Bash函数中,该函数允许您有选择地为aws命令指定一个配置文件:< / p>

# print S3 bucket size and count
# usage: bsize <bucket> [profile]
function bsize() (
  bucket=$1 profile=${2-default}

  if [[ -z "$bucket" ]]; then
    echo >&2 "bsize <bucket> [profile]"
    return 1
  fi

  # ensure aws/jq/numfmt are installed
  for bin in aws jq numfmt; do
    if ! hash $bin 2> /dev/null; then
      echo >&2 "Please install \"$_\" first!"
      return 1
    fi
  done

  # get bucket region
  region=$(aws --profile $profile s3api get-bucket-location --bucket $bucket 2> /dev/null | jq -r '.LocationConstraint // "us-east-1"')
  if [[ -z "$region" ]]; then
    echo >&2 "Invalid bucket/profile name!"
    return 1
  fi

  # get storage class (assumes
  # all objects in same class)
  sclass=$(aws --profile $profile s3api list-objects --bucket $bucket --max-items=1 2> /dev/null | jq -r '.Contents[].StorageClass // "STANDARD"')
  case $sclass in
    REDUCED_REDUNDANCY) sclass="ReducedRedundancyStorage" ;;
    GLACIER)            sclass="GlacierStorage" ;;
    DEEP_ARCHIVE)       sclass="DeepArchiveStorage" ;;
    *)                  sclass="StandardStorage" ;;
  esac

  # _bsize <metric> <stype>
  _bsize() {
    metric=$1 stype=$2
    utnow=$(date +%s)
    aws --profile $profile cloudwatch get-metric-statistics --namespace AWS/S3 --start-time "$(echo "$utnow - 604800" | bc)" --end-time "$utnow" --period 604800 --statistics Average --region $region --metric-name $metric --dimensions Name=BucketName,Value="$bucket" Name=StorageType,Value="$stype" 2> /dev/null | jq -r '.Datapoints[].Average'
  }

  # _print <number> <units> <format> [suffix]
  _print() {
    number=$1 units=$2 format=$3 suffix=$4
    if [[ -n "$number" ]]; then
      numfmt --to="$units" --suffix="$suffix" --format="$format" $number | sed -En 's/([^0-9]+)$/ \1/p'
    fi
  }
  _print "$(_bsize BucketSizeBytes $sclass)" iec-i "%10.2f" B
  _print "$(_bsize NumberOfObjects AllStorageTypes)" si "%8.2f"
)

一些警告:

  • 为简单起见,该函数假定存储桶中的所有对象都在同一存储类中!
  • 在macOS上,使用gnumfmt代替numfmt
  • 如果numfmt抱怨无效的--format选项,请升级GNU coreutils以获得浮点精度支持。