tencent cloud

All product documents
Cloud Object Storage
Setting Custom Headers
Last updated: 2024-06-25 10:53:13
Setting Custom Headers
Last updated: 2024-06-25 10:53:13

Overview

This document describes how to include custom headers in a request using the SDK.

SDK API References

For the parameters and method descriptions of all the APIs in the SDK, see SDK API Reference.

Description

COS allows object upload requests to include custom headers that specify user-defined metadata. These headers start with x-cos-meta-, end with a custom suffix, and are saved as part of the object metadata.
If you have activated the Tencent Cloud CI service, you can specify the Pic-Operations header to enable automatic image processing. For detailed API instructions, see Persistence Processing.

Sample code

// The bucket region can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket/. For more information about regions, see https://intl.cloud.tencent.com/document/product/436/6224.
String region = "ap-beijing"; // Bucket region
String commonHeaderKey = "commonexamplekey"; // Key of the custom common header
String commonHeaderValue = "commonexamplevalue"; // Value of the custom common header
String requestHeaderKey = "requestexamplekey"; // Key of the custom request header
String requestHeaderValue = "requestexamplevalue"; // Value of the custom request header

CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder()
.isHttps(true)
.setRegion(region)
.setDebuggable(false)
// Add a custom common header to each request
.addHeader(commonHeaderKey, commonHeaderValue)
.builder();

CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig,
credentialProvider);

// Add a custom header with higher priority than common headers to a single request
HeadObjectRequest headObjectRequest = new HeadObjectRequest(bucket, cosPath);
try {
headObjectRequest.setRequestHeaders(requestHeaderKey, requestHeaderValue, false);
} catch (CosXmlClientException e) {
e.printStackTrace();
}

// Initiate a request
cosXmlService.headObjectAsync(headObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
HeadObjectResult headObjectResult = (HeadObjectResult) result;
}

@Override
public void onFail(CosXmlRequest request, CosXmlClientException clientException,
CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
For more samples, please visit GitHub.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon