Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename Object in S3 using Ruby

I want to rename an item in s3 using the Ruby sdk. How do I do this?

I have tried:

require 'aws-sdk'
s3 = AWS.config(
        :region => 'region',
        :access_key_id => 'key',
        :secret_access_key => 'key'
)

b = AWS::S3::Bucket.new(client: s3, name: 'taxalli')


    b.objects.each do |obj|
       obj.rename_to('imports/files/' + line.split(' ').last.split('/').last)
      end

But I dont see anything in the new sdk for moves or renames.

like image 823
Ben Nelson Avatar asked Dec 05 '22 21:12

Ben Nelson


1 Answers

In AWS-SDK version 2 is now method called "move_to" (http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#move_to-instance_method) which you can use in this case. Technically it will still copy & delete that file on S3 but you don't need to copy & delete it manually and most importantly it will not delete that file if that copy action fails from some reason.

like image 115
Daniel Dimitrov Avatar answered Dec 22 '22 00:12

Daniel Dimitrov