Setting Custom Headers

Last updated: 2020-08-26 11:01:38

    Overview

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

    SDK API Reference

    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

    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?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help