Upload files

Updated at:

PDSClient provides two methods to upload files: uploadFile and createUploadTask. This topic describes each method.

uploadFile method

This method provides a convenient way to upload a file.

  • The await keyword waits for the upload to complete.

  • If the state is stopped, cancelled, or error, the method throws a PDSError.

// client is a PDSClient instance
const cp = await client.uploadFile(from, uploadTo, options)

Example

<input type="file" id="f1"/>

<script> 
  document.getElementById('f1').onchange = async function(e){
    let file = e.target.files[0];
    let uploadTo = {drive_id: '1', parent_file_id: 'root'};
    await client.uploadFile(file, uploadTo);
    console.log("Upload successful");
  }
</script>

Parameters

Parameter

Type

Required

Description

file

IFile | string

Yes

The file to upload.

In a browser, this is an HTML File object.

In Node.js, this is the path to a local file or local file information.

uploadTo

IUpCheckpoint

Yes

The PDS destination directory information or the breakpoint information from the last upload.

options

IUploadOptions

Upload configurations, including callback methods.

IFile

Parameter

Type

Required

Description

path

string

Yes

The path.

name

string

Yes

The file name.

size

number

Yes

The file size.

type

string

The Content-Type of the file.

IUpCheckpoint

Parameter

Type

Required

Description

file

IFile

Yes

The file object to upload.

In a browser, this is an HTML File object.

In Node.js, this is local file information.

path_type

string

Yes

The path_type attribute of the domain. Valid values: StandardMode and HostingMode.

loc_id

string

The ID of the upload destination. This can be a drive_id or a share_id.

loc_type

string

The type of the upload destination. Valid values: "drive" or "share".

parent_file_key

string

(Not recommended. Use parent_file_id or parent_file_path instead.) The ID of the destination directory. In standard mode, this is the directory ID. In hosting mode, this is the directory path.

drive_id

string

The ID of the destination drive. The drive_id and share_id parameters are mutually exclusive. Specify only one.

share_id

string

(Valid in hosting mode) The ID of the destination share. The drive_id and share_id parameters are mutually exclusive. Specify only one.

parent_file_id

string

The ID of the parent directory for the destination (valid in standard mode).

parent_file_path

string

The path of the parent directory for the destination (valid in hosting mode).

new_name

string

Rename (Standard mode only). This is controlled by the check_name_mode parameter.

id

string

The upload task ID.

file_key

string

The file ID (in standard mode) or path (in hosting mode).

update_id

string

The upload ID. This ID is generated after the PDS service creates a temporary file.

part_info_list

Array<IUpPartInfo>

Information about the uploaded parts.

state

string

The state of the upload task. Valid values: waiting, start, computing_hash, created, running, stopped, complete, checking, success, rapid_success, error, cancelled.

message

string

The error message.

progress

number

The upload progress. The value ranges from 0 to 100.

loaded

number

The number of bytes that have been uploaded (Bytes).

chunk_size

number

The part size in bytes (Bytes).

start_time

number

The upload start time in milliseconds (ms).

end_time

number

The upload end time in ms.

used_avg_speed

number

The average speed in bytes per second (Bytes/s).

used_time_len

number

The elapsed time in ms.

  • Specify either the loc_id + loc_type pair or the share_id || drive_id pair.

  • Specify either the parent_file_key or the parent_file_id || parent_file_path pair.

IUploadOptions (extends IUpConfig)

Parameter

Type

Required

Description

onReady

(task: UploadTask) => void

A callback method that is invoked after the UploadTask is created.

onProgress

(state: string, progress:number) => void

A callback method that listens for progress changes. You can monitor the progress of rapid upload calculation, file upload, and CRC-64 calculation.

The state can be computing_hash (calculating for rapid upload), running (uploading), or checking (calculating CRC-64).

The progress value ranges from 0 to 100.

onStateChange

(cp: IUpCheckpoint, state: string, error?: PDSError) => void

A callback method that listens for task state changes.

Valid values for state: waiting, start, computing_hash, created, running, stopped, complete, checking, success, rapid_success, error, cancelled

onPartComplete

(cp:IUpCheckpoint, part: IPartInfo) => void

A callback method that listens for the completion of a part upload.

... Other IUpConfig fields

IUpPartInfo

Parameter

Type

Required

Description

part_number

number

Yes

The part number. The numbering starts from 1.

part_size

number

Yes

The part size.

etag

string

The ETag after a part is successfully uploaded.

IUpConfig

Parameter

Type

Required

Description

parallel_upload

boolean

(Valid in standard mode) Specifies whether to upload parts concurrently. Default: false.

check_name_mode

string

Valid values:

  • overwrite: Overwrites the existing file.

  • auto_rename: Appends a random string to the original name.

  • refuse: Does not create the file.

  • ignore: Creates a file with a duplicate name.

checking_crc

boolean

Specifies whether to perform a CRC check. Default: true.

chunk_con_auto

boolean

Specifies whether to automatically adjust the concurrency.

init_chunk_con

number

The custom concurrency. This parameter takes effect only when chunk_con_auto is false.

max_chunk_size

number

The part size.

ignore_rapid

boolean

Forces the system to not use rapid upload. (Valid in standard mode. Can be used for testing.)

Returns

Parameter

Type

Required

Description

cp

IUpCheckpoint

Yes

The breakpoint information for the task. The state of the breakpoint can be success, rapid_success, error, stopped, or cancelled.

The createUploadTask method

This method creates an upload task.

const task = client.createUploadTask(checkpoint, configs)

Example

<input type="file" id="f1"/>

<script> 
  document.getElementById('f1').onchange = async function(e){
    let file = e.target.files[0];
    let cp = {
      
      // from
      file,
      
      // to
      path_type: "StandardMode",
      // loc_id: "1",    // Can be replaced with drive_id
      // loc_type: "drive",
      // parent_file_key: "root", // Can be replaced with parent_file_id
      
      drive_id: "1", 
      parent_file_id: "root"
    };
  
    // Create an upload task
    const task = client.createUploadTask(cp);
    
    // Listen for progress
    task.on('progress', (state, progress)=>{
       console.log(state, progress);
    });
  
    // Start the upload
    task.start();
   
    // ...
  }
</script>

Parameters

Parameter

Type

Required

Description

checkpoint

IUpCheckpoint

Yes

Upload parameters.

configs

IUpConfig

Upload configurations.

Returns an UploadTask instance.

Parameter

Type

Required

Description

task

UploadTask

Yes

The upload task instance.

UploadTask class

Represents a file upload task.

Supported methods

.wait()

Puts the task in a waiting state. The state changes to waiting.

.start()

Starts the task. The behavior depends on the domain mode. In standard mode, the system first calculates information for a rapid upload. If a rapid upload is possible, the task completes immediately. Otherwise, a multipart upload begins. The config.parallel_upload parameter determines whether parts are uploaded concurrently. In hosting mode, a concurrent multipart upload starts directly.

task.start();

.stop()

Pauses the task. The state changes to stopped. You can store the checkpoint information to resume the upload later.

task.stop();

.cancel()

Cancels the task. The state changes to cancelled.

task.cancel()

.getCheckpoint()

Retrieves the checkpoint information. You can also obtain checkpoint information through an event callback.

var cp = task.getCheckpoint()

.on(eventname, callback)

Registers an event listener.

// Example: progress event
task.on('progress', (state, progress)=>{
   console.log(state, progress)
})

Parameters

Parameter

Type

Required

Description

eventname

ICheckpoint

Yes

The event name. Currently, progress, statechange, and partialcomplete are supported.

callback

Function

Yes

Callback function

Supported events

progress event callback

Supported events: progress, statechange, and partialcomplete.

task.on('progress', (state, progress)=>{
   //
})

Parameters

Parameter

Type

Required

Description

state

string

Yes

The task state.

Possible values for state are computing_hash (calculating for rapid upload), running (uploading), and checking (calculating CRC-64).

progress

number

Yes

The progress. The value ranges from 0 to 100.

statechange event

task.on('statechange', (checkpoint, state, error)=>{
   //
})

Parameter

Type

Required

Description

checkpoint

IUpCheckpoint

Yes

The upload breakpoint information.

state

string

Yes

The state of the upload task. Valid values: waiting, start, computing_hash, created, running, stopped, complete, checking, success, rapid_success, error, cancelled

error

PDSError

The upload error. This parameter has a value when the state is error, stopped, or cancelled.

partialcomplete event

task.on('partialcomplete', (checkpoint, partInfo)=>{
   //
})

Parameter

Type

Required

Description

checkpoint

IUpCheckpoint

Yes

The upload breakpoint information.

partInfo

IUpPartInfo

Yes

Information about the part that was just completed.

State descriptions

An upload task can have one of the following 12 states:

State

Description

waiting

Waiting. This is the default state after a task is created. It is triggered by calling the wait() method.

start

Starting upload. Triggered by calling the start() method.

created

Triggered after a PDS temporary file is created. At this point, an upload_id is generated.

computing_hash

Calculating the sha1 hash for rapid upload. If the rapid upload is successful, the state changes directly to

rapid_success.

running

Uploading.

stopped

Paused.

complete

Upload complete. The file integrity has not been verified.

checking

Verifying file integrity (crc64).

success

Upload successful.

rapid_success

Rapid upload successful.

error

Upload task failed. This state is triggered if an API call fails during the upload (including network errors) or if a local I/O operation fails.

cancelled

Upload task canceled. This state is triggered only by calling the .cancel() method.

Attributes

// Example:
console.log(task.speed)

Attribute

Type

Description

speed

number

The upload speed in bytes per second (Byte/s). This is valid when the state is `running`.

progress

number

The progress. The value ranges from 0 to 100.

loaded

number

The size of the uploaded data in bytes (Byte).

file

IFile

The file information.

left_time

number

The approximate remaining time in seconds (s).

start_time

number

The start time in milliseconds (ms).

end_time

number

The end time in ms.

used_avg_speed

number

The calculated average upload speed in Byte/s.

used_time_len

number

The calculated elapsed time in ms.