Help Center/ Object Storage Service/ SDK Reference/ BrowserJS/ Object APIs/ Basic Operations/ Object Download/ Performing a Batch Download
Updated on 2026-08-14 GMT+08:00
Performing a Batch Download
This example downloads objects in batches.
// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"
// Create an ObsClient instance.
var obsClient = new ObsClient({
access_key_id: AK,
secret_access_key: SK,
server: server
});
// Bucket name
var bucket = 'ztw-test11';
// Prefix of the objects to be downloaded. If the prefix is left blank, all objects will be downloaded.
var prefix = 'prefix/';
// Trigger download via browser.
function downloadBlob(blob, filename) {
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
async function getObjects() {
var marker = '';
var totalCount = 0;
console.log('Starting listing and downloading objects...');
while (true) {
var listParams = { Bucket: bucket };
if (prefix) {
listParams.Prefix = prefix;
}
if (marker) {
listParams.Marker = marker;
}
var listResult = await obsClient.listObjects(listParams);
if (listResult.CommonMsg.Status <= 300) {
if (listResult.InterfaceResult.Contents) {
var batchCount = listResult.InterfaceResult.Contents.length;
console.log ('Obtained ' + batchCount + ' objects in this batch. Starting download...');
for (var i = 0; i < listResult.InterfaceResult.Contents.length; i++) {
var key = listResult.InterfaceResult.Contents[i].Key;
var size = listResult.InterfaceResult.Contents[i].Size;
totalCount++;
try {
var params = {
Bucket: bucket,
Key: key,
SaveByType: 'arraybuffer'
};
var result = await obsClient.getObject(params);
if (result.CommonMsg.Status <= 300) {
var blob = new Blob([result.InterfaceResult.Content]);
downloadBlob(blob, key.split('/').pop());
console.log('[' + totalCount + '] succeeded: ' + key + ' (' + size + ' bytes)');
} else {
console.log('[' + totalCount + '] failed: ' + key + ' - ' + result.CommonMsg.Message);
}
} catch (error) {
console.log('[' + totalCount + '] exception occurred: ' + key + ' - ' + error);
}
}
}
if (listResult.InterfaceResult.IsTruncated == 'true') {
marker = listResult.InterfaceResult.NextMarker;
} else {
break;
}
} else {
console.log('Failed to list objects: ', listResult.CommonMsg.Message);
return;
}
}
console.log('Batch download completed. A total of ' + totalCount + ' objects are processed.');
}
getObjects()
- Set the SaveByType parameter to arraybuffer to use binary download.
- In the returned result of a binary download, InterfaceResult.Content is an instance of ArrayBuffer.
Parent topic: Object Download
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot