11

I have a rails 3.2 app. Using fog to store files in S3.

I would like to write a script to rename all of the files that have been uploaded. I can't seem to find any fog documentation in this area.

Is this possible with fog? Do I need another gem?

BM5k
  • 1,210
  • 10
  • 28

2 Answers2

13

What about copying as mentioned in this post? cf. fog's requests sources and fog's models sources.

You should be able to do:

storage.copy_object('old_bucket', 'old_filename', 'new_bucket', 'new_filename')

or

file.copy('new_bucket', 'new_filename')

Destroying the original file after a successful copy remains necessary though.

Adit Saxena
  • 1,617
  • 15
  • 25
Mick F
  • 7,312
  • 6
  • 51
  • 98
  • 2
    The parameters are out of order in the storage.copy_object example above. Should be old_bucket, old_filename, new_bucket, new_filename according to the source at https://github.com/fog/fog/blob/master/lib/fog/aws/requests/storage/copy_object.rb – Melinda Weathers Feb 27 '14 at 23:50
  • @MelindaWeathers, taken into account in the answer. – Mick F Aug 13 '14 at 19:18
  • In this case fog will take advantage of S3's `copy` command and you don't have to transfer the file contents to your local system. Much faster. – jwadsack Sep 07 '15 at 22:40
8

The bad news is you need to do a get/create/destroy

foo = bucket.files.get 'foo'
bar = bucket.files.create :key => 'bar', :body => foo.body
foo.destroy

The good news is if you're doing it from ec2 in the same region it will probably happen as fast as renaming a file on your local computer

pguardiario
  • 53,827
  • 19
  • 119
  • 159