Download

Last update:2025-08-18 15:54:18

The WOS Java SDK provides comprehensive object download interfaces. You can download objects using WosClient.getObject.

1. Stream Download

The following code demonstrates how to perform a stream download:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
final WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

WosObject wosObject = wosClient.getObject("bucketname", "objectname");

// Read object content
System.out.println("Object content:");
InputStream input = wosObject.getObjectContent();
byte[] b = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
int len;
while ((len = input.read(b)) != -1) {
    bos.write(b, 0, len);
}
System.out.println(new String(bos.toByteArray()));
bos.close();
input.close();

Notes:

  • WosClient.getObject returns a WosObject instance containing the bucket name, object key, object metadata, object input stream, and more.
  • You can use the input stream to read the object’s content into local files or memory.

Important:

  • Always explicitly close the input stream retrieved by WosObject.getObjectContent to avoid resource leaks.

2. Range Download

If you only need to download part of an object, you can use range download to retrieve a specified data range. For example, specifying a range of 0-1000 will return bytes 0 through 1000 (inclusive)—a total of 1001 bytes, i.e., [0, 1000]. If an invalid range is specified, the entire object will be returned. The example below shows how to perform a range download:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
final WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

GetObjectRequest request = new GetObjectRequest("bucketname", "objectname");
// Specify the start and end range
request.setRangeStart(0L);
request.setRangeEnd(1000L);
WosObject wosObject = wosClient.getObject(request);

// Read data
byte[] buf = new byte[1024];
InputStream in = wosObject.getObjectContent();
for (int n = 0; n != -1;) {
    n = in.read(buf, 0, buf.length);
}
in.close();

Notes:

  • If the specified range is invalid (e.g., negative or exceeds the file size), the entire object will be returned.
  • You can implement parallel downloads for large files using concurrent range requests. Refer to ConcurrentDownloadObjectSample for more details.

3. Conditional Download

You can specify constraints when downloading an object. If the specified condition is not met, an exception is thrown and the download fails.

The available conditional parameters are:

Parameter Description Java SDK Method
If-Modified-Since Returns object content if it has been modified since the specified date. Otherwise, an exception is thrown. GetObjectRequest.setIfModifiedSince
If-Unmodified-Since Returns object content if it has not been modified since the specified date. Otherwise, an exception is thrown. GetObjectRequest.setIfUnmodifiedSince
If-Match Returns object content if the object’s ETag matches the specified value. Otherwise, an exception is thrown. GetObjectRequest.setIfMatchTag
If-None-Match Returns object content if the object’s ETag does not match the specified value. Otherwise, an exception is thrown. GetObjectRequest.setIfNoneMatchTag

Notes:

  • The object’s ETag is the MD5 checksum of its data.
  • If you specify If-Unmodified-Since or If-Match and the condition is not met, the HTTP status code returned will be 412 Precondition Failed.
  • If you specify If-Modified-Since or If-None-Match and the condition is not met, the HTTP status code returned will be 304 Not Modified.

Conditional Download Example

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
final WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

GetObjectRequest request = new GetObjectRequest("bucketname", "objectname");
request.setRangeStart(0L);
request.setRangeEnd(1000L);
request.setIfModifiedSince(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01"));
WosObject wosObject = wosClient.getObject(request);

wosObject.getObjectContent().close();

4. Override Response Headers

When downloading an object, you can override certain HTTP/HTTPS response headers. The following headers can be overridden:

Parameter Description Java SDK Method
contentType Override the Content-Type header ObjectRepleaceMetadata.setContentType
contentLanguage Override the Content-Language ObjectRepleaceMetadata.setContentLanguage
expires Override the Expires header ObjectRepleaceMetadata.setExpires
cacheControl Override the Cache-Control header ObjectRepleaceMetadata.setCacheControl
contentDisposition Override Content-Disposition ObjectRepleaceMetadata.setContentDisposition
contentEncoding Override Content-Encoding ObjectRepleaceMetadata.setContentEncoding

Overriding Response Header Example

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

GetObjectRequest request = new GetObjectRequest("bucketname", "objectname");
ObjectRepleaceMetadata replaceMetadata = new ObjectRepleaceMetadata();
replaceMetadata.setContentType("image/jpeg");
request.setReplaceMetadata(replaceMetadata);

WosObject wosObject = wosClient.getObject(request);
System.out.println(wosObject.getMetadata().getContentType());

wosObject.getObjectContent().close();

5. Retrieve Custom Metadata

You can retrieve user-defined metadata when downloading an object. The following code demonstrates how to access custom metadata:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
final WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

// Upload object with custom metadata
PutObjectRequest request = new PutObjectRequest("bucketname", "objectname");
ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata("property", "property-value");
request.setMetadata(metadata);
wosClient.putObject(request);

// Download object and retrieve custom metadata
WosObject wosObject = wosClient.getObject("bucketname", "objectname");
System.out.println(wosObject.getMetadata().getUserMetadata("property"));

wosObject.getObjectContent().close();

6. Downloading Archived Objects

To download an object stored in the archive storage class, you must first restore it using WosClient.restoreObject. The following example demonstrates how to download an archived object:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
final WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

RestoreObjectRequest request = new RestoreObjectRequest();
request.setBucketName("bucketname");
request.setObjectKey("objectname");
request.setDays(1);
wosClient.restoreObject(request);

// Wait for the object to be restored
Thread.sleep(60 * 6 * 1000);

// Download object
WosObject wosObject = wosClient.getObject("bucketname", "objectname");

wosObject.getObjectContent().close();

Notes:

  • The object specified in WosClient.restoreObject must be of the archive storage class. If not, an exception will be thrown.
Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!