ruby/example_code/s3/s3-ruby-example-upload-item-to-folder.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
  file_name = 'my-file-1.txt'
  bucket_name = 'doc-example-bucket'
  folder_name = 'my-folder/'
  region = 'us-west-2'
  s3_client = Aws::S3::Client.new(region: region)
  puts 'Checking whether the specified file exists and is indeed a file...'
  if file_exists_and_file?(file_name)
    puts "The file '#{file_name}' exists and is a file."
  else
    puts "The file '#{file_name}' does not exist or is not a file and will " \
        'not be uploaded. 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 "\nChecking whether the specified folder exists..."
  if folder_exists?(s3_client, bucket_name, folder_name)
    puts "The folder '#{folder_name}' exists."
  else
    puts "The folder '#{folder_name}' does not exist in the bucket " \
      "'#{bucket_name}' or access to the bucket is denied. Stopping program."
    exit 1
  end
  puts "\nUploading file..."
  if upload_file_to_folder?(s3_client, bucket_name, folder_name, file_name)
    puts "The file '#{file_name}' was uploaded."
  else
    puts "The file '#{file_name}' could not be uploaded. Stopping program."
    exit 1
  end
end
def run_me file_name = 'my-file-1.txt' bucket_name = 'doc-example-bucket' folder_name = 'my-folder/' region = 'us-west-2' s3_client = Aws :: S3 :: Client . new ( region : region ) puts 'Checking whether the specified file exists and is indeed a file...' if file_exists_and_file? ( file_name ) puts "The file '#{file_name}' exists and is a file." else puts "The file '#{file_name}' does not exist or is not a file and will " 'not be uploaded. 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 "\nChecking whether the specified folder exists..." if folder_exists? ( s3_client , bucket_name , folder_name ) puts "The folder '#{folder_name}' exists." else puts "The folder '#{folder_name}' does not exist in the bucket " "'#{bucket_name}' or access to the bucket is denied. Stopping program." exit 1 end puts "\nUploading file..." if upload_file_to_folder? ( s3_client , bucket_name , folder_name , file_name ) puts "The file '#{file_name}' was uploaded." else puts "The file '#{file_name}' could not be 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 .