Skip to content

Instantly share code, notes, and snippets.

@hopewise
Created February 27, 2019 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopewise/7c10ffc36033b4ee2335eedde7453bd0 to your computer and use it in GitHub Desktop.
Save hopewise/7c10ffc36033b4ee2335eedde7453bd0 to your computer and use it in GitHub Desktop.
ruby method to copy from a folder to another folder within the same bucket
tested with `gem 'aws-sdk', '~> 2'`
```
@s3 = Aws::S3::Client.new(
region: ENV['AWS_REGION'],
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_KEY']
)
copy_files_s3("my-bucket", "folder_a", "folder_b")
def copy_files_s3(bucket_name, source_folder, destination_folder)
list= @s3.list_objects_v2({bucket: bucket_name, max_keys: 1000, prefix: source_folder})
list.contents.each do |source_object|
source_object = source_object.key
new_file_name = source_object.sub(source_folder, "")
esp = @s3.copy_object({ bucket: bucket_name, copy_source: "#{bucket_name}/#{source_object}", key: "#{destination_folder}#{new_file_name}"})
end
end
```
@hopewise
Copy link
Author

hopewise commented Feb 27, 2019

use token to iterate further 1000's when more than 999 file in the folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment