ruby/example_code/s3/auth_federation_token_request_test.rb
# Lists the keys and ETags for the objects in an Amazon S3 bucket.
#
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
# @param bucket_name [String] The bucket's name.
# @return [Boolean] true if the objects were listed; otherwise, false.
# @example
#   s3_client = Aws::S3::Client.new(region: 'us-west-2')
#   exit 1 unless list_objects_in_bucket?(s3_client, 'doc-example-bucket')
Lists the keys and ETags for the objects in an Amazon S3 bucket.
Lists the keys and ETags for the objects in an Amazon S3 bucket .
def list_objects_in_bucket?(s3_client, bucket_name)
  puts "Accessing the contents of the bucket named '#{bucket_name}'..."
  response = s3_client.list_objects_v2(
    bucket: bucket_name,
    max_keys: 50
  )
  if response.count.positive?
    puts "Contents of the bucket named '#{bucket_name}' (first 50 objects):"
    puts 'Name => ETag'
    response.contents.each do |obj|
      puts "#{obj.key} => #{obj.etag}"
    end
  else
    puts "No objects in the bucket named '#{bucket_name}'."
  end
  return true
rescue StandardError => e
  puts "Error while accessing the bucket named '#{bucket_name}': #{e.message}"
end
def list_objects_in_bucket? ( s3_client , bucket_name ) puts "Accessing the contents of the bucket named '#{bucket_name}'..." response = s3_client . list_objects_v2 ( bucket : bucket_name , max_keys : 50 ) if response . count . positive? puts "Contents of the bucket named '#{bucket_name}' (first 50 objects):" puts 'Name => ETag' response . contents . each do | obj | puts "#{obj.key} => #{obj.etag}" end else puts "No objects in the bucket named '#{bucket_name}'." end return true rescue StandardError => e puts "Error while accessing the bucket named '#{bucket_name}': #{e.message}" end
Lists the keys and ETags for the objects in an Amazon S3 bucket.
Lists the keys and ETags for the objects in an Amazon S3 bucket .
true if the objects were listed; otherwise, false.
true if the objects were listed ; otherwise false .
Boolean
s3_client
An initialized Amazon S3 client.
An initialized Amazon S3 client .
bucket_name
The bucket's name.
The bucket ' s name .
example
s3_client = Aws::S3::Client.new(region: 'us-west-2') exit 1 unless list_objects_in_bucket?(s3_client, 'doc-example-bucket')
s3_client = Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) exit 1 unless list_objects_in_bucket? ( s3_client ' doc - example - bucket ' )