Last active
January 14, 2018 14:21
-
-
Save dominiceden/1baf9d52fd1efc240bd7e0486440a179 to your computer and use it in GitHub Desktop.
RUBY: Move AWS S3 Paperclip files using a non-standard region (i.e. not us-east-1)
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
# I had issues with moving S3 files in a non-standard region, such as London | |
# (eu-west-2). The AWS docs don't seem to say that you cannot pass a simple | |
# string as the new_object destination - it must be a Hash that contains a | |
# Aws::S3::Client instance. This is so the region is set correctly - it's passed | |
# in the client instance. Otherwise, you get the error "The bucket you are | |
# attempting to access must be addressed using the specified endpoint". | |
# Tested and working using the Ruby AWS SDK v2.9.5 in January 2018. | |
client = Aws::S3::Client.new(region: 'eu-west-2') | |
resource = Aws::S3::Resource.new(client: client) | |
bucket = resource.bucket(BUCKET_NAME) | |
errors = [] | |
Model.all.each do |item| | |
if item.attachment? # assume using Rails Paperclip model attachments | |
current_object = bucket.object(CURRENT_PATH_STRING) | |
if current_object.exists? | |
new_object = Aws::S3::Object.new( | |
BUCKET_NAME, | |
NEW_PATH_STRING, | |
{client: client} | |
) | |
begin | |
current_object.move_to(new_object) | |
rescue => e | |
errors << "Error moving file: - #{e.message}" | |
end | |
end | |
end | |
end | |
p errors | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment