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

Torch Object Storage

Copy page
Download PDF
Upload Object
Resumable Upload (Browser.js SDK)
Copy page
Download PDF
Resumable Upload (Browser.js SDK)
When uploading files to TOS using the resumable upload method, you can configure parameters such as part size, upload concurrency, and event callback functions. During the upload process, if a network error or program crash results in a failed file upload, the system will resume uploading the incomplete portion from the last recorded breakpoint. During the upload process, you can cancel the object upload by invoking the Cancel method of the passed-in cancelToken.​
Notes​
  • To prevent exposure of your Volcano Engine account credentials (namely, AccessKey ID and AccessKey Secret) in browser environments, it is strongly recommended that you perform TOS-related operations using temporary access credentials. For detailed instructions, please refer to Using STS Temporary AK/SK+Token to Access Volcano Engine TOS.​
  • Endpoint is the domain name used to access TOS external services. For information on endpoints supported by TOS, please refer to Access Domain Name Endpoint.​
  • When uploading objects, object names must comply with specific conventions. For more information, see Object Naming Conventions.​
  • TOS is a distributed object storage product designed for large-scale data storage, with internal partitions storing object index data. To maximize throughput scalability when uploading and downloading objects and to reduce the likelihood of hotspot partitions, please avoid using lexicographically incremental object naming. For more information, see Performance Optimization.​
  • If an object with the same name already exists in the bucket, the new object will overwrite the existing one. If versioning is enabled for your bucket, the original object will be retained, and a new version number will be generated to identify the newly uploaded object.​
  • The SDK records the upload status information in the Checkpoint file, so the program must have write permissions for the Checkpoint file.​
  • When using resumable upload, the progress information of the file upload is recorded in the Checkpoint file. If a part of the upload fails during the process, when uploading again, it will continue from the point recorded in the Checkpoint file. After the upload is complete, the Checkpoint file will be deleted.​
  • If the local file is modified during the upload process, all segments will be re-uploaded.​
Sample Code​
Object upload​
The following code is used to upload files using the resumable upload method.​
​
<!DOCTYPE html>​
<html lang="en">​
<head>​
<meta charset="UTF-8" />​
<title>Document</title>​
</head>​
<body>​
<button id="upload">Upload</button>​
<input id="file" type="file" />​
<script src="https://tos-public.volccdn.com/obj/volc-tos-public/@volcengine/tos-sdk@latest/browser/tos.umd.production.min.js"></script>​
<script type="text/javascript">​
const client = new TOS({​
region: yourRegion,​
endpoint: yourEndpoint,​
accessKeyId: yourAccessKey,​
accessKeySecret: yourSecretKey,​
stsToken: yourSecurityToken,​
bucket: examplebucket,​
});​
​
const upload = document.getElementById('upload');​
upload.addEventListener('click', async () => {​
const data = document.getElementById('file').files[0];​
console.log('data', data);​
​
try {​
const partSize = 5 * 1024 * 1024;​
​
const result = await client.uploadFile({​
key: 'exampledir/putObjectobject.txt',​
file: data,​
partSize,​
taskNum: 5,​
progress: (p, cpt) => {​
abortCheckpoint = cpt;​
console.log(p * 100);​
},​
});​
console.log(result);​
} catch (e) {​
console.log(e);​
}​
});​
</script>​
</body>​
</html>​
​
Cancellation of the Resumable Transfer Mechanism​
The following code is used to cancel an active resumable upload task during runtime.​
​
<!DOCTYPE html>​
<html lang="en">​
<head>​
<meta charset="UTF-8" />​
<title>Document</title>​
</head>​
<body>​
<button id="upload">Upload</button>​
<input id="file" type="file" />​
<script src="https://tos-public.volccdn.com/obj/volc-tos-public/@volcengine/tos-sdk@latest/browser/tos.umd.production.min.js"></script>​
<script type="text/javascript">​
const client = new TOS({​
region: yourRegion,​
endpoint: yourEndpoint,​
accessKeyId: yourAccessKey,​
accessKeySecret: yourSecretKey,​
stsToken: yourSecurityToken,​
bucket: examplebucket,​
});​
​
const upload = document.getElementById('upload');​
upload.addEventListener('click', async () => {​
const data = document.getElementById('file').files[0];​
console.log('data', data);​
​
const partSize = 5 * 1024 * 1024;​
​
let cancelTokenSource = TOS.CancelToken.source();​
let tosCheckpoint;​
​
client.uploadFile({​
key: 'exampledir/canclePutObjectobject.txt',​
file: data,​
partSize,​
cancelToken: cancelTokenSource.token,​
progress: (percent, checkpoint) => {​
tosCheckpoint = checkpoint;​
console.log(percent, checkpoint);​
},​
});​
​
setTimeout(() => {​
cancelTokenSource.cancel('manual cancel');​
console.log('cancel request');​
}, 1000);​
​
setTimeout(() => {​
cancelTokenSource = TOS.CancelToken.source();​
client.uploadFile({​
key: 'exampledir/canclePutObjectobject.txt',​
file: data,​
partSize,​
checkpoint: tosCheckpoint,​
cancelToken: cancelTokenSource.token,​
progress: (percent, checkpoint) => {​
tosCheckpoint = checkpoint;​
console.log(percent, checkpoint);​
},​
});​
}, 5000);​
});​
</script>​
</body>​
</html>​
​
​
Last updated: 2025.09.22 18:55:46