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

I have a bunch of files on s3. I have fog with a .fog configuration file, so I can start fogand get a hint. Now how do I access and edit a file on s3 if I know its path?

+5
source share
1 answer

The easiest way to do this is perhaps to use IRB or PRY to get a local copy of the file, or write a simple script to load, edit, and download it later. Suppose you have a file called 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")

, .

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

, , S3.

file = directory.files.get('data.txt')
file.body = File.open("/path/to/my/data.txt")
file.save
+10

All Articles