edit-icon download-icon

Download objects

Last Updated: Oct 31, 2017

Simple download

You can download a specified object, and obtain the input stream of the downloaded object. This operation requires read permission on the object.

Synchronous call:

  1. // Construct an object download request
  2. GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>");
  3. //Set download progress callback
  4. get.setProgressListener(new OSSProgressCallback<GetObjectRequest>() {
  5. @Override
  6. public void onProgress(GetObjectRequest request, long currentSize, long totalSize) {
  7. OSSLog.logDebug("getobj_progress: " + currentSize+" total_size: " + totalSize, false);
  8. }
  9. });
  10. try {
  11. // Process the download request synchronously and then return the result
  12. GetObjectResult getResult = oss.getObject(get);
  13. Log.d("Content-Length", "" + getResult.getContentLength());
  14. // Obtain the object input stream
  15. InputStream inputStream = getResult.getObjectContent();
  16. byte[] buffer = new byte[2048];
  17. int len;
  18. while ((len = inputStream.read(buffer)) != -1) {
  19. // Process the downloaded data, such as image display or object write
  20. }
  21. // Display the metadata of the downloaded object
  22. ObjectMetadata metadata = getResult.getMetadata();
  23. Log.d("ContentType", metadata.getContentType());
  24. } catch (ClientException e) {
  25. // Local exception, such as a network exception
  26. e.printStackTrace();
  27. } catch (ServiceException e) {
  28. // Service exception
  29. Log.e("RequestId", e.getRequestId());
  30. Log.e("ErrorCode", e.getErrorCode());
  31. Log.e("HostId", e.getHostId());
  32. Log.e("RawMessage", e.getRawMessage());
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }

Asynchronous call:

  1. GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>");
  2. //Set download progress callback
  3. get.setProgressListener(new OSSProgressCallback<GetObjectRequest>() {
  4. @Override
  5. public void onProgress(GetObjectRequest request, long currentSize, long totalSize) {
  6. OSSLog.logDebug("getobj_progress: " + currentSize+" total_size: " + totalSize, false);
  7. }
  8. });
  9. OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
  10. @Override
  11. public void onSuccess(GetObjectRequest request, GetObjectResult result) {
  12. // Request succeeds
  13. InputStream inputStream = result.getObjectContent();
  14. byte[] buffer = new byte[2048];
  15. int len;
  16. try {
  17. while ((len = inputStream.read(buffer)) != -1) {
  18. // Process the downloaded data
  19. }
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. @Override
  25. public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  26. // Request exception
  27. if (clientExcepion != null) {
  28. // Local exception, such as a network exception
  29. clientExcepion.printStackTrace();
  30. }
  31. if (serviceException != null) {
  32. // Service exception
  33. Log.e("ErrorCode", serviceException.getErrorCode());
  34. Log.e("RequestId", serviceException.getRequestId());
  35. Log.e("HostId", serviceException.getHostId());
  36. Log.e("RawMessage", serviceException.getRawMessage());
  37. }
  38. }
  39. });

Image processing

Image processing is a service provided to external users by Alibaba Cloud OSS that features massive processing power, security, low costs, and high reliability.

By uploading and storing source images in OSS, you can process images anytime, anywhere, and on any Internet device through a simple RESTful interface.

The OSS image processing offers image processing interfaces. For image uploads, use the OSS upload interfaces. You can build your own image processing service based on OSS image processing.

OSS image processing provides the following features:

While using SDK, you only need to call the request.setxOssProcess() method to process parameters when downloading images. Example:

  1. // Construct the image download request
  2. GetObjectRequest get = new GetObjectRequest("<bucketName>", "example.jpg");
  3. // Image processing
  4. request.setxOssProcess("image/resize,m_fixed,w_100,h_100");
  5. OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
  6. @Override
  7. public void onSuccess(GetObjectRequest request, GetObjectResult result) {
  8. // Request succeeds
  9. InputStream inputStream = result.getObjectContent();
  10. byte[] buffer = new byte[2048];
  11. int len;
  12. try {
  13. while ((len = inputStream.read(buffer)) != -1) {
  14. // Process the downloaded data
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. @Override
  21. public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  22. // Processing exception
  23. }
  24. });

Download within a specified range

You can specify a range for downloading an object. This feature applies to large objects. If the Range parameter is specified in the request header, the returned message contains the length of the entire object and the range of the returned object content.

For example:

  1. GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>");
  2. // Set the range
  3. get.setRange(new Range(0, 99)); // Download 100 (from 0 to 99) bytes starting from 0
  4. // get.setRange(new Range(100, Range.INFINITE)); // Download the range starting from the 100th byte to the end of the object
  5. OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
  6. @Override
  7. public void onSuccess(GetObjectRequest request, GetObjectResult result) {
  8. // Request succeeds
  9. InputStream inputStream = result.getObjectContent();
  10. byte[] buffer = new byte[2048];
  11. int len;
  12. try {
  13. while ((len = inputStream.read(buffer)) != -1) {
  14. // Process the downloaded data
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. @Override
  21. public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  22. // Request exception
  23. if (clientExcepion != null) {
  24. // Local exception, such as a network exception
  25. clientExcepion.printStackTrace();
  26. }
  27. if (serviceException != null) {
  28. // Service exception
  29. Log.e("ErrorCode", serviceException.getErrorCode());
  30. Log.e("RequestId", serviceException.getRequestId());
  31. Log.e("HostId", serviceException.getHostId());
  32. Log.e("RawMessage", serviceException.getRawMessage());
  33. }
  34. }
  35. });

Retrieve the object metadata only

The headObject method can be used to obtain only the metadata of an object without the object body. The code is as follows:

  1. // Create a request for synchronously obtaining object metadata
  2. HeadObjectRequest head = new HeadObjectRequest("<bucketName>", "<objectKey>");
  3. OSSAsyncTask task = oss.asyncHeadObject(head, new OSSCompletedCallback<HeadObjectRequest, HeadObjectResult>() {
  4. @Override
  5. public void onSuccess(HeadObjectRequest request, HeadObjectResult result) {
  6. Log.d("headObject", "object Size: " + result.getMetadata().getContentLength());
  7. Log.d("headObject", "object Content Type: " + result.getMetadata().getContentType());
  8. }
  9. @Override
  10. public void onFailure(HeadObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  11. // Request exception
  12. if (clientExcepion != null) {
  13. // Local exception, such as a network exception
  14. clientExcepion.printStackTrace();
  15. }
  16. if (serviceException != null) {
  17. // Service exception
  18. Log.e("ErrorCode", serviceException.getErrorCode());
  19. Log.e("RequestId", serviceException.getRequestId());
  20. Log.e("HostId", serviceException.getHostId());
  21. Log.e("RawMessage", serviceException.getRawMessage());
  22. }
  23. }
  24. });
  25. // task.waitUntilFinished();
Thank you! We've received your feedback.