0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS SDK for RubyでS3オブジェクトサイズの取得

Posted at

HLS形式の動画サイズ取得の為に使いました!

※オブジェクトデータ1000件以上を想定しています。

Class: Aws::S3::Client

    
client = AWS::S3::new

size = 0
options = { bucket: [バケット名], prefix: [プレフィックス] }
loop do
  object_list = client.list_objects_v2(options)
  object_list.contents.each do |object|
    size += object.size
  end
  options[:continuation_token] = object_list.next_continuation_token
  break unless object_list.next_continuation_token
end

gigabyte = (size / (2 ** 30).to_f).round(2)

CLIだとさらに簡単
AWS CLIを使ってS3上にあるファイル数とファイルサイズの合計を取得する

$ aws s3 ls s3://[バケット名]/[フォルダ名]/  --recursive --human --sum

実装後にLambdaに書けばよかったなーーと反省。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?