ruby/example_code/s3/s3-ruby-example-upload-multiple-items.rb
# Full example call:
# Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call:
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call : Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .
def run_me
  proposed_file_names = ['my-file-1.txt', 'my-file-2.txt']
  existing_file_names = []
  uploaded_file_names = []
  bucket_name = 'doc-example-bucket'
  region = 'us-west-2'
  s3_client = Aws::S3::Client.new(region: region)
  puts 'Checking whether the specified files exist and are indeed files...'
  proposed_file_names.each do |file_name|
    if file_exists_and_file?(file_name)
      puts "The file '#{file_name}' exists and is a file."
      existing_file_names.push(file_name)
    else
      puts "The file '#{file_name}' does not exist or is not a file and will " \
        'not be uploaded.'
    end
  end
  if existing_file_names.count.positive?
    puts "\nThe list of existing file names is:"
    puts existing_file_names
  else
    puts "\nNone of the specified files exist. Stopping program."
    exit 1
  end
  puts "\nChecking whether the specified bucket exists..."
  if bucket_exists?(s3_client, bucket_name)
    puts "The bucket '#{bucket_name}' exists."
  else
    puts "The bucket '#{bucket_name}' does not exist. Stopping program."
    exit 1
  end
  puts "\nUploading files..."
  existing_file_names.each do |file_name|
    if upload_file_to_bucket?(s3_client, bucket_name, file_name)
      puts "The file '#{file_name}' was uploaded."
      uploaded_file_names.push(file_name)
    else
      puts "The file '#{file_name}' could not be uploaded."
    end
  end
  if uploaded_file_names.count.positive?
    puts "\nThe list of uploaded file names is:"
    puts uploaded_file_names
  else
    puts "\nNone of the existing files were uploaded. Stopping program."
    exit 1
  end
end
def run_me proposed_file_names = [ 'my-file-1.txt' , 'my-file-2.txt' ] existing_file_names = [ ] uploaded_file_names = [ ] bucket_name = 'doc-example-bucket' region = 'us-west-2' s3_client = Aws :: S3 :: Client . new ( region : region ) puts 'Checking whether the specified files exist and are indeed files...' proposed_file_names . each do | file_name | if file_exists_and_file? ( file_name ) puts "The file '#{file_name}' exists and is a file." existing_file_names . push ( file_name ) else puts "The file '#{file_name}' does not exist or is not a file and will " 'not be uploaded.' end end if existing_file_names . count . positive? puts "\nThe list of existing file names is:" puts existing_file_names else puts "\nNone of the specified files exist. Stopping program." exit 1 end puts "\nChecking whether the specified bucket exists..." if bucket_exists? ( s3_client , bucket_name ) puts "The bucket '#{bucket_name}' exists." else puts "The bucket '#{bucket_name}' does not exist. Stopping program." exit 1 end puts "\nUploading files..." existing_file_names . each do | file_name | if upload_file_to_bucket? ( s3_client , bucket_name , file_name ) puts "The file '#{file_name}' was uploaded." uploaded_file_names . push ( file_name ) else puts "The file '#{file_name}' could not be uploaded." end end if uploaded_file_names . count . positive? puts "\nThe list of uploaded file names is:" puts uploaded_file_names else puts "\nNone of the existing files were uploaded. Stopping program." exit 1 end end
Full example call:
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Full example call : Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .