在 s3 上设置 mp4 文件的内容类型 [英] setting content-type for mp4 files on s3
问题描述
借助回形针 gem 和 s3 存储,我将用户上传的视频添加到我的 RoRs 站点.出于某种我无法弄清楚的原因,每当用户上传 mp4 文件时,s3 都会将该文件的内容类型设置为 application/mp4
而不是 video/mp4
.
I am adding user uploaded videos to my RoRs site with the help of the paperclip gem and s3 storage. For some reason that I can't figure out, whenever a user uploads an mp4 file, s3 sets content-type for that file as application/mp4
instead of video/mp4
.
请注意,我已经在初始化文件中注册了 mp4 mime 类型:
Note that I have registered mp4 mime type in an initializer file:
Mime::Type.lookup_by_extension('mp4').to_s=>视频/mp4"
这是我的 Post 模型的相关部分:
Here is the relevant part of my Post model:
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename"
validates_attachment_content_type :video,
:content_type => ['video/mp4'],
:message => "Sorry, this site currently only supports MP4 video"
我的回形针和/或 s3 设置中缺少什么.
What am I missing in my paperclip and/or s3 set-up.
####Update#####
由于某种原因超出了我对 Rails 的了解,我的 mp4 包含文件的默认 MIME 类型如下:
For some reason that is beyond my knowledge of Rails, my default mime types for mp4 contained files is the following:
MIME::Types.type_for("my_video.mp4").to_s
=> "[application/mp4, audio/mp4, video/mp4, video/vnd.objectvideo]"
因此,当回形针将 mp4 文件发送到 s3 时,它似乎将文件的 mime 类型标识为第一个默认值,即application/mp4".这就是为什么 s3 将文件标识为具有application/mp4"内容类型的原因.因为我想启用这些 mp4 文件的流式传输,所以我需要回形针来将文件识别为具有视频/mp4"的 MIME 类型.
So, when paperclip send an mp4 file to s3, it seems to identify the file's mime type as the first default, "application/mp4". That is why s3 identifies the file as having a content-type of "application/mp4". Because I want to enable streaming of these mp4 files, I need paperclip to identify the file as having a mime type of "video/mp4".
有没有办法修改回形针(可能在 before_post_process 过滤器中)来实现这一点,或者有没有办法通过 init 文件修改 rails 以将 mp4 文件识别为视频/mp4".如果我可以做任何一个,哪种方式最好.
Is there a way to modify paperclip (maybe in a before_post_process filter) to allow for this, or is there a way to modify rails through an init file to identify mp4 files as being "video/mp4". If I could do either, which way is best.
感谢您的帮助
推荐答案
原来我需要在模型中设置一个默认的 s3 标头 content_type.这对我来说不是最好的解决方案,因为在某些时候我可能会开始允许使用 mp4 以外的视频容器.但它让我继续解决下一个问题.
Turns out that I needed to set a default s3 header content_type in the model. This isn't the best solution for me because at some point I might start allowing video containers other than mp4. But it gets me moving on to the next problem.
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename",
:s3_headers => { "Content-Type" => "video/mp4" }
这篇关于在 s3 上设置 mp4 文件的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!