Upload files
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
awaitkeyword waits for the upload to complete.If the
stateisstopped,cancelled, orerror, the method throws aPDSError.
// 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 |
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 |
loc_id | string | The ID of the upload destination. This can be a | |
loc_type | string | The type of the upload destination. Valid values: " | |
parent_file_key | string | (Not recommended. Use | |
drive_id | string | The ID of the destination drive. The | |
share_id | string | (Valid in hosting mode) The ID of the destination share. The | |
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 | |
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: | |
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 ( | |
chunk_size | number | The part size in bytes ( | |
start_time | number | The upload start time in milliseconds ( | |
end_time | number | The upload end time in | |
used_avg_speed | number | The average speed in bytes per second ( | |
used_time_len | number | The elapsed time in |
Specify either the
loc_id + loc_typepair or theshare_id || drive_idpair.Specify either the
parent_file_keyor theparent_file_id || parent_file_pathpair.
IUploadOptions (extends IUpConfig)
Parameter | Type | Required | Description |
onReady | (task: UploadTask) => void | A callback method that is invoked after the | |
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 The | |
onStateChange | (cp: IUpCheckpoint, state: string, error?: PDSError) => void | A callback method that listens for task state changes. Valid values for state: | |
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 |
IUpConfig
Parameter | Type | Required | Description |
parallel_upload | boolean | (Valid in standard mode) Specifies whether to upload parts concurrently. Default: | |
check_name_mode | string | Valid values:
| |
checking_crc | boolean | Specifies whether to perform a CRC check. Default: | |
chunk_con_auto | boolean | Specifies whether to automatically adjust the concurrency. | |
init_chunk_con | number | The custom concurrency. This parameter takes effect only when | |
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 |
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, |
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 |
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: |
error | PDSError | The upload error. This parameter has a value when the state is |
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 |
start | Starting upload. Triggered by calling the |
created | Triggered after a PDS temporary file is created. At this point, an |
computing_hash | Calculating the
|
running | Uploading. |
stopped | Paused. |
complete | Upload complete. The file integrity has not been verified. |
checking | Verifying file integrity ( |
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 |
Attributes
// Example:
console.log(task.speed)Attribute | Type | Description |
speed | number | The upload speed in bytes per second ( |
progress | number | The progress. The value ranges from 0 to 100. |
loaded | number | The size of the uploaded data in bytes ( |
file | IFile | The file information. |
left_time | number | The approximate remaining time in seconds ( |
start_time | number | The start time in milliseconds ( |
end_time | number | The end time in |
used_avg_speed | number | The calculated average upload speed in |
used_time_len | number | The calculated elapsed time in |