ruby/example_code/s3/copy_object_between_buckets.rb
# Copies an object from one Amazon S3 bucket to another.
#
# Prerequisites:
#
# - Two S3 buckets (a source bucket and a target bucket).
# - An object in the source bucket to be copied.
#
# @param s3_client [Aws::S3::Client] An initialized Amazon S3 client.
# @param source_bucket_name [String] The source bucket's name.
# @param source_key [String] The name of the object
#   in the source bucket to be copied.
# @param target_bucket_name [String] The target bucket's name.
# @param target_key [String] The name of the copied object.
# @return [Boolean] true if the object was copied; otherwise, false.
# @example
#   s3_client = Aws::S3::Client.new(region: 'us-east-1')
#   exit 1 unless object_copied?(
#     s3_client,
#     'doc-example-bucket1',
#     'my-source-file.txt',
#     'doc-example-bucket2',
#     'my-target-file.txt'
#   )
Copies an object from one Amazon S3 bucket to another.
Prerequisites.

Two S3 buckets (a source bucket and a target bucket).
An object in the source bucket to be copied.
Copies an object from one Amazon S3 bucket to another . Prerequisites . Two S3 buckets ( a source bucket and a target bucket ) . An object in the source bucket to be copied .
def object_copied?(
  s3_client,
  source_bucket_name,
  source_key,
  target_bucket_name,
  target_key)
  return true if s3_client.copy_object(
    bucket: target_bucket_name,
    copy_source: source_bucket_name + '/' + source_key,
    key: target_key
  )
rescue StandardError => e
  puts "Error while copying object: #{e.message}"
end
def object_copied? ( s3_client , source_bucket_name , source_key , target_bucket_name , target_key ) return true if s3_client . copy_object ( bucket : target_bucket_name , copy_source : source_bucket_name + '/' + source_key , key : target_key ) rescue StandardError => e puts "Error while copying object: #{e.message}" end
Copies an object from one Amazon S3 bucket to another.
Copies an object from one Amazon S3 bucket to another .
true if the object was copied; otherwise, false.
true if the object was copied ; otherwise false .
Boolean
s3_client
An initialized Amazon S3 client.
An initialized Amazon S3 client .
source_bucket_name
The source bucket's name.
The source bucket ' s name .
source_key
The name of the object in the source bucket to be copied.
The name of the object in the source bucket to be copied .
target_bucket_name
The target bucket's name.
The target bucket ' s name .
target_key
The name of the copied object.
The name of the copied object .
example
s3_client = Aws::S3::Client.new(region: 'us-east-1') exit 1 unless object_copied?( s3_client, 'doc-example-bucket1', 'my-source-file.txt', 'doc-example-bucket2', 'my-target-file.txt' )
s3_client = Aws :: S3 :: Client . new ( region : ' us - east - 1 ' ) exit 1 unless object_copied? ( s3_client ' doc - example - bucket1 ' ' my - source - file . txt ' ' doc - example - bucket2 ' ' my - target - file . txt ' )