Before running the below script, ensure you have gem 'aws-sdk-s3' installed on your system.

## require the gem in console
require 'aws-sdk-s3'

Aws.config.update(
  region: ENV['AWS_REGION'],
  credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
)

bucket1 = Aws::S3::Bucket.new('source-bucket')
bucket2 = Aws::S3::Bucket.new('destination-bucket')
#### keep both buckets as same, for copying in intra bucket

errors = []
Document.each do |document|
  source = "#{document.user_id}/#{document.type}-#{document.filename}" ## new file path, which doesn't exist yet
  destination = "#{document.user_id}/#{document.type}/#{document.category}/#{document.id}/#{document.filename}" ## source file path which exists
  
  obj = bucket2.object(destination)
  obj.copy_from(bucket1.object(source))
rescue StandardError => _err
  puts "--------------------error with document id: #{document.id}---------------------------------"
  errors << document.id
end