Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use fog to edit a file on s3?

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?

like image 650
John Bachir Avatar asked Nov 21 '11 09:11

John Bachir


People also ask

What is storage fog?

fog supports storing files with AWS, Google, Local and Rackspace. You need to setup some options to configure your usage: :fog_credentials. host info and credentials for service.

How do I upload or store files on Amazon s3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.


1 Answers

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
like image 85
Pan Thomakos Avatar answered Oct 24 '22 15:10

Pan Thomakos