Get S3 object size with AWS SDK for Ruby

I used it to get the video size in HLS format!

Class: Aws::S3::Client

    
client = AWS::S3::new

size = 0
options = { bucket: [Bucket name], prefix: [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 makes it even easier Get the total number of files and file size on S3 using AWS CLI

$ aws s3 ls s3://[Bucket name]/[Folder name]/  --recursive --human --sum

I regret that I should have written it in Lambda after implementation.

Recommended Posts

Get S3 object size with AWS SDK for Ruby
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
AWS SDK for Ruby profile change & assume role
Try local development of AWS (S3, DynamoDB) with AWS SDK for JavaScript and Docker
Notify Slack of AWS bills daily with Lambda for Ruby
AWS SDK for Java 1.11.x and 2.x
Encrypt data uploaded to S3 using AWS SDK for Java / SSE-KMS
Upload / download / bulk delete data to S3 using Amazon S3 Client Builder with AWS SDK for Java
Learn AWS for free with LocalStack
[AWS SDK for Java] Set a retry policy on the S3 client
Getting Started with Ruby for Java Engineers
Get Timestamp with Azure BlobStorage Java SDK
Get a list of Qiita articles for a specific user with Ruby + Qiita API v2
Procedure for operating google spreadsheet with API (ruby)
Get block information with Hyperledger Iroha's Java SDK
Try to get redmine API key with ruby
Set a signed cookie (for CloudFront) with a custom policy using the AWS SDK for Java