Updated on 2024-06-21 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 .

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/eu/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 uses a URL to decode the information in the response header. For example, if content-disposition in your metadata is set to attachment; filename="%E4%B8%AD%E6%96%87.txt", the result obtained by the SDK is attachment; filename="Chinese characters.txt".
  • 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.