Obtaining Object Metadata (SDK for Java)
Function
Object metadata is a set of name-value pairs that describe the object and is used for object management.
Users with the READ permission on objects can send HEAD requests to retrieve metadata from objects. The metadata of the objects is included in the response.
This operation supports server-side encryption.
If you have any questions during development, post them on the Issues page of GitHub.
Restrictions
- To obtain object metadata, you must be the bucket owner or have the required permission (obs:object:GetObject in IAM or GetObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
- You can also call metadata.getOriginalHeaders to obtain information about all original response headers.
Method
obsClient.getObjectMetadata(GetObjectMetadataRequest request)
Request Parameters
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| request | Yes | Explanation: Request parameters for obtaining object metadata. For details, see Table 2. |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| bucketName | String | Yes | Explanation: Bucket name. Restrictions:
Default value: None |
| objectKey | String | Yes | Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None |
| versionId | String | No | Explanation: Object version ID. Value range: The value must contain 32 characters. Default value: None |
| userHeaders | HashMap<String, String> | No | Explanation: User header list. In HashMap, the String key and value indicate the name and value of the user header field respectively. The SDK does not process the userHeaders and instead transparently transmits it to the server for later use. Value range: The value must contain 32 characters. Default value: None |
| encodeHeaders | boolean | No | Explanation: Whether to enable OBS to automatically encode request headers. Due to HTTP coding restrictions, only ASCII characters can be sent. If your request headers contain full-width characters, the SDK will URL encode these characters before sending the request. When you use a browser to access the object metadata, the browser automatically decodes the data. Value range: true: Encoding with SDK is enabled. false: Encoding with SDK is disabled. Default value: true |
| sseHeader | No | Explanation: Server-side decryption header. For details, see Table 3. |
| Parameter | Type | Mandatory (Yes/No) | Description |
|---|---|---|---|
| algorithm | Yes | Explanation: SSE-C is used for encrypting objects on the server side. Value range: AES256, indicating AES is used to encrypt the object in SSE-C. For details, see Table 4. Default value: None | |
| sseAlgorithm | No | Explanation: Encryption algorithm. Restrictions: Only AES256 is supported. Value range: See Table 5. Default value: None | |
| sseCKey | byte[] | Yes | Explanation: Key used for encrypting the object when SSE-C is used, in byte[] format. Default value: None |
| sseCKeyBase64 | String | No | Explanation: Base64-encoded key used for encrypting the object when SSE-C is used. Default value: None |
Responses
| Parameter | Type | Description |
|---|---|---|
| contentLength | Long | Explanation: Object size. The object size in a single upload ranges from 0 to 5 GB. To upload a file larger than 5 GB, use the multipart upload by referring to Multipart Upload Overview (SDK for Java). If this parameter is not specified, the SDK automatically calculates the size of the object. Value range: None |
| contentType | String | Explanation: MIME type of the object file. MIME type is a standard way of describing a data type and is used by the browser to decide how to display data. If this parameter is not specified, the SDK determines the file type based on the object name suffix and assigns a value to the parameter. For example, if the object name suffix is .xml, the object is an application/xml file. If the suffix is .html, the object is a text/html file. Value range: |
| contentEncoding | String | Explanation: Content-Encoding header in the response. It specifies which encoding is applied to the object. Value range: None |
| contentDisposition | String | Explanation: Provides a default file name for the requested object. When the object with the default file name is being downloaded or accessed, the content is displayed as part of a web page in the browser or as an attachment in a download dialog box. Value range: None |
| cacheControl | String | Explanation: Cache-Control header in the response. It specifies the cache behavior of the web page when an object is downloaded. Value range: None |
| contentLanguage | String | Explanation: Language or language combination for visitors to customize and use. For details, see the definition of ContentLanguage in the HTTP protocol. Value range: None |
| expires | String | Explanation: The time a cached web page object expires. The time must be in the GMT format. Value range: None |
| contentMd5 | String | Explanation: Base64-encoded MD5 value of the object data. It is provided for the OBS server to verify data integrity. The OBS server will compare this MD5 value with the MD5 value calculated based on the object data. If the two values are not the same, HTTP status code 400 is returned. If the MD5 value is not specified, the OBS server will not verify the MD5 value of the object. Value range: Base64-encoded 128-bit MD5 value of the request body calculated according to RFC 1864. Example: n58IG6hfM7vqI4K0vnWpog== |
| lastModified | java.util.Date | Explanation: Last time the target part was modified. Value range: None |
| crc64 | String | Explanation: The 64-bit CRC value calculated for the object based on the ECMA-182 standard. The CRC64 value is the unique identifier of the object content. It can be used to determine whether the object content is changed. If an object has different CRC64 values when being uploaded and downloaded, its content has been changed. CRC64 reflects changes to the content of the object, not its metadata. Value range: A 64-bit CRC value calculated based on the ECMA-182 standard |
| storageClass | Explanation: Storage class of an object that can be specified at object creation. If you do not specify this header, the object inherits the storage class of the bucket. Value range:
| |
| webSiteRedirectLocation | String | Explanation: If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL. This parameter specifies the address the request for the object is redirected to. To another object in the same bucket: WebsiteRedirectLocation:/anotherPage.html To an external URL: WebsiteRedirectLocation:http://www.example.com/ Value range: None |
| nextPosition | long | Explanation: Start position for the next append upload. Value range: 0 to the object length, in bytes. |
| appendable | boolean | Explanation: Whether the object is appendable. Value range: true: The object is appendable. false: The object is not appendable. |
| userMetadata | Map<String, Object> | Explanation: User-defined metadata of the object. To define it, you can add a header starting with x-obs-meta- in the request. In Map, the String key indicates the name of the user-defined metadata that starts with x-obs-meta-, and the Object value indicates the value of the user-defined metadata. To obtain the user-defined metadata of an object, use ObsClient.getObjectMetadata. For details, see Obtaining Object Metadata (SDK for Java). An object can have multiple pieces of metadata. The size of the metadata cannot exceed 8 KB in total. When you call ObsClient.getObject to download an object, its user-defined metadata will also be downloaded. Value range: None |
Code Examples
This example returns the metadata of object objectname in bucket examplebucket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.ObjectMetadata; public class GetObjectMetadata001 { public static void main(String[] args) { // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. // Obtain an AK/SK pair and a security token using environment variables or import them in other ways. // String securityToken = System.getenv("SECURITY_TOKEN"); // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. String endPoint = "https://obs.ap-southeast-1.myhuaweicloud.com"; // Obtain an endpoint using environment variables or import it in other ways. //String endPoint = System.getenv("ENDPOINT"); // Create an ObsClient instance. // Use a permanent AK/SK pair to initialize the client. ObsClient obsClient = new ObsClient(ak, sk, endPoint); // Use a temporary AK/SK pair and security token to initialize the client. // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint); try { // Obtain the object metadata. ObjectMetadata metadata = obsClient.getObjectMetadata("examplebucket", "objectname"); System.out.println("getObjectMetadata successfully"); System.out.println("ContentType:" + metadata.getContentType()); System.out.println("ContentLength:" + metadata.getContentLength()); // Obtain the user-defined metadata. System.out.println("UserMetadata:" + metadata.getUserMetadata("property")); // Obtain all original response headers. System.out.println("OriginalHeaders:" + metadata.getOriginalHeaders()); } catch (ObsException e) { System.out.println("getObjectMetadata failed"); // Request failed. Print the HTTP status code. System.out.println("HTTP Code:" + e.getResponseCode()); // Request failed. Print the server-side error code. System.out.println("Error Code:" + e.getErrorCode()); // Request failed. Print the error details. System.out.println("Error Message:" + e.getErrorMessage()); // Request failed. Print the request ID. System.out.println("Request ID:" + e.getErrorRequestId()); System.out.println("Host ID:" + e.getErrorHostId()); e.printStackTrace(); } catch (Exception e) { System.out.println("getObjectMetadata failed"); // Print other error information. e.printStackTrace(); } } } |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot