在此之前,我在 Stack Overflow 上问了另外两个问题,但得到的帮助很少,我想我会问一个悬而未决的问题。我花时间解析 AWS-SDK API 文档,但几乎没有找到直接满足我需求的答案。我也在 AWS 论坛上发过帖子,但在那里没有得到很好的回应。似乎不可能找到一个简单、全面、循序渐进的解决方案。
我完成了什么:
- 使用 CarrierWave 直接上传到 s3。我遵循 Railscast #383 并根据我的需要对其进行了调整。
- 我能够从我的 s3 存储桶中“检索”我的文件。
到目前为止我所做的详细信息:
我使用 Carrierwave-Direct 直接上传到 s3(这利用雾来处理直接上传到 s3)。上传在使用 Sidekiq 的后台作业中处理。将文件放入存储桶后,我只需通过迭代用户上传来检索它,并通过上传的 url 从 s3 调用文件。
这是我迷路的地方:
- 我需要使用 AWS 提供的 Elastic Transcoder 对视频进行转码。
- 我需要从输出存储桶中调用上传/转换的视频。如何从“输出桶”链接到 URL?它是新的 URL 引用还是 URL 与原始“上传 URL”保持一致?
- 我需要将转码后的视频从转码器集成到 Cloudfront 并使用 JWPlayer 显示它们。
- 如何在后台将 API 代码集成到我的上传过程中?
到目前为止,这是我的代码:
我的上传者:
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
end
我处理 s3 详细信息的初始化程序:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: 'AWS_ACCESS_KEY_ID',
aws_secret_access_key: 'AWS_SECRET_ACCESS_KEY',
region: 'us-west-1'}
config.fog_directory = 'video-input'
config.fog_public = false # optional, defaults to true
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {}
end
我的上传模型:
class Upload < ActiveRecord::Base
belongs_to :user
mount_uploader :video, VideoUploader
after_save :enqueue_video
def enqueue_video
VideoWorker.perform_async(id, key) if key.present?
end
class VideoWorker
include Sidekiq::Worker
def perform(id, key)
upload = Upload.find(id)
upload.key = key
video.remote_video_url = upload.video.direct_fog_url(with_path: true)
upload.save!
end
end
end
我的观点:
显示所有用户的上传:
<% @user.uploads.each do |upload| %>
<%= link_to upload.title, upload_url(upload) %>
<% end %>
要显示上传(现在它只是一个下载链接):
<h1><%= @upload.title %></h1>
<p><%= link_to @upload.video_url %></p>
我不认为我的模式或表格是相关的。
我认为代码可能如何工作的类似示例:
我会将此代码添加到 Sidekiq 工作程序中,但我不确定我是否正确执行此操作。我也不确定如何将我的“上传”连接到“转换后的上传”。
upload.update_column 'converted_video',
File.basename(upload.video.path)
transcoder = AWS::ElasticTranscoder::Client.new
transcoder.create_job(
pipeline_id: APP_CONFIG[Rails.env][:pipeline_id],
input: {
key: upload.video.path,
frame_rate: 'auto',
resolution: 'auto',
aspect_ratio: 'auto',
interlaced: 'auto',
container: 'auto'
},
output: {
key: upload.converted_video.path,
preset_id: WEB_M4_PRESET_ID,
thumbnail_pattern: "",
rotate: '0'
}
)
指向一篇有用的文章和有关 Elastic Transcoder 的文档的链接:
http://www.techdarkside.com/getting-started-with-the-aws-elastic-transcoder-api-in-rails
http://docs.aws.amazon.com/sdkforruby/api/Aws/ElasticTranscoder.html