我正在使用Fog和Amazon s3來管理視頻和圖像文件。我爲我的文件設置content_type時遇到了很多麻煩。在s3上設置霧存儲文件的content_type
從控制檯工作時,我可以通過並單獨更新每個文件的content_type,然後運行保存。但是,當我嘗試對特定目錄中的所有文件運行更新時,我沒有收到錯誤,但沒有更新。我運行了多種不同的方法,所有方法都基本相同,所有設置都打印「保存」!如果文件保存。這些方法運行正常並打印出「已保存!」,但當我回去檢查文件時,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_3文件中的content_type?我覺得我必須以這種錯誤的方式去做。
更新: 使用文件#試圖複製方法:
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/[email protected]/gems/excon-0.22.1/lib/excon/middlewares/expects.rb:6:in `response_call'
from /Users/marybethlee/.rvm/gems/[email protected]/gems/excon-0.22.1/lib/excon/connection.rb:355:in `response'
from /Users/marybethlee/.rvm/gems/[email protected]/gems/excon-0.22.1/lib/excon/connection.rb:249:in `request'
from /Users/marybethlee/.rvm/gems/[email protected]/gems/fog-1.11.1/lib/fog/core/connection.rb:21:in `request'
from /Users/marybethlee/.rvm/gems/[email protected]/gems/fog-1.11.1/lib/fog/aws/storage.rb:506:in `request'
from /Users/marybethlee/.rvm/gems/[email protected]/gems/fog-1.11.1/lib/fog/aws/requests/storage/copy_object.rb:33:in `copy_object'
from /Users/marybethlee/.rvm/gems/[email protected]/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>'
所以,我想你的建議,但得到了一個錯誤: 'EXCON ::錯誤::錯誤請求:預期(200)<=>實際(400錯誤的請求)' 我將修改我原來的職位,包括錯誤,以及我個人更新的意思示例 – oolong
感謝您的更新。我發現至少有一個問題(我忽略了您需要指定'REPLACE',因爲默認情況下它將使用舊的元數據並忽略在複製請求中傳遞的任何內容)。 此外,錯誤的更多細節將是方便的。你能否更新你的霧版,然後再試一次?我不知道它會修復它,但它至少應該提供更詳細的錯誤信息。謝謝! – geemus
啊哈!是的指定替換固定的錯誤,但它仍然是我以前遇到的同樣的問題。通過循環打印輸出複製,但所有文件的內容類型仍然爲零,並且單獨更新它們似乎可以正常工作,但不能達到aws – oolong