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

ool*_*ong 2 content-type amazon-s3 fog ruby-on-rails-4

我正在与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
Run Code Online (Sandbox Code Playgroud)

此外,当我通过并单独更新每个文件时,保存工作和content_type得到更新,但数据不会持久.

例如:

file = directory.files.first
file.content_type = 'video/quicktime'
file.save         # returns true
file.content_type # returns 'video/quicktime'
Run Code Online (Sandbox Code Playgroud)

但是,当我在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
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

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>'
Run Code Online (Sandbox Code Playgroud)

gee*_*mus 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
Run Code Online (Sandbox Code Playgroud)

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

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