I'm working with Fog and Amazon s3 to manage video and image files. I've been running into a lot of trouble with setting the content_type for my files.
When working from the console, I am able to go through and individually update each file's content_type, and then run save. However, when I try to run an update on all of the files within a specific directory, I don't get an error, but nothing gets updated. I've run multiple different methods, all with the same basic idea, and all set to print "saved!" if the file saves. The methods run properly and print out "saved!", but when I go back and check the files, the content_type is still nil.
Here's an example of what I'm doing:
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
Also, when I go through and individually update each file, the save works and the content_type gets updated, but the data doesn't persist.
For example:
file = directory.files.first
file.content_type = 'video/quicktime'
file.save # returns true
file.content_type # returns 'video/quicktime'
However, when I go check the file in AWS, the content type is still nil.
Is there a better (persistent) way of going about updating content_type on Fog s3 files? I feel like I must be going about this the wrong way.
Update: Tried using the file#copy method:
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
I got an error:
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>'