5

I have a bunch of files on s3. I have fog set up with a .fog config file so I can fire up fog and get a prompt. Now how do I access and edit a file on s3, if I know its path?

John Bachir
  • 22,495
  • 29
  • 154
  • 227

1 Answers1

11

The easiest thing to do is probably to use IRB or PRY to get a local copy of the file, or write a simple script to download, edit and then re-upload it. Assume you have a file named data.txt.

You can use the following script to initialize a connection to S3.

require 'fog'

connection = Fog::Storage.new({
  :provider                 => 'AWS',
  :aws_secret_access_key    => YOUR_SECRET_ACCESS_KEY,
  :aws_access_key_id        => YOUR_SECRET_ACCESS_KEY_ID
})

directory = connection.directories.get("all-my-data")

Then use the directory object to get a copy of your file on your local file-system.

local_file = File.open("/path/to/my/data.txt", "w")
file = directory.files.get('data.txt')
local_file.write(file.body)
local_file.close

Edit the file using your favorite editor and then upload it to S3 again.

file = directory.files.get('data.txt')
file.body = File.open("/path/to/my/data.txt")
file.save
Pan Thomakos
  • 34,082
  • 9
  • 88
  • 85
  • I imagine there's a way to change a file's properties without re-uploading it, no? At any rate -- what you provided gets me the fog environment I need for experimenting (which isn't very easy to find in their docs). thanks! – John Bachir Nov 21 '11 at 19:23
  • I believe you can set header information without re-uploading a file. You can find more information in the [documentation](http://fog.io/1.1.1/storage/) or by browsing the [source code](http://github.com/fog/fog). – Pan Thomakos Nov 21 '11 at 21:37