ruby脚本请求视频并将其上传到s3

时间:2014-09-10 12:21:42

标签: ruby video amazon-s3 fog

问题:将一些视频从openstack(swift)传输到s3

宝石:雾,aws-sdk

我有一系列路径,如:

videos_paths = ["videos/attachments/5142/9f988f89ds9f8/lecture.mp4", "videos/attachments/3134/lecture2.mp4" ..... ]

我根据这些路径为视频创建了网址。

我的问题是如何将视频直接“下载”到S3存储桶,以及是否有任何方法可以根据视频路径创建目录结构。

E.g。

视频:https://myproject.com:443/v1/AUTH_a0fffc9ea361409795fb2e9736012940/production_videos/videos%2Fattachments%2F18116%2Fd6a5bd77a3b203cddsfb0c9d%2Foriginal%2Flecture.mp4?temp_url_sig=dce06f61775f24e88c80bed803b808668b073ed0&temp_url_expires=141243074

工作流程:请求视频 - >将其发送到S3并将其存储在类似的目录结构中

我接受任何消极和想法。如果我可以使用其他宝石,或者可以用其他方式完成。

谢谢,

我已经检查过了:

1:Uploading Videos to S3 with Carrierwave and Fog

2:Upload videos to Amazon S3 using ruby with sinatra

1 个答案:

答案 0 :(得分:0)

最后有时间在截止日期之前完成这项任务:)如果有人有类似的问题,我希望他们可以使用这个答案的一些东西作为灵感。

#!/usr/bin/env ruby

require 'fog'
require 'aws-sdk'
require 'open-uri'

videos_paths = ["videos/attachments/5142/e01a339b41ce487643e85/original/lecture.mp4", "videos/attachments/5143/a4fa624f9324bd9988fcc/original/lecture-only.mp4", "videos/attachments/5144/95141978d5ecc14a1995fc/original/lecture.mp4", .... ] # 282 videos

fog_credentials = {
  "hp_access_key" => "",
  "hp_secret_key" => "",
  "hp_tenant_id" => "",
  "hp_auth_uri" => "",
  "hp_use_upass_auth_style" => true,
  "hp_avl_zone" => "",
  "os_account_meta_temp_url_key" => "",
  "persistent" => false
}

@storage = Fog::Storage::HP.new(fog_credentials) # Connect to fog storage
@my_time = 60 * 60 * 24 * 7 * 4 # 4 week links?

def make_temp_url(path, time = @my_time)
  @storage.generate_object_temp_url("videos", path, time, "GET")
end

def status(path, options = {})
  File.open('./stats.txt', 'a') { |file| file.puts "#{options[:msg]}: #{path}" }
end

s3 = AWS::S3.new(
  :access_key_id => '',
  :secret_access_key => ''
)

bucket = s3.buckets['']

videos_paths.each do |video_path|
  cur_url = make_temp_url(video_path)
  obj = bucket.objects[video_path]

  if obj.exists?
    status(video_path, msg: "Exists")
  else
    begin
      open(cur_url, 'rb') do |video|
        obj.write(video.read)
        status(video_path, msg: "Success")
      end
    rescue
      status(video_path, msg: "Error")
    end
  end
end