How do I rename a file with Fog?
Asked Answered
G

2

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?

Garnetgarnett answered 20/12, 2012 at 21:48 Comment(0)
T
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

Tor answered 21/12, 2012 at 7:56 Comment(0)
R
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.

Reporter answered 29/7, 2013 at 16:35 Comment(3)
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 github.com/fog/fog/blob/master/lib/fog/aws/requests/storage/…Oquendo
@MelindaWeathers, taken into account in the answer.Reporter
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.Ketty
T
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

Tor answered 21/12, 2012 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.