ruby/example_code/s3/s3-ruby-example-upload-item-to-folder.rb
# Checks whether a folder exists in a bucket in Amazon S3.
#
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
# @param bucket_name [String] The name of the bucket.
# @param folder_name [String] The name of the folder.
# @return [Boolean] true if the folder exists; otherwise, false.
# @example
#   s3_client = Aws::S3::Client.new(region: 'us-west-2')
#   exit 1 unless folder_exists?(s3_client, 'doc-example-bucket', 'my-folder/')
Checks whether a folder exists in a bucket in Amazon S3.
Checks whether a folder exists in a bucket in Amazon S3 .
def folder_exists?(s3_client, bucket_name, folder_name)
  response = s3_client.list_objects_v2(bucket: bucket_name)
  if response.count.positive?
    response.contents.each do |object|
      return true if object.key == folder_name
    end
  end
rescue StandardError => e
  puts "Error while checking whether the folder '#{folder_name}' " \
    "exists: #{e.message}"
end
def folder_exists? ( s3_client , bucket_name , folder_name ) response = s3_client . list_objects_v2 ( bucket : bucket_name ) if response . count . positive? response . contents . each do | object | return true if object . key == folder_name end end rescue StandardError => e puts "Error while checking whether the folder '#{folder_name}' " "exists: #{e.message}" end
Checks whether a folder exists in a bucket in Amazon S3.
Checks whether a folder exists in a bucket in Amazon S3 .
true if the folder exists; otherwise, false.
true if the folder exists ; otherwise false .
Boolean
s3_client
An initialized Amazon S3 client.
An initialized Amazon S3 client .
bucket_name
The name of the bucket.
The name of the bucket .
folder_name
The name of the folder.
The name of the folder .
example
s3_client = Aws::S3::Client.new(region: 'us-west-2') exit 1 unless folder_exists?(s3_client, 'doc-example-bucket', 'my-folder/')
s3_client = Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) exit 1 unless folder_exists? ( s3_client ' doc - example - bucket ' ' my - folder / ' )