2013-07-10 52 views
2

我正在使用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>' 

回答

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

+0

所以,我想你的建議,但得到了一個錯誤: 'EXCON ::錯誤::錯誤請求:預期(200)<=>實際(400錯誤的請求)' 我將修改我原來的職位,包括錯誤,以及我個人更新的意思示例 – oolong

+0

感謝您的更新。我發現至少有一個問題(我忽略了您需要指定'REPLACE',因爲默認情況下它將使用舊的元數據並忽略在複製請求中傳遞的任何內容)。 此外,錯誤的更多細節將是方便的。你能否更新你的霧版,然後再試一次?我不知道它會修復它,但它至少應該提供更詳細的錯誤信息。謝謝! – geemus

+1

啊哈!是的指定替換固定的錯誤,但它仍然是我以前遇到的同樣的問題。通過循環打印輸出複製,但所有文件的內容類型仍然爲零,並且單獨更新它們似乎可以正常工作,但不能達到aws – oolong

0

我發現這個解決方法,以獲取內容類型:

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

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