Updated on 2024-12-03 GMT+08:00

Obtaining Object Attributes

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

You can call ObsClient.getObjectMetadata to obtain attributes of an object, including the length, MIME type, and custom metadata. Sample code is as follows:

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ObjectMetadata metadata = obsClient.getObjectMetadata("bucketname", "objectname");
Log.i("GetObjectMetadata","\t" + metadata.getContentType());
Log.i("GetObjectMetadata","\t" + metadata.getContentLength());
// Obtain custom metadata.
Log.i("GetObjectMetadata","\t" + metadata.getUserMetadata("property"));
// Obtain all original response headers.
Log.i("GetObjectMetadata","\t" + metadata.getOriginalHeaders());
  • Due to HTTP coding restrictions, non-ASCII characters cannot be sent. The SDK will decode the information in response headers using URL decoding rules.
  • If you do not need the SDK to decode for you, call GetObjectMetadataRequest.setIsEncodeHeaders(false) to disable auto decoding.
  • You can also call metadata.getOriginalHeaders to obtain information about all original response headers.