Upload videos to Amazon S3 using ruby with sinatra 마지막으로 마감하기 전에이 작업을 완료하는 시간을 가졌다 :) 누군가가 비슷한 문제가있는 경우, 나는 그들이이 대답에서 영감으로 사용할 수 있기를 바랍니다.
#!/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