Updated on 2024-06-21 GMT+08:00

Obtaining Customized Metadata

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 .

After an object is successfully downloaded, its customized data is returned. 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 instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

// Upload the object and customize the metadata.
PutObjectRequest request = new PutObjectRequest("bucketname", "objectname");
ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata("property", "property-value");
request.setMetadata(metadata);
obsClient.putObject(request);
// Download the object and obtain the customized metadata.
GetObjectRequest request = new GetObjectRequest("bucketname", "objectname");
ObsObject obsObject = obsClient.getObject(request);
// Get object metadata.
System.out.println(obsObject.getMetadata().getContentType());
System.out.println(obsObject.getMetadata().getUserMetadata("property"));

obsObject.getObjectContent().close();
  • After ObsClient.getObject is called, an instance of ObsObject will be returned. This instance contains the residing bucket, name, properties, and input streams of the object.
  • You can operate the input streams of an object to read and write the object to a local file or to the memory.
  • 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 GetObjectRequest.setIsEncodeHeaders(false) to disable auto decoding.
  • You can also call obsObject.getMetadata().getOriginalHeaders() to obtain information about all original response headers.