APIs and SDKs

Last updated: 2020-05-12 12:08:19

    FAQs About APIs and Other SDKs

    What should I do if an error message occurs, e.g. "Request has expired", when I call an API?

    There are two possible reasons for this:

    • The signature has expired when you initiate the request.
    • Your local system lock is out of sync with the current local time in your time zone.

    In the former case, you are advised to get a new valid request signature before using the API, while in the latter case, you need to correct your system time according to the local time.

    How do I call API to delete partially uploaded files?

    First, call the ListMultipartUploads API to list the partially uploaded files. Then, call the Abort Multipart upload API to abort multipart upload and delete parts that are already uploaded.

    What should I do if it returns correctly after calling the batch deletion API, but in fact the file deletion failed?

    Check the deleted file path, which should not begin with a /.

    Can I use XML API to manage the bucket created and objects uploaded via JSON API?

    Yes, XML API is based on the COS underlying architecture. It can be used to work with data generated by JSON API.

    What is the relationship between XML API and JSON API?

    JSON API is the API used to access COS after September 2016. Its upload domain name is .file.myqcloud.com. JSON API will be in the maintenance state, usable without added new features. JSON API has the same underlying architecture as the standard XML API. Their data is interoperable and can be cross-used, but the two APIs are incompatible and have different domain names.

    Do XML API and JSON API share the same key?

    Yes. For key information, go to the API Key Managementpage in the CAM Console.

    Do XML API and JSON API share the same signature?

    No. They have different signatures. For more information, see:

    Do XML API and JSON API share the same ACL permissions?

    No. They have different ACL permissions.

    How do I get a temporary download URL to the Python SDK?

    For more information, see Pre-signed URL.

    FAQs About the SDK for WeChat Mini Program

    How do I configure and limit the whitelist if multiple domain names are requested in the Mini Program or the bucket name is uncertain?

    When you instantiate the SDK, open the suffix type using ForcePathStyle:true. If you make a suffix type request in a URL format like https://cos-ap-beijing.myqcloud.com//, the bucket name / will also be added to the signature calculation.

    How do I save an image in a Mini Program to my device?

    First, obtain the image URL using cos.getObjectUrl, and then call wx.downloadFile to download the image to get the temp path. When the Save Image button appears, click the button and call wx.saveImageToPhotosAlbum to save the image to your album.

    FAQs About SDK for Java

    Why does java.lang.NoSuchMethodError appear when I run the SDK?

    Typically, it is because a JAR package conflict has occurred: for example, if the JAR package in the HttpClient library in your project does not have method A, but the JAR package that the SDK relies on uses method A, then the HttpClient library in your project will be loaded based on the runtime load order and the runtime will throw the NoSuchMethodError exception.
    Solution: change the version of the package in your project that has caused NoSuchMethodError to the version of the corresponding library in pom.xml in the SDK.

    What if upload is slow in the SDK with IOException frequently printed in the log?

    Cause and solution:

    a. Check whether you are accessing COS through the public network. Currently, CVM instances in the same region as COS can access COS through the private network (the IP ranges resolved by the private domain name is 10, 100, and 169). For more information on COS domain names, see Regions and Access Domain Names. If the public network is used, check whether the egress bandwidth is too small or whether bandwidth resources are occupied by other programs.
    b. Make sure that the logs in the production environment are not at the DEBUG level. INFO logs are recommended.
    c. Currently, the simple upload speed is up to 10 MB/s, and the speed of 32 concurrent uploads using advanced APIs can reach 60 MB/s. If the speed is far lower than those two values, see a and b.
    d. If a WARN log prints IOException, ignore it, as the SDK will retry. IOException may be caused by slow network connection. For possible reasons, see a and b.

    How to create a directory in the SDK?

    In COS, both files and directories are objects, where directories are just objects that end with /. When creating a file, you don't need to create a directory. For example, when you create a file with the object key of xxx/yyy/zzz.txt, set the key to xxx/yyy/zzz.txt instead of creating the object xxx/yyy/. Directories are separated by / to show the hierarchy when displayed in the console. To create a directory object, use the following sample code:

    String bucketName = "examplebucket-1250000000";
    String key = "folder/images/";
    // A directory object is an empty file that ends with /, where a byte stream with a length of 0 is uploaded
    InputStream input = new ByteArrayInputStream(new byte[0]);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(0);
    
    PutObjectRequest putObjectRequest =
    new PutObjectRequest(bucketName, key, input, objectMetadata);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);

    How to use HTTPS in the SDK?

    In the SDK, relevant configuration items are put in the ClientConfig class. Below is the sample code:

    // Initialize user authentication information (secretId, secretKey).
    String secretId = "COS_SECRETID";
    String secretKey = "COS_SECRETKEY";
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // Set the bucket region. For abbreviations of COS regions, see https://intl.cloud.tencent.com/document/product/436/6224.
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // Configure HTTPS
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // Generate a COS client.
    COSClient cosClient = new COSClient(cred, clientConfig);

    How to use a proxy in the SDK?

    If you need to use a proxy to access COS, you can configure a proxy IP (or a domain name) and port in the ClientConfig class. Below is the sample code:

    // Initialize user authentication information (secretId, secretKey).
    String secretId = "COS_SECRETID";
    String secretKey = "COS_SECRETKEY";
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // Set the bucket region. For abbreviations of COS regions, see https://intl.cloud.tencent.com/document/product/436/6224.
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // Configure a proxy (you need to set both the IP and port at the same time.)
    // Set the proxy IP (or pass in the domain name)
    clientConfig.setHttpProxyIp("192.168.2.3");
    // Set the proxy port
    clientConfig.setHttpProxyPort(8080);
    // Generate a COS client.
    COSClient cosClient = new COSClient(cred, clientConfig);

    How to set up custom EndpointBuilder?

    In scenarios where the endpoint needs to be specified for an API request, you need to implement the buildGeneralApiEndpoint and buildGetServiceApiEndpoint functions in the EndpointBuilder API to specify the remote endpoints for a general API request and GETService request respectively. Examples:

    // Step 1: implement the two functions in the EndpointBuilder API.
    class SelfDefinedEndpointBuilder implements EndpointBuilder {
        @Override
        public String buildGeneralApiEndpoint(String bucketName) {
            return String.format("%s.%s", bucketName, "mytest.com");
        }
    
        @Override
        public String buildGetServiceApiEndpoint() {
            return "service.mytest.com";
        }
    }
    
    // Step 2: Initialize the client.
    String secretId = "COS_SECRETID";
    String secretKey = "COS_SECRETKEY";
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    SelfDefinedEndpointBuilder selfDefinedEndpointBuilder = new SelfDefinedEndpointBuilder();
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
    clientConfig .setEndpointBuilder(selfDefinedEndpointBuilder);
    COSClient cosClient = new COSClient(cred, clientConfig);

    Do I need to add the '/' prefix to key values used in SDK operations such as upload, download, and batch delete?

    The key value need not be prefixed with '/' for COS objects. For example, if you set the key value to an uploaded COS object exampleobject, you can access it using the URL http://cos.ap-guangzhou.myqcloud.com/exampleobject.

    Do no pass in a key prefixed with '/' in a batch delete request, or the object deletion may fail.

    Was this page helpful?

    Was this page helpful?

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