如何使用一次Fog调用在存储桶中创建S3对象?

时间:2012-09-05 19:05:17

标签: ruby fog

以下是Fog walkthrough of creating a file (an S3 object) in a directory (an S3 bucket)

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

directory = connection.directories.create(
  :key    => "fog-demo-#{Time.now.to_i}", # globally unique name
  :public => true
)

file = directory.files.create(
  :key    => 'resume.html',
  :body   => File.open("/path/to/my/resume.html"),
  :public => true
)

但它看起来好像这需要2个API调用:

  1. connection.directories.create
  2. directory.files.create
  3. 如果我已经创建了目录(S3存储桶),如何创建只有一个Fog调用的文件(S3对象)?

2 个答案:

答案 0 :(得分:16)

如果您知道该目录存在,则可以

dir = connection.directories.new(:key => 'foo')# no request made
dir.files.create(...)

答案 1 :(得分:3)

或者,如果您已经有一个存储文件的存储桶,那么您可以执行以下操作:

bucket = connection.directories.get({BUCKET_NAME})

之后,您将能够调用bucket.files.create来存储该存储桶上的文件。

希望它有所帮助!