【问题标题】:Set content_type of Fog storage files on s3在 s3 上设置 Fog 存储文件的 content_type
【发布时间】:2013-07-10 19:15:34
【问题描述】:

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

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

这是我正在做的一个例子:

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 中检查文件时,内容类型仍然是 nil。

有没有更好的(持久的)方法来更新 Fog s3 文件上的 content_type?我觉得我一定是走错路了。

更新: 尝试使用 file#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>'

【问题讨论】:

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


    【解决方案1】:

    如果您只是更新元数据(而不是正文/内容本身),您可能希望使用复制而不是保存。这可能不是很明显,但它使操作保持在 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

    【讨论】:

    • 所以我尝试了您的建议,但出现错误:Excon::Errors::BadRequest: Expected(200) &lt;=&gt; Actual(400 Bad Request) 我将编辑我的原始帖子以包含错误,以及我所指的个别更新的示例
    • 感谢您的更新。我发现了至少一个问题(我忽略了您需要指定“REPLACE”,因为默认情况下它将使用旧的元数据并忽略复制请求中传递的任何内容)。此外,更详细的错误信息会很方便。你能更新你的雾版本并再试一次吗?我不知道它会修复它,但它至少应该给出更详细的错误消息。谢谢!
    • 啊哈!是的,指定替换修复了错误,但这仍然是我之前遇到的相同问题。遍历循环打印出副本,但所有文件的内容类型仍然为零,并且单独更新它们似乎可以正常工作,但不符合 aws
    • 那么澄清一下。您提出请求并且它似乎成功了,您如何查找返回 nil 的内容类型?你在做files.all.each {|file| file.content_type}files.get(key).content_type 还是别的什么?只是想确保我知道那一端发生了什么(因为它可能是在 S3 上设置的,但只是没有在客户端正确更新/显示)。特别是我不认为在执行files.all 时默认包含此信息。
    • 你是对的。如果我做directory.files.first.content_type,我会得到零。但是如果我将同一个文件的密钥传递给directory.files.get(key).content_type,它会返回"video/quicktime。这是一个与 S3 相关的问题,还是一个问题?既然我知道发生了什么,我就可以使用它了。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 2018-06-23
      • 2014-08-05
      • 2010-11-10
      • 2013-04-13
      相关资源
      最近更新 更多