设置内容类型在S3上的MP4文件 [英] setting content-type for mp4 files on s3

查看:261
本文介绍了设置内容类型在S3上的MP4文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加用户上传的视频给我的错误。因此网站的回形针创业板和S3存储的帮助。出于某种原因,我想不通,只要用户上传的MP4文件,S3设置内容类型为文件应用程序/ MP4 而不是视频/ 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:

默:: Type.lookup_by_extension('MP4')。to_s  => 视频/ MP4

Mime::Type.lookup_by_extension('mp4').to_s => "video/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.

####更新#####

出于某种原因,这超出了我的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类型作为第一个默认,应用程序/ MP4。这就是为什么S3将文件标识为具有内容类型应用程序/ 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过滤器),考虑到这一点,或者是有没有办法通过一个初始化文件来修改轨,以确定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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆