使用Rails插件Paperclip上传视频到亚马逊 S3服务

本文介绍如何利用Amazon S3与Rails应用结合Paperclip插件实现视频文件的上传功能。通过具体的代码示例,展示了从创建模型、配置S3、安装所需插件到实现Controller与View的具体步骤。
云服务里亚马逊的S3 和 EC2比较常用的,这里是介绍一个上传插件和S3一起使用的例子。

创建model

#video.rb
class Video < ActiveRecord::Base

# Paperclip
# http://www.thoughtbot.com/projects/paperclip
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style/:basename.:extension",
:bucket => 'yourbucketname'

# Paperclip Validations
validates_attachment_presence :video
validates_attachment_content_type :video, :content_type => ['application/x-shockwave-flash', 'application/x-shockwave-flash', 'application/flv', 'video/x-flv']
end

配置S3相关的配置


#config/s3.yml
access_key_id: YOURSECRETACCESSID
secret_access_key: YOUR-secRE/TACCe\ssKEy


添加插件
Rails::Initializer.run do |config|
...
config.gem 'right_aws', :version => '1.9.0'
...
end


执行
sudo rake gems:install 
sudo rake gems:unpack


Controller和view会如下:

class VideosController < ApplicationController

def index
@videos = Video.find :all
end

def new
@video = Video.new
end

def create
@video = Video.new(params[:video])
if @video.save
flash[:notice] = 'Video has been uploaded'
redirect_to :action => 'index'
else
render :action => 'new'
end
end

def show
@video = Video.find(params[:id])
end

end


# index view
<h2>Videos (<%= link_to 'Create new', new_video_path %>)</h2>

<ul>
<% for video in @videos %>
<li><%= video.video_file_name %> <%= video.video.url %> <%= link_to 'view', video_path(video) %></li>
<% end %>
</ul>

# new view
<% form_for(@video, :html => {:multipart => true, :class => 'styledform' }) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :video %><br />
<%= f.file_field :video %>
</p>
<p>
<%= f.submit %> or <%= link_to 'Cancel', videos_path %>
</p>
<% end %>

# show view
<div id="player">
You need to have <%= link_to 'Flash Player', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' %> installed to display this video
</div>

<script type="text/javascript">
var so = new SWFObject('/flash/player.swf', 'mpl', '480', '360', '8');
so.addParam('allowscriptaccess', 'always');
so.addParam('allowfullscreen', 'true');
so.addVariable('height', '360');
so.addVariable('width', '480');
so.addVariable('file', '<%= @video.video.url %>');
so.write('player');
</script>


配置flash player显示视频
[list=1]
[*]下载JW FLV MEDIA PLAYER ([url=http://scottmotte.com/wp-content/uploads/2008/11/tmediaplayer.zip]mediaplayer.zip[/url])
[*]复制player.swf到public/flash/player.swf
[*]复制swfobject.js 到 public/javascript/swfobject.js
[*]修改配置application.html.erb加载swfobject.js
[/list]

<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title><%= SITE_NAME %></title>
<%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'swfobject' %>
</head>


这样你就可以在http://localhost:3000/videos进行上传操作了


关于开发环境和生产环境

通过在配置yml来区分开发和生产环境往往是不错的办法
当然,这里有一个前提,就是有一个settings.yml用来加载load_config.rb file
设置如下:

 APP_CONFIG = YAML.load_file(“#{RAILS_ROOT}/config/settings.yml”)[RAILS_ENV].symbolize_keys



#In config/settings.yml
development: &non_production_settings
site_url: http://localhost:3000
site_name: Site
admin_email: youremail@domain.com
bucket: yourappdevelopment

test:
<<: *non_production_settings

production:
site_url: http://domain.com
site_name: Site
admin_email: app@domain.com
bucket: yourappproduction


#In video.rb
class Video < ActiveRecord::Base

# Paperclip
# http://www.thoughtbot.com/projects/paperclip
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style/:basename.:extension",
:bucket => APP_CONFIG[:bucket]
end



[url=http://scottmotte.com/archives/181.html] 出处[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值