Class: Uploadcare::Client::MultipartUpload::ChunksClient

Inherits:
ApiStruct::Client
  • Object
show all
Defined in:
lib/uploadcare/client/multipart_upload/chunks_client.rb

Overview

This class splits file into chunks of set chunk_size and uploads them into cloud storage. Used for multipart uploads

Constant Summary collapse

CHUNK_SIZE =
5_242_880

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.upload_chunks(object, links) ⇒ Object

In multiple threads, split file into chunks and upload those chunks into respective Amazon links

Parameters:

  • object (File)
  • links (Array)

    of strings; by default list of Amazon storage urls



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uploadcare/client/multipart_upload/chunks_client.rb', line 20

def self.upload_chunks(object, links)
  Parallel.each(0...links.count, in_threads: Uploadcare.config.upload_threads) do |link_id|
    offset = link_id * CHUNK_SIZE
    chunk = File.read(object, CHUNK_SIZE, offset)
    new.upload_chunk(chunk, links[link_id])
    next unless block_given?

    yield(
      chunk_size: CHUNK_SIZE,
      object: object,
      offset: offset,
      link_id: link_id,
      links: links,
      links_count: links.count
    )
  end
end

Instance Method Details

#api_rootObject



38
39
40
# File 'lib/uploadcare/client/multipart_upload/chunks_client.rb', line 38

def api_root
  ''
end

#headersObject



42
43
44
# File 'lib/uploadcare/client/multipart_upload/chunks_client.rb', line 42

def headers
  {}
end

#upload_chunk(chunk, link) ⇒ Object



46
47
48
# File 'lib/uploadcare/client/multipart_upload/chunks_client.rb', line 46

def upload_chunk(chunk, link)
  put(path: link, body: chunk, headers: { 'Content-Type': 'application/octet-stream' })
end