Skip to content

Instantly share code, notes, and snippets.

@sjha4
Last active July 8, 2019 19:30
Show Gist options
  • Select an option

  • Save sjha4/6838ca83ad1145ba46d0ed602a9ec515 to your computer and use it in GitHub Desktop.

Select an option

Save sjha4/6838ca83ad1145ba46d0ed602a9ec515 to your computer and use it in GitHub Desktop.
Ruby gem - Chunked uploads test steps
# Install ruby, pulpcore-client gem and setup a temp file
sudo yum install ruby
sudo yum install ruby-devel
sudo yum install gcc
gem install pulpcore_client -v 3.0.0rc2.dev.1560186355 --pre
curl -o katello.gemspec https://github.com/Katello/katello/blob/master/katello.gemspec
# Open irb
irb
#Run below script:
require 'pulpcore_client'
PulpcoreClient.configure do |config|
config.host= "http://localhost:24817"
config.username= 'admin'
config.password= 'password'
config.debugging=true
end
api_instance = PulpcoreClient::UploadsApi.new
content_range = "bytes 0-2373/2373" # String | The Content-Range header specifies the location of the file chunk within the file.
file = File.new('katello.gemspec') # File | A chunk of a file to upload.
begin
#Start Upload
result = api_instance.uploads_create(content_range, file)
p result
rescue PulpcoreClient::ApiError => e
puts "Exception when calling UploadsApi->uploads_create: #{e}"
end
@sjha4
Copy link
Copy Markdown
Author

sjha4 commented Jun 10, 2019

require 'pulpcore_client'
PulpcoreClient.configure do |config|
config.host= "http://localhost:24817"
config.username= 'admin'
config.password= 'password'
config.debugging=true
end

api_instance = PulpcoreClient::UploadsApi.new
content_range = "bytes 0-84239/84239" # String | The Content-Range header specifies the location of the file chunk within the file.

file = File.new('katello.gemspec') # File | A chunk of a file to upload.

begin
#Start Upload
result = api_instance.uploads_create_and_check(file, Digest::MD5.hexdigest(File.read(file)))
p result
rescue PulpcoreClient::ApiError => e
puts "Exception when calling UploadsApi->uploads_create: #{e}"
end

@sjha4
Copy link
Copy Markdown
Author

sjha4 commented Jul 8, 2019

require 'pulpcore_client'
PulpcoreClient.configure do |config|
config.host= "http://localhost:24817"
config.username= 'admin'
config.password= 'password'
config.debugging=true
end

uploads_api = PulpcoreClient::UploadsApi.new
upload_chunk_size = 1000

file = File.new('katello.gemspec') # File | A chunk of a file to upload.
total_size = File.size(file)
offset = 0

upload_data = PulpcoreClient::Upload.new({size: total_size})
response = uploads_api.create(upload_data)
upload_href = response._href

def content_range(start, finish, total)
finish = finish > total ? total : finish
"bytes #{start}-#{finish}/#{total}"
end

def tmp_file(file, upload_chunk_size)
tmp_path = File.path(file) + "_tmp_upload"
File.delete(tmp_path) if File.exist?(tmp_path)
File.new(tmp_path, 'wb+', 0600)
File.open(tmp_path, "wb+") do |temp_file|
temp_file.write(file.read(upload_chunk_size))
end
tmp_path
end

def clean_temps(file)
tmp_path = File.path(file) + "_tmp_upload"
File.delete(tmp_path) if File.exist?(tmp_path)
end

file_full = file

File.open(file_full, "rb") do |file|
while (chunk = tmp_file(file, upload_chunk_size))
response = uploads_api.update(upload_href, content_range(offset, offset + upload_chunk_size, total_size), chunk)
offset += upload_chunk_size
break if (offset + upload_chunk_size >= total_size)
end
end

clean_temps file_full

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment