Get Object
Updated at:2025-11-03
Simple object get
You can load the object into memory using the provided code.
Basic workflow
- Create an instance of the BOSClient class.
- Running the BosClient getObject method returns a GetObjectResult instance.
- Access the data attribute of the GetObjectResult instance to retrieve the data.
Example code
Plain Text
1let getObjectResult: GetObjectResult;
2try {
3 getObjectResult = await bosClient.getObject(bucketName, objectName);
4 logger.info(`get object success, etag:${JSON.stringify(getObjectResult.etag)}`);
5 // Get the actual data
6 let data = getObjectResult.data as ArrayBuffer;
7} catch (bosResponse) {
8 logger.error(`errCode: ${bosResponse.error.code}`)
9 logger.error(`requestId: ${bosResponse.error.requestId}`)
10 logger.error(`errMessage: ${bosResponse.error.message}`)
11 logger.error(`statusCode: ${bosResponse.statusCode}`)
12}
Note:
- GetObjectResult stores the object’s data information (
data) and metadata information (userMeta,contentType, etc.)- This operation is to download the entire object. Due to the limitations of the underlying API, the total length of the object must be less than 5 MB
Complete code
Plain Text
1import { logger, Credential, BosClient, ClientOptions} from "bos"
2import { GetObjectResult } from "bos/src/main/ets/bos/api/DataType"
3
4 let credential = new Credential(AccessKeyID, SecretAccessKey, Token); //Temporary AK/SK and Token returned by STS
5let clientOptions = new ClientOptions();
6 clientOptions.endpoint = "bj.bcebos.com"; //Pass in the domain name of the region where the bucket is located
7 let bosClient = new BosClient(credential, clientOptions); // Create BosClient
8let bucketName = "test-harmony-bucket";
9let objectName = "test-object";
10
11let getObjectResult: GetObjectResult;
12try {
13 getObjectResult = await bosClient.getObject(bucketName, objectName);
14 logger.info(`get object success, etag:${JSON.stringify(getObjectResult.etag)}`);
15 // Get the actual data processing
16 let data = getObjectResult.data as ArrayBuffer;
17} catch (bosResponse) {
18 logger.error(`errCode: ${bosResponse.error.code}`)
19 logger.error(`requestId: ${bosResponse.error.requestId}`)
20 logger.error(`errMessage: ${bosResponse.error.message}`)
21 logger.error(`statusCode: ${bosResponse.statusCode}`)
22}
Download part of the content of an object
Basic workflow
- Create an instance of the BOSClient class.
- Execute the BosClient getObject method and specify the ranges parameter
- Access the data attribute of the GetObjectResult instance to retrieve the data.
Example code
Plain Text
1//Return data from byte 0 to byte 100, including byte 100
2let ranges = [0, 100];
3try {
4 getObjectResult = await bosClient.getObject(bucketName, objectName, ranges);
5 let data = getObjectResult.data as ArrayBuffer
6 logger.info(`get object success, ranges : 0 - 100 , return length:${JSON.stringify(data.byteLength)} `);
7} catch (bosResponse) {
8 logger.error(`errCode: ${bosResponse.error.code}`)
9 logger.error(`requestId: ${bosResponse.error.requestId}`)
10 logger.error(`errMessage: ${bosResponse.error.message}`)
11 logger.error(`statusCode: ${bosResponse.statusCode}`)
12}
Note: Users can use this function to implement segmented download and resumable download of files. When the total size of the object exceeds 5 MB, ranges must be specified to download in multiple times to succeed.
Get the metadata information of an object
Basic workflow
- Create an instance of the BOSClient class.
- Run the BosClient.getObjectMeta method to obtain an ObjectMeta instance.
- Access methods such as contentLength, userMeta, and storageClass in the ObjectMeta instance to get different metadata information
Example code
Plain Text
1let objectMeta: ObjectMeta;
2try {
3 objectMeta = await bosClient.getObjectMeta(bucketName, objectName);
4 logger.info(`get object meta success, info : ${JSON.stringify(objectMeta)}`);
5} catch (bosResponse) {
6 logger.error(`errCode: ${bosResponse.error.code}`)
7 logger.error(`requestId: ${bosResponse.error.requestId}`)
8 logger.error(`errMessage: ${bosResponse.error.message}`)
9 logger.error(`statusCode: ${bosResponse.statusCode}`)
10}
Get the file download link
The following code allows you to get the URL of a specific object, which is commonly used for temporarily sharing the object's URL with others.
Basic workflow
- Create an instance of the BOSClient class.
- Run the BosClient generatePresignedUrl method.
- The method returns the URL of the object.
Example code
Plain Text
1let expire = 1800;
2try {
3 let url = await bosClient.generatePresignedUrl(bucketName, objectName, expire);
4 logger.info(`url: ${url}`);
5} catch (bosResponse) {
6 logger.error(`errCode: ${bosResponse.error.code}`)
7 logger.error(`requestId: ${bosResponse.error.requestId}`)
8 logger.error(`errMessage: ${bosResponse.error.message}`)
9 logger.error(`statusCode: ${bosResponse.statusCode}`)
10}
Note: Expire is the specified URL validity period, calculated from the current time. It is an optional parameter, and the system default value is 1,800 seconds if not configured.
Download large files to a specified path
Basic workflow
- Create an instance of the BOSClient class.
- Run the BosClient downloadSuperFile method.
Complete code
Plain Text
1import fs from '@ohos.file.fs';
2import { logger, Credential, BosClient, ClientOptions } from "bos"
3
4 let credential = new Credential(AccessKeyID, SecretAccessKey, Token); //Temporary AK/SK and Token returned by STS
5let clientOptions = new ClientOptions();
6 clientOptions.endpoint = "bj.bcebos.com"; //Pass in the domain name of the region where the bucket is located
7 let bosClient = new BosClient(credential, clientOptions); // Create BosClient
8let bucketName = "test-harmony-bucket";
9let objectName = "superfile.txt";
10
11
12let cacheDir = getContext().cacheDir;
13let path = cacheDir + '/test.txt';
14
15// download
16try {
17 await bosClient.downloadSuperFile(bucketName, objectName, path + ".copy");
18 let stat = fs.lstatSync(path + ".copy");
19 logger.info(`download super file success, filesize : ${stat.size}`);
20} catch (bosResponse) {
21 logger.error(`errCode: ${bosResponse.error.code}`)
22 logger.error(`requestId: ${bosResponse.error.requestId}`)
23 logger.error(`errMessage: ${bosResponse.error.message}`)
24 logger.error(`statusCode: ${bosResponse.statusCode}`)
25}
Note: This method uses a single thread to download large files to the local device through multiple HTTP requests, each downloading 5 MB.
