Building on top of @ScottJShea's answer, you can use the with_prefix(prefix)
-method:
size = 0
AWS::S3::Bucket.objects.with_prefix('/user//:id//files/').each do |s3_object|
size += s3_object.content_length
end
or:
AWS::S3::Bucket.objects.with_prefix('/user//:id//files/').inject(0) {
|sum, s3_object| sum + s3_object.content_length
}
Also:
If you are iterating through objects that might be deleted, you will see an exception, you can rescue it like this:
AWS::S3::Bucket.objects.with_prefix('/user//:id//files/').inject(0) {
|sum, s3_object| sum + (s3_object.content_length rescue 0)
}