class BrickFTP::RESTfulAPI::UploadFile
Overview of uploading
@see developers.files.com/#overview-of-uploading Overview of uploading
### Uploading files using the REST API is done in 3 stages:
-
{developers.files.com/#starting-a-new-upload Start a new upload} by sending a request to REST API to indicate intent to upload a file.
-
{developers.files.com/#uploading-the-file-or-file-parts Upload the file} to the URL(s) provided by the REST API, possibly in parts via multiple uploads.
-
{developers.files.com/#completing-an-upload Complete the upload} by notifying the REST API that the file upload has completed.
Constants
- CHUNK_SIZE_RANGE
Public Instance Methods
At this point, you are to send a PUT request to the returned upload_uri with the file data, along with the headers and parameters provided to you from BrickFTP
.
The upload_uri link is signed by BrickFTP
and must be used within 15 minutes. You will receive an HTTP 200 response with no body upon successful upload.
Should you wish to upload the file in multiple parts (required if the file size exceeds 5 GB) you will need to request an additional upload URL for the next part.
@param [String] path Full path of the file or folder. Maximum of 550 characters. @param [IO] data @param [Integer, nil] chunk_size the chunk sizes are required to be between 5 MB and 5 GB.
This option is ignored if `data` is `StringIO`.
@return [BrickFTP::Types::File] File object
# File lib/brick_ftp/restful_api/upload_file.rb, line 37 def call(path, data, chunk_size: nil) chunk_size = adjust_chunk_size(data, chunk_size) validate_range_of_chunk_size!(chunk_size) upload = StartUpload.new(client).call(path) chunk_io = BrickFTP::Utils::ChunkIO.new(data, chunk_size: chunk_size) rest = data.size chunk_io.each do |chunk| rest -= client.upload_file(upload.http_method, upload.upload_uri, chunk) break if !chunk_size || rest <= 0 upload = ContinueUpload.new(client).call(path, ref: upload.ref, part: upload.part_number + 1) end CompleteUpload.new(client).call(path, ref: upload.ref) end
Private Instance Methods
To single uploading if chunk_size less than equals data size.
# File lib/brick_ftp/restful_api/upload_file.rb, line 57 def adjust_chunk_size(data, chunk_size) chunk_size && chunk_size >= data.size ? nil : chunk_size end
# File lib/brick_ftp/restful_api/upload_file.rb, line 61 def validate_range_of_chunk_size!(chunk_size) raise ArgumentError, 'chunk_size must be between 5MB and 5GB' if chunk_size && !CHUNK_SIZE_RANGE.cover?(chunk_size) end