All Products
Search
Document Center

Object Storage Service:Query object metadata by using OSS SDK for Android

Last Updated:Feb 21, 2024

You can call the HeadObject operation to query only the metadata of an object. When you call this operation, the object content is not returned.

Usage notes

  • Object metadata describes the attributes of an object. The metadata includes standard HTTP headers and user metadata. You can use standard HTTP headers to specify HTTP request policies for an object, such as caching and forced download. You can configure user metadata for an object to identify the purposes or attributes of the object. For more information, see Manage object metadata.

  • You cannot use Object Storage Service (OSS) SDK for Android to configure or modify object metadata.

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

Examples

The following sample code provides an example on how to query object metadata:

// Create a request to synchronously query object metadata. 
// Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt. Do not include the bucket name in the full path of the object. 
HeadObjectRequest head = new HeadObjectRequest("examplebucket", "exampledir/exampleobject.txt");

// Query the object metadata. 
OSSAsyncTask task = oss.asyncHeadObject(head, new OSSCompletedCallback<HeadObjectRequest, HeadObjectResult>() {
    @Override
    public void onSuccess(HeadObjectRequest request, HeadObjectResult result) {
    
    // Obtain the length of the object. 
        Log.d("headObject", "object Size: " + result.getMetadata().getContentLength()); 
    // Query the type of the object. 
        Log.d("headObject", "object Content Type: " + result.getMetadata().getContentType()); 
    }

    @Override
    public void onFailure(HeadObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientExcepion != null) {
            // Handle client exceptions, such as network exceptions. 
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Handle service exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

// task.waitUntilFinished(); // Wait until the object metadata is queried.

References

  • For more information about the API operation that you can call to query object metadata, see GetObjectMeta.

  • For more information about how to initialize an OSSClient instance, see Initialization.