在s3上设置Fog存储文件的content_type

时间:2013-07-10 19:15:35

标签: amazon-s3 content-type ruby-on-rails-4 fog

我正在与Fog和Amazon s3合作管理视频和图像文件。我在设置文件的content_type时遇到了很多麻烦。

从控制台工作时,我可以通过并单独更新每个文件的content_type,然后运行save。但是,当我尝试对特定目录中的所有文件运行更新时,我没有收到错误,但没有任何更新。我运行了多种不同的方法,所有方法都具有相同的基本思想,并且所有设置都打印“已保存!”如果文件保存。方法运行正常并打印出“已保存!”,但当我返回并检查文件时,content_type仍为零。

以下是我正在做的一个例子:

directory.files.each do |f|
  case f.key.split(".").last
  when "jpg"
    f.content_type = "image/jpeg"
    puts "saved!" if f.save
  when "mov"
    f.content_type = "video/quicktime"
    puts "saved!" if f.save
  end
end

此外,当我浏览并单独更新每个文件时,保存工作并且content_type会更新,但数据不会保留。

例如:

file = directory.files.first
file.content_type = 'video/quicktime'
file.save         # returns true
file.content_type # returns 'video/quicktime'

但是,当我在AWS中检查文件时,内容类型仍为零。

是否有更好的(持久的)方法来更新Fog s3文件上的content_type?我觉得我必须以错误的方式解决这个问题。

更新: 尝试使用文件#copy方法:

directory.files.each do |f|
  content_type = case f.key.split(".").last
  when "jpg"
    "image/jpeg"
  when "mov"
    "video/quicktime"
  end
  puts "copied!" if f.copy(f.directory.key, f.key, { 'Content-Type' => content_type })
end

我收到了一个错误:

Excon::Errors::BadRequest: Expected(200) <=> Actual(400 Bad Request)

from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/middlewares/expects.rb:6:in `response_call'
from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/connection.rb:355:in `response'
from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/connection.rb:249:in `request'
from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/core/connection.rb:21:in `request'
from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/storage.rb:506:in `request'
from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/requests/storage/copy_object.rb:33:in `copy_object'
from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/models/storage/file.rb:93:in `copy'
from (irb):14
from /Users/marybethlee/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'

2 个答案:

答案 0 :(得分:5)

如果您只是更新元数据(而不是正文/内容本身),您可能希望使用复制而不是保存。这可能是不明显的,但是它会使操作保持在S3侧,以便它可以更快。

副本的签名如下:

copy(target_directory_key, target_file_key, options = {})

所以我认为我提出的解决方案应该(或多或少)看起来像这样:

directory.files.each do |f|
  content_type = case f.key.split(".").last
  when "jpg"
    "image/jpeg"
  when "mov"
    "video/quicktime"
  end
  options = {
    'Content-Type' => content_type,
    'x-amz-metadata-directive' => 'REPLACE'
  }
  puts "copied!" if f.copy(f.directory, f.key, options)
end

那应该基本上告诉S3“将我的文件复制到自己的顶部,但是更改此标题”。这样您就不必下载/重新上载文件了。这可能就是你想要的方法。

所以,除了解决方案,似乎你仍然发现了一个错误。你可以在“单独更新每个文件”中包含一个例子吗?只是想确保我确切地知道你的意思,并且我可以并肩看到工作/非工作案例。此外,您认为它是如何/为什么不更新内容类型(它实际上可能正在更新它,但只是没有正确显示更新的值,或类似的东西)。如果您可以在此处创建问题以确保我不会忘记解决问题,则可获得奖励积分:https://github.com/fog/fog/issues?state=open

答案 1 :(得分:0)

我找到了解决方法来获取内容类型:

directory.files.head(file.key).content_type

其中file.key是您文件的名称。