如何使用雾编辑在S3上的文件?如何使用、编辑、文件

2023-09-11 23:45:27 作者:酒归

我对S3一堆文件。我有雾设置了一个.fog配置文件,所以我可以启动,并得到一个提示。现在,我该如何访问和编辑在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?

推荐答案

做最简单的事情可能是使用内部评级法或PRY获得文件的本地副本,或者写一个简单的脚本,下载,编辑,然后再-upload它。假设你有一个名为data.txt中的文件。

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.

您可以使用下面的脚本来初始化到S3的连接。

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

用你喜欢的编辑器编辑文件,然后把它上传到S3了。

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