Manage objects

Last Updated: Oct 25, 2017

List all objects in a bucket

  1. ListObjectsRequest listObjects = new ListObjectsRequest("<bucketName>");
  2. // Set a prefix
  3. listObjects.setPrefix("file");
  4. // Set a success callback and a failure callback, and send an asynchronous list request
  5. OSSAsyncTask task = oss.asyncListObjects(listObjects, new OSSCompletedCallback<ListObjectsRequest, ListObjectsResult>() {
  6. @Override
  7. public void onSuccess(ListObjectsRequest request, ListObjectsResult result) {
  8. Log.d("AyncListObjects", "Success!");
  9. for (int i = 0; i < result.getObjectSummaries().size(); i++) {
  10. Log.d("AyncListObjects", "object: " + result.getObjectSummaries().get(i).getKey() + " "
  11. + result.getObjectSummaries().get(i).getETag() + " "
  12. + result.getObjectSummaries().get(i).getLastModified());
  13. }
  14. }
  15. @Override
  16. public void onFailure(ListObjectsRequest request, ClientException clientExcepion, ServiceException serviceException) {
  17. // Request exception
  18. if (clientExcepion != null) {
  19. // Local exception, such as a network exception
  20. clientExcepion.printStackTrace();
  21. }
  22. if (serviceException != null) {
  23. // Service exception
  24. Log.e("ErrorCode", serviceException.getErrorCode());
  25. Log.e("RequestId", serviceException.getRequestId());
  26. Log.e("HostId", serviceException.getHostId());
  27. Log.e("RawMessage", serviceException.getRawMessage());
  28. }
  29. }
  30. });
  31. task.waitUntilFinished();

The preceding code enlists all objects whose name contains the “file” prefix in a bucket. Settable parameter names and their functions are as follows:

Name Function
delimiter A character to group object names. All the objects whose names contain the specified prefix and appear before the delimiter for the first time form a group of elements: CommonPrefixes.
marker Set the returned results to begin from the first entry after the marker in alphabetical order.
maxkeys Limit the maximum number of objects returned for one request. If not specified, the default value is 100. The maxkeys value cannot exceed 1,000.
prefix The keys of returned objects must use the prefix.

Note: When you make a query using the prefix, the returned key still contains a prefix. |

Check object availability

OSS Android SDK provides an easy-to-use synchronous interface to check whether a specified object exists on OSS:

  1. try {
  2. if (oss.doesObjectExist("<bucketName>", "<objectKey>")) {
  3. Log.d("doesObjectExist", "object exist.");
  4. } else {
  5. Log.d("doesObjectExist", "object does not exist.");
  6. }
  7. } catch (ClientException e) {
  8. // Local exception, such as a network exception
  9. e.printStackTrace();
  10. } catch (ServiceException e) {
  11. // Service exception
  12. Log.e("ErrorCode", e.getErrorCode());
  13. Log.e("RequestId", e.getRequestId());
  14. Log.e("HostId", e.getHostId());
  15. Log.e("RawMessage", e.getRawMessage());
  16. }

Copy an object

  1. // Create a COPY request
  2. CopyObjectRequest copyObjectRequest = new CopyObjectRequest("<srcBucketName>", "<srcObjectKey>",
  3. "<destBucketName>", "<destObjectKey>");
  4. // Configure the metadata of the object to be copied (optional)
  5. // ObjectMetadata objectMetadata = new ObjectMetadata();
  6. // objectMetadata.setContentType("application/octet-stream");
  7. // copyObjectRequest.setNewObjectMetadata(objectMetadata);
  8. // Asynchronous COPY
  9. OSSAsyncTask copyTask = oss.asyncCopyObject(copyObjectRequest, new OSSCompletedCallback<CopyObjectRequest, CopyObjectResult>() {
  10. @Override
  11. public void onSuccess(CopyObjectRequest request, CopyObjectResult result) {
  12. Log.d("copyObject", "copy success!");
  13. }
  14. @Override
  15. public void onFailure(CopyObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  16. // Request exception
  17. if (clientExcepion != null) {
  18. // Local exception, such as a network exception
  19. clientExcepion.printStackTrace();
  20. }
  21. if (serviceException != null) {
  22. // Service exception
  23. Log.e("ErrorCode", serviceException.getErrorCode());
  24. Log.e("RequestId", serviceException.getRequestId());
  25. Log.e("HostId", serviceException.getHostId());
  26. Log.e("RawMessage", serviceException.getRawMessage());
  27. }
  28. }
  29. });

When you use the preceding code to copy any object, consider the following points:

  • The source object and target object must belong to the same data center.

  • In the COPY operation, if the source object address and target object address are same, you can modify the meta information of the existing object.

  • The object to be copied must not exceed 1 GB. Otherwise, you are required to upload it through multipart upload.

Delete an object

  1. // Create a DELETE request
  2. DeleteObjectRequest delete = new DeleteObjectRequest("<bucketName>", "<objectKey>");
  3. // Asynchronous deletion
  4. OSSAsyncTask deleteTask = oss.asyncDeleteObject(delete, new OSSCompletedCallback<DeleteObjectRequest, DeleteObjectResult>() {
  5. @Override
  6. public void onSuccess(DeleteObjectRequest request, DeleteObjectResult result) {
  7. Log.d("asyncCopyAndDelObject", "success!");
  8. }
  9. @Override
  10. public void onFailure(DeleteObjectRequest 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. });

When you use the preceding code to delete any object, consider the following point:

  • To delete an object with DeleteObject, you must have the write permission on the bucket that stores the object.

Retrieve the object metadata only

The following code obtains the metadata of an object:

  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()); // Get the object length
  7. Log.d("headObject", "object Content Type: " + result.getMetadata().getContentType()); // Obtain the object type
  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.