ruby/example_code/s3/s3_ruby_create_bucket.rb
# 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 . Full example call .
def run_me
  bucket_name = 'doc-example-bucket'
  object_key = 'my-file.txt'
  object_content = 'This is the content of my-file.txt.'
  target_bucket_name = 'doc-example-bucket1'
  target_object_key = 'my-file-1.txt'
  region = 'us-west-2'
  s3_client = Aws::S3::Client.new(region: region)
  puts 'Available buckets:'
  list_buckets(s3_client)
  if bucket_created?(s3_client, bucket_name)
    puts "Bucket '#{bucket_name}' created."
  else
    puts "Bucket '#{bucket_name}' not created. Program will stop."
    exit 1
  end
  if bucket_created?(s3_client, target_bucket_name)
    puts "Bucket '#{target_bucket_name}' created."
  else
    puts "Bucket '#{target_bucket_name}' not created. Program will stop."
    exit 1
  end
  if object_uploaded?(s3_client, bucket_name, object_key, object_content)
    puts "Object '#{object_key}' uploaded to bucket '#{bucket_name}'."
  else
    puts "Object '#{object_key}' uploaded to bucket '#{bucket_name}'. " \
      'Program will stop.'
    exit 1
  end
  puts "Objects in bucket '#{bucket_name}':"
  list_objects(s3_client, bucket_name)
  if object_copied?(
    s3_client,
    bucket_name,
    object_key,
    target_bucket_name,
    target_object_key
  )
    puts "Object '#{object_key}' copied to bucket '#{target_bucket_name}' " \
      "as object '#{target_object_key}'."
  else
    puts "Object '#{object_key}' not copied to bucket '#{target_bucket_name}' " \
    "as object '#{target_object_key}'."
  end
  if object_deleted?(s3_client, bucket_name, object_key)
    puts "Object '#{object_key}' deleted from bucket '#{bucket_name}'."
  else
    puts "Object '#{object_key}' not deleted from bucket '#{bucket_name}'."
  end
end
def run_me bucket_name = 'doc-example-bucket' object_key = 'my-file.txt' object_content = 'This is the content of my-file.txt.' target_bucket_name = 'doc-example-bucket1' target_object_key = 'my-file-1.txt' region = 'us-west-2' s3_client = Aws :: S3 :: Client . new ( region : region ) puts 'Available buckets:' list_buckets ( s3_client ) if bucket_created? ( s3_client , bucket_name ) puts "Bucket '#{bucket_name}' created." else puts "Bucket '#{bucket_name}' not created. Program will stop." exit 1 end if bucket_created? ( s3_client , target_bucket_name ) puts "Bucket '#{target_bucket_name}' created." else puts "Bucket '#{target_bucket_name}' not created. Program will stop." exit 1 end if object_uploaded? ( s3_client , bucket_name , object_key , object_content ) puts "Object '#{object_key}' uploaded to bucket '#{bucket_name}'." else puts "Object '#{object_key}' uploaded to bucket '#{bucket_name}'. " 'Program will stop.' exit 1 end puts "Objects in bucket '#{bucket_name}':" list_objects ( s3_client , bucket_name ) if object_copied? ( s3_client , bucket_name , object_key , target_bucket_name , target_object_key ) puts "Object '#{object_key}' copied to bucket '#{target_bucket_name}' " "as object '#{target_object_key}'." else puts "Object '#{object_key}' not copied to bucket '#{target_bucket_name}' " "as object '#{target_object_key}'." end if object_deleted? ( s3_client , bucket_name , object_key ) puts "Object '#{object_key}' deleted from bucket '#{bucket_name}'." else puts "Object '#{object_key}' not deleted from bucket '#{bucket_name}'." end end
Replace us-west-2 with the AWS Region you're using for Amazon S3.
Replace us - west - 2 with the AWS Region you ' re using for Amazon S3 .