You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Download Object
Resumable Download (Harmony SDK)
Copy page
Download PDF
Resumable Download (Harmony SDK)
When downloading large files, you can use the downloadFile resumable download interface. Resumable download utilizes chunked downloading to write content into a temporary file. In cases where some chunks are downloaded successfully while others fail, information regarding the success and failure of each chunk can be recorded and stored in a Checkpoint file. When re-downloading the same object, only the failed parts are downloaded. When all shards have completed successfully, rename the temporary file as the official file and delete the Checkpoint file.​
Precautions​
  • Before downloading an object, you must have the tos:GetObject permission. For detailed instructions, please refer to the Permission Configuration Guide.​
  • For buckets with versioning enabled, you must have the tos:GetObjectVersion permission to download a specified version of an object. For detailed instructions, please refer to the Permission Configuration Guide.​
  • If an application downloads the same object in large quantities simultaneously, your access speed will be limited by TOS bandwidth and regional restrictions. It is recommended that you use CDN products, which can enhance performance while also reducing your costs. For detailed information on accessing TOS via CDN, please refer to Using CDN to Accelerate Access to TOS Resources.​
  • The SDK records the upload status information in the Checkpoint file; therefore the program must have write permissions for the Checkpoint file.​
  • When using resumable download, the progress information of the file is recorded in the Checkpoint file. If a segment fails to download during the process, the next download attempt will resume from the point recorded in the Checkpoint file, thus enabling resumable download. After the download is complete, the Checkpoint file will be deleted.​
  • If, during the download process, the file's ETag changes, a Part is lost, or a Part is modified, the file will be re-downloaded.​
Sample Code​
Resumable Download​
The following code is used to download files using the resumable download method.​
​
import { TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';​
​
​
const client = new TosClient({​
accessKeyId: "Provide your ak",​
accessKeySecret: "Provide your sk",​
securityToken: 'Provide your securityToken',​
region: "Provide your region", ​
endpoint: "Provide your endpoint", ​
});​
​
function handleError(error: Error) {​
if (error instanceof TosClientError) {​
console.log('Client Err Msg:', error.message);​
console.log('Client Err Stack:', error.stack);​
} else if (error instanceof TosServerError) {​
console.log('Request ID:', error.RequestID);​
console.log('Response Status Code:', error.StatusCode);​
console.log('Response Header:', error.Headers);​
console.log('Response Err Code:', error.Code);​
console.log('Response Err Msg:', error.Message);​
} else {​
console.log('unexpected exception, message: ', error);​
}​
}​
​
async function main() {​
try {​
const bucketName = 'harmony-sdk-test-bucket';​
const objectName = 'example_dir/example.txt';​
const filePath = './example_dir/example.txt';​
const checkpointFile = './example_dir/example.checkpoint'​
​
client.downloadFile({​
Bucket: bucketName,​
Key: objectName,​
​
FilePath: filePath,​
​
CheckpointFile: checkpointFile,​
​
EnableCheckpoint: true,​
});​
} catch (error) {​
handleError(error);​
}​
}​
​
main();​
​
Progress Bar Configuration​
During resumable downloads, you can receive download progress via the DataTransferStatusChange parameter. The sample code is as follows.​
​
import { DataTransferType, TosClient, TosClientError, TosServerError } from '@volcengine/tos_harmonyos_sdk';​
​
​
const client = new TosClient({​
accessKeyId: "Provide your ak",​
accessKeySecret: "Provide your sk",​
securityToken: 'Provide your securityToken',​
region: "Provide your region", ​
endpoint: "Provide your endpoint",​
});​
​
function handleError(error: Error) {​
if (error instanceof TosClientError) {​
console.log('Client Err Msg:', error.message);​
console.log('Client Err Stack:', error.stack);​
} else if (error instanceof TosServerError) {​
console.log('Request ID:', error.RequestID);​
console.log('Response Status Code:', error.StatusCode);​
console.log('Response Header:', error.Headers);​
console.log('Response Err Code:', error.Code);​
console.log('Response Err Msg:', error.Message);​
} else {​
console.log('unexpected exception, message: ', error);​
}​
}​
​
async function main() {​
try {​
const bucketName = 'harmony-sdk-test-bucket';​
const objectName = 'example_dir/example.txt';​
const filePath = './example_dir/multipartExample';​
const checkpointFile = './example_dir/example.checkpoint';​
​
await client.downloadFile({​
Bucket: bucketName,​
Key: objectName,​
FilePath: filePath,​
​
CheckpointFile: checkpointFile,​
​
EnableCheckpoint: true,​
DataTransferListener:{​
DataTransferStatusChange(event){​
if (event.Type === DataTransferType.Started) {​
console.log('Data Transfer Started');​
} else if (event.Type === DataTransferType.RW) {​
const percent = ((event.ConsumedBytes / event.TotalBytes) * 100).toFixed(2);​
console.log(`Once Read:${event.RWOnceBytes},ConsumerBytes/TotalBytes: ${event.ConsumedBytes}/${event.TotalBytes},${percent}%`);​
} else if (event.Type === DataTransferType.Succeed) {​
const percent = ((event.ConsumedBytes / event.TotalBytes) * 100).toFixed(2);​
console.log(`Data Transfer Succeed, ConsumerBytes/TotalBytes:${event.ConsumedBytes}/${event.TotalBytes},${percent}%`);​
} else if (event.Type === DataTransferType.Failed) {​
console.log('Data Transfer Failed');​
}​
}​
}​
});​
} catch (error) {​
handleError(error);​
}​
}​
​
main();​
​
Cancellation Mechanism​
The following code is used to cancel an ongoing resumable upload task during runtime.​
​
import { TosClient, TosClientError, TosServerError, CancelHook } from '@volcengine/tos_harmonyos_sdk';​
​
​
const client = new TosClient({​
accessKeyId: "Provide your ak",​
accessKeySecret: "Provide your sk",​
securityToken: 'Provide your securityToken',​
region: "Provide your region", ​
endpoint: "Provide your endpoint", ​
});​
​
function handleError(error: Error) {​
if (error instanceof TosClientError) {​
console.log('Client Err Msg:', error.message);​
console.log('Client Err Stack:', error.stack);​
} else if (error instanceof TosServerError) {​
console.log('Request ID:', error.RequestID);​
console.log('Response Status Code:', error.StatusCode);​
console.log('Response Header:', error.Headers);​
console.log('Response Err Code:', error.Code);​
console.log('Response Err Msg:', error.Message);​
} else {​
console.log('unexpected exception, message: ', error);​
}​
}​
​
async function main() {​
const cancelHook = new CancelHook();​
try {​
const bucketName = 'harmony-sdk-test-bucket';​
const objectName = 'example_dir/example.txt';​
const filePath = './example_dir/multipartExample';​
const checkpointFile = './example_dir/example.checkpoint'​
​
client.downloadFile({​
Bucket: bucketName,​
Key: objectName,​
​
FilePath: filePath,​
CancelHook: cancelHook,​
​
CheckpointFile: checkpointFile,​
​
EnableCheckpoint: true,​
});​
​
​
setTimeout(() => {​
cancelHook.Cancel();​
}, 1000)​
​
} catch (error) {​
handleError(error);​
}​
}​
​
main();​
​
​
Last updated: 2025.09.26 19:05:17