Download file
The BOS Java SDK provides a variety of file download APIs, allowing users to download files from BOS in the following ways:
- Simple streaming download
- Download to a local file
- Resumable download
- Range download
- Download progress bar
For the complete sample code, please refer to File Download Demo
Simple streaming download
Users can read the object into a stream using the following code:
1public void getObject(BosClient client, String bucketName, String objectKey)
2 throws IOException {
3 //Obtain the object, with returned result BosObject object
4 BosObject object = client.getObject(bucketName, objectKey);
5 // Retrieve ObjectMeta
6 ObjectMetadata meta = object.getObjectMetadata();
7 //Obtain the object's input stream
8 InputStream objectContent = object.getObjectContent();
9 // Process object
10 ...
11 // Close stream
12 objectContent.close();
13}
Note:
- The BosObject includes various details about the object, such as the bucket it belongs to, the object’s name, metadata, and an input stream. Users can read the object's content into a file or memory by interacting with the input stream.
- ObjectMetadata contains information such as the ETag defined during object upload, HTTP headers, and custom metadata.
- Users can also use the getObjectContent method of BosObject to obtain the input stream of the returned object, enabling operations on the object's content.
Directly download an object to a file
Users can directly download an object to the specified file using the following code:
1// Create GetObjectRequest
2GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey);
3
4 // Download object to file
5ObjectMetadata objectMetadata = client.getObject(getObjectRequest, new File("/path/to/file","filename"));
When downloading an object directly to a specified file using the mentioned method, the method returns an ObjectMetadata object.
Range download
To implement more functions, you can use GetObjectRequest to specify the download range for more refined acquisition of the object. If the specified download range is 0-100, it will return data from byte 0 to byte 100, including byte 100, with a total of 101 bytes of data, i.e., [0, 100].
1// Create GetObjectRequest
2GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey);
3 // Obtain data within the 0~100 byte range
4getObjectRequest.setRange(0, 100);
5 //Obtain the object, with returned result BosObject object
6BosObject object = client.getObject(getObjectRequest);
The range of the returned object can be configured using the setRange method of the getObjectRequest. This allows users to perform segmented downloads and resumable uploads of files.
Download the image-processed file
The Java SDK supports image processing while downloading files. You only need to set the x-bce-process parameter in the request. For the usage of specific parameters, please refer to Image Processing. Example code is as follows:
1// Create GetObjectRequest
2GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey);
3 // Set image processing to zoom
4getObjectRequest.setxBceProcess("image/resize,m_fixed,w_200,h_100");
5 // Get the object, with the processed BosObject returned
6BosObject object = client.getObject(getObjectRequest);
Other methods
Get the storage class of the object
The storage class attributes of an object are divided into STANDARD (standard storage), STANDARD_IA (infrequent access storage), and COLD (cold storage). This can be achieved through the following code:
1public void getObjectStorageClass(){
2 ObjectMetadata meta = client.getObjectMetadata(bucketName, key);
3 String storageClass = meta.getStorageClass();
4}
Get only ObjectMetadata
The getObjectMetadata method is designed to retrieve only the ObjectMetadata, not the object itself. This can be demonstrated in the following code:
1ObjectMetadata objectMetadata = client.getObjectMetadata(bucketName, objectKey);
The parameters available for calling in the getObjectMetadata parsing class are:
| Parameters | Description |
|---|---|
| contentType | Object type |
| contentLength | Object size |
| contentMd5 | Object MD5 |
| etag | The HTTP protocol entity tag of object |
| storageClass | Storage class of the object |
| userMetadata | If userMetadata custom meta is specified in PutObject, this item will be returned |
| xBceCrc | If the CRC value (Cyclic Redundancy Check code) of the object is specified in PutObject, this item will be returned |
Single-link rate limit
Baidu AI Cloud Object Storage (BOS) applies bandwidth limits per bucket. When the user's upload or download bandwidth usage reaches the threshold, the error code RequestRateLimitExceeded will be triggered.
To ensure normal service usage, BOS implements traffic control during upload and download processes, ensuring that large traffic services do not impact other applications.
The download API supports setting the specified rate limit value. The value range of the rate limit value is 819200~838860800 in bit/s, that is, 100KB/s~100MB/s. The rate limit value must be a number. BOS will limit the rate of this request according to the specified rate limit value. When the rate limit value is not within this range or is illegal, it will return the error.
- GetObject sample code:
1GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey);
2 // Specify the download speed limit as 819,200 bit/s, i.e., 100 KB/s
3getObjectRequest.setTrafficLimitBitPS(819200);
4ObjectMetadata objectMetadata = client.getObject(getObjectRequest, new File("/path/to/file","filename"));
Download progress bar
The SDK provides real-time upload progress information during uploads. The upload progress callback APIs defined in the Java SDK allow you to specify operations to be performed during the upload, such as updating the API.
1public interface ProgressCallback<T> {
2 void onProgress(long currentSize, long totalSize, T data);
3}
- GetObject sample code:
1File file = new File("tmp-download");
2BosProgressCallback<Object> callback = new BosProgressCallback<Object>() {
3 @Override
4 public void onProgress(long currentSize, long totalSize, Object data) {
5 System.out.println("get " + currentSize + "/" + totalSize);
6 }
7};
8GetObjectRequest getObjectRequest = new GetObjectRequest("bucketName","objectKey");
9getObjectRequest.setProgressCallback(callback);
10this.client.getObject(getObjectRequest, file);
