Skip to content

Instantly share code, notes, and snippets.

@joelio
Last active December 24, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelio/f91f0d46aa9de53cd5e3 to your computer and use it in GitHub Desktop.
Save joelio/f91f0d46aa9de53cd5e3 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'aws-sdk'
bucket_name = 'tester'
file_name = 'filename.ts'
AWS.config(
:access_key_id => 'xxxxxx',
:secret_access_key => "xxxxxx",
:s3_force_path_style => true,
:s3_endpoint => 'SERVER_NAME',
:s3_port => 80,
:use_ssl => false
)
def compute_part_size options
max_parts = 10000
min_size = 5242880 #5 MB
estimated_size = options[:estimated_content_length]
[(estimated_size.to_f / max_parts).ceil, min_size].max.to_i
#
end
s3 = AWS::S3.new.buckets[bucket_name]
file = File.open(file_name, 'r', encoding: 'BINARY')
#file_to_upload = "#{s3_dir}/#{filename}"
file_to_upload = file_name
upload_progress = 0
opts = {
content_type: 'video/MP2T',
cache_control: 'max-age=31536000',
estimated_content_length: file.size,
}
part_size = compute_part_size(opts)
parts_number = (file.size.to_f / part_size).ceil.to_i
obj = s3.objects[file_to_upload]
begin
obj.multipart_upload(opts) do |upload|
until file.eof? do
break if (abort_upload = upload.aborted?)
upload.add_part(file.read(part_size))
upload_progress += 1.0/parts_number
# Yields the Float progress and the String filepath from the
# current file that's being uploaded
yield(upload_progress, upload) if block_given?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment