PDSClient provides two methods to upload a file: uploadFile and createUploadTask. This topic describes the two methods.
uploadFile method
Uploads a file in a convenient way.
The
awaitkeyword can be used to wait until the file is uploaded.If the value of
stateisstopped,cancelled, orerror,PDSErroris thrown.
// client indicates 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("Uploaded");
}
</script>Request parameters
Parameter | Type | Required | Description |
file | IFile | string | Yes | The file to be uploaded. If you want to upload a file in a web browser, specify the HTML file that you want to upload. If you want to upload a file in the Node.js environment, set the value to the local file path or the local path information. |
uploadTo | IUpCheckpoint | Yes | The information about the Drive and Photo Service (PDS) directory to which the file is to be uploaded. You can also set the value to the last checkpoint information. |
options | IUploadOptions | The upload configurations, including callback methods. |
Fields of IFile
Field | Type | Required | Description |
path | string | Yes | The path. |
name | string | Yes | The name of the file. |
size | number | Yes | The size of the file. |
type | string | The |
Fields of IUpCheckpoint
Field | Type | Required | Description |
file | IFile | Yes | The file to be uploaded. If you want to upload a file in a web browser, specify the HTML file that you want to upload. If you want to upload a file in the Node.js environment, set the value to the local path information. |
path_type | string | Yes | The |
loc_id | string | The ID of the location to which the file is to be uploaded. It can be a | |
loc_type | string | The type of the location to which the file is to be uploaded. Valid values: | |
parent_file_key | string | The ID of the directory to which the file is to be uploaded. We recommend that you replace this parameter with the | |
drive_id | string | The drive ID of the directory to which the file is to be uploaded. You muse specify one of the | |
share_id | string | The share ID of the directory to which the file is to be uploaded. You must specify one of the | |
parent_file_id | string | The parent directory ID of the directory to which the file is to be uploaded. | |
new_name | string | The new name to be used for the uploaded file. This parameter may be affected by the | |
id | string | The ID of the upload task. | |
file_key | string | The ID of the file to be uploaded. We recommend that you replace this parameter with the file_id parameter. | |
update_id | string | The upload ID. This ID is generated after PDS creates a temporary file. | |
part_info_list | Array<IUpPartInfo> | The list of parts that are to be uploaded. | |
state | string | The state of the upload task. Valid values: | |
message | string | The error message. | |
progress | number | The upload progress. Valid values: 0 to 100. | |
loaded | number | The number of bytes that have been uploaded. Unit: | |
chunk_size | number | The size of a part. Unit: | |
start_time | number | The start time of the upload task. Unit: | |
end_time | number | The end time of the upload task. Unit: | |
used_avg_speed | number | The average speed at which the file is uploaded. Unit: | |
used_time_len | number | The period of time that has elapsed since the start of the upload task. Unit: |
You must specify
loc_id + loc_typeorshare_id || drive_id.You must specify
parent_file_keyorparent_file_id.
Fields of IUploadOptions (extends IUpConfig)
Field | Type | Required | Description |
onReady | (task: UploadTask) => void | The callback method for the | |
onProgress | (state: string, progress:number) => void | The callback method that is used to listen to the progress. You can use this method to listen to the progress of calculating the SHA-1 hash value, the upload progress, or the progress of calculating the CRC-64 value. Valid values of Valid values of | |
onStateChange | (cp: IUpCheckpoint, state: string, error?: PDSError) => void | The callback method that is used to listen to task state changes. Value values of state: | |
onPartComplete | (cp:IUpCheckpoint, part: IPartInfo) => void | The callback method that is used to listen to the completion of part upload. | |
... Other IUpConfig fields |
Fields of IUpPartInfo
Field | Type | Required | Description |
part_number | number | Yes | The sequence number of the part. The number starts from 1. |
part_size | number | Yes | The size of the part. |
etag | string | The |
Fields of IUpConfig
Field | Type | Required | Description |
parallel_upload | boolean | Specifies whether to concurrently upload parts. Default value: | |
check_name_mode | string | The policy that is used when a new file has the same name as an existing file. Valid values:
| |
checking_crc | boolean | Specifies whether to perform CRC verification. Default value: | |
chunk_con_auto | boolean | Specifies whether to automatically adjust the number of concurrent parts that are being uploaded. | |
init_chunk_con | number | The custom number of concurrent parts to be uploaded. This field takes effect only if | |
max_chunk_size | number | The size of the part. | |
ignore_rapid | boolean | Specifies whether to forcibly disable instant file transfer. Default value: |
Response parameters
Parameter | Type | Required | Description |
cp | IUpCheckpoint | Yes | The checkpoint information of the upload task. Valid states include |
createUploadTask 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", // You can replace this parameter with the drive_id parameter.
// loc_type: "drive",
// parent_file_key: "root", // You can replace this parameter with the parent_file_id parameter.
drive_id: "1",
parent_file_id: "root"
};
// Create an upload task.
const task = client.createUploadTask(cp);
// Listen to the upload progress.
task.on('progress', (state, progress)=>{
console.log(state, progress);
});
// Start the upload task.
task.start();
// ...
}
</script>Request parameters
Parameter | Type | Required | Description |
checkpoint | IUpCheckpoint | Yes | The upload attributes. |
configs | IUpConfig | The upload configurations. |
Response parameters
Parameter | Type | Required | Description |
task | UploadTask | Yes | The UploadTask instance. |
UploadTask class
Creates a task to upload a file.
Supported methods
.wait()
After you call this method, the state of the task changes to waiting.
.start()
You can call this method to start an upload task. After you start the upload task, the system starts to calculate the SHA-1 hash value. If the file is instantly uploaded, a message is returned, which indicates a successful instant file transfer. If the file cannot be instantly uploaded, the system starts to upload the file by uploading parts of the file. The config.parallel_upload parameter specifies whether to concurrently upload parts.
task.start();.stop()
You can call this method to suspend an upload task. The state of the task changes to stopped. In this case, the client instance stores the checkpoint information for the next resumable upload.
task.stop();.cancel()
You can call this method to cancel an upload task. The state of the task changes to cancelled.
task.cancel().getCheckpoint()
You can call this method to query the checkpoint information of the upload task. Alternatively, you can query the checkpoint information of the task by using event callbacks.
IUpCheckpoint cp = task.getCheckpoint().on(eventname, callback)
You can call this method to configure listeners.
// Example: progress event
task.on('progress', (state, progress)=>{
console.log(state, progress)
})Parameters
Parameter | Type | Required | Description |
eventname | ICheckpoint | Yes | The name of the event. Valid values: |
callback | Function | Yes | The callback function. |
Supported events
Callback method for a progress event
You can listen to events of the following types: progress, statechange, and partialcomplete.
task.on('progress', (state, progress)=>{
//
})Parameters
Parameter | Type | Required | Description |
state | string | Yes | The state of the task. Valid values: |
progress | number | Yes | The upload progress. Valid values: 0 to 100. |
Callback method for a statechange event
task.on('statechange', (checkpoint, state, error)=>{
//
})Parameter | Type | Required | Description |
checkpoint | IUpCheckpoint | Yes | The checkpoint information of the upload task. |
state | string | Yes | The state of the upload task. Valid values: |
error | PDSError | The upload error. A value is returned if the state of the task is |
Callback method for a partialcomplete event
task.on('partialcomplete', (checkpoint, partInfo)=>{
//
})Parameter | Type | Required | Description |
checkpoint | IUpCheckpoint | Yes | The checkpoint information of the upload task. |
partInfo | IUpPartInfo | Yes | The information about the parts that are uploaded. |
States
An upload task can be in one of the following states.
State | Description |
waiting | The upload task is waiting to start. By default, an upload task is in this state after it is created. You can call the |
start | The upload task starts. You can call the |
created | The upload task is created. This state is triggered after PDS creates a temporary file. In this case, an |
computing_hash | The
|
running | The upload task is running. |
stopped | The upload task is suspended. |
complete | The upload task is complete but the file is not verified. |
checking | The |
success | The upload task is successful. |
rapid_success | The file is instantly uploaded. |
error | The upload task fails. This state is triggered if an API operation fails to be called, a network connection fails, or a local I/O operation fails during the upload process. |
cancelled | The upload task is canceled. You can call the |
Parameters that may be used
// Example:
console.log(task.speed)Parameter | Type | Description |
speed | number | The upload speed. Unit: |
progress | number | The upload progress. Valid values: 0 to 100. |
loaded | number | The size of the parts that have been uploaded. Unit: |
file | IFile | The information about the file. |
left_time | number | The approximate amount of time that is required for uploading the remaining parts. Unit: |
start_time | number | The start time of the upload task. Unit: |
end_time | number | The end time of the upload task. Unit: |
used_avg_speed | number | The average upload speed. Unit: |
used_time_len | number | The duration of the upload. Unit: |