Last update:2025-08-18 15:54:18
The WOS Java SDK provides comprehensive object download interfaces. You can download objects using WosClient.getObject.
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.Important:
- Always explicitly close the input stream retrieved by
WosObject.getObjectContentto avoid resource leaks.
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:
ConcurrentDownloadObjectSample for more details.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:
412 Precondition Failed.304 Not Modified.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();
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 |
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();
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();
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:
WosClient.restoreObject must be of the archive storage class. If not, an exception will be thrown.