Created
February 27, 2019 18:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use token to iterate further 1000's when more than 999 file in the folder