tencent cloud

All product documents
Cloud Infinite
Getting Started
Last updated: 2024-02-29 17:31:27
Getting Started
Last updated: 2024-02-29 17:31:27

Overview

CI's media processing and file preview APIs are integrated into the COS XML Java SDK.

Download and Installation

Relevant resources

Download the COS XML Java SDK source code here.
Download the XML Java SDK here.
Download the demo here.
For all the code samples, visit GitHub.

Environmental dependencies

The SDK supports JDK v1.8 and later.
For the JDK installation, see Java Installation and Configuration.
Note
For the definitions of terms such as SecretId, SecretKey, and bucket, see Introduction.
You can find common classes for the COS Java SDK in the following packages:
The classes related to client configuration are in the com.qcloud.cos.* package.
The classes related to permissions are in the com.qcloud.cos.auth.* sub-package.
The classes related to exceptions are in the com.qcloud.cos.exception.* sub-package.
The classes related to requests are in the com.qcloud.cos.model.* sub-package.
The classes related to regions are in the com.qcloud.cos.region.* sub-package.
The classes related to advanced APIs are in the com.qcloud.cos.transfer.* sub-package.

Installing SDK

You can install the Java SDK using Maven or source code:
Using Maven Add required dependencies to the pom.xml file of your Maven project as follows:
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.133</version>
</dependency>
Note
Dependency coordinates may not be on the latest version. Click here to get the latest version.
Using source code Download the source code from GitHub or here, and import it using Maven. For example, to import it into Eclipse, select File > Import > Maven > Existing Maven Projects.

Uninstalling SDK

Uninstall the SDK by removing the POM dependencies or source code.

Directions

The section below describes how to use the Java SDK to perform basic operations, such as initializing a client, creating a bucket, querying a bucket list, uploading an object, querying an object list, downloading an object, or deleting an object. CI's media processing APIs are integrated into the COS XML Java SDK, where you can perform operations related to templates, jobs, workflows, and queues.

Importing classes

The COS Java SDK package is named com.qcloud.cos.*. You can import the classes required for running your program through your IDE, such as Eclipse and IntelliJ.

Initializing the client

Before making any request for COS services, you need to generate a COSClient class object for calling the COS APIs.
Note
COSClient is a thread-safe class that allows multi-thread access to the same instance. Because an instance maintains a connection pool internally, creating multiple instances may lead to resource exhaustion. Make sure that there is only one instance in the program lifecycle, and call the shutdown method to turn off the instance when it is no longer needed. If you need to create a new instance, shut down the previous instance first.
If you use a permanent key to initialize COSClient, you need to obtain your SecretId and SecretKey on the API Key Management page in the CAM Console. A permanent key can be used for most application scenarios. Below is a sample:
// 1. Initialize the user credentials (secretId, secretKey).
String secretId = "COS_SECRETID";
String secretKey = "COS_SECRETKEY";
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2. Set the bucket region. For abbreviations of COS regions, see https://www.tencentcloud.com/document/product/436/6224
// `clientConfig` contains the set methods to set region, HTTPS (HTTP by default), timeout, and proxy. For detailed usage, see the source code or the FAQs about the SDK for Java.
Region region = new Region("COS_REGION");
ClientConfig clientConfig = new ClientConfig(region);
// 3. Generate a COS client.
COSClient cosClient = new COSClient(cred, clientConfig);
You can also use a temporary key to initialize the COSClient. For more information on how to generate and use a temporary key, see Generating and Using Temporary Keys. An example is shown below:
// 1. Pass in the obtained temporary key (tmpSecretId, tmpSecretKey, sessionToken).
String tmpSecretId = "COS_SECRETID";
String tmpSecretKey = "COS_SECRETKEY";
String sessionToken = "COS_TOKEN";
BasicSessionCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);
// 2. Set the bucket region. For abbreviations of COS regions, see https://www.tencentcloud.com/document/product/436/6224
// `clientConfig` contains the set methods to set region, HTTPS (HTTP by default), timeout, and proxy. For detailed usage, please see the source code or the FAQs about the SDK for Java.
Region region = new Region("COS_REGION");
ClientConfig clientConfig = new ClientConfig(region);
// 3. Generate a COS client
COSClient cosClient = new COSClient(cred, clientConfig);
The ClientConfig class is a configuration class containing the following main members:
Member Name
Setting Method
Description
Type
region
Constructor or set method
Bucket region. For the abbreviations of COS regions, see Regions and Access Domain Names.
Region
httpProtocol
Set method
The protocol used by the request. By default, HTTP is used to interact with COS.
HttpProtocol
signExpired
Set method
Validity period (in seconds) of the request signature. Default value: 3600.
int
connectionTimeout
Set method
Timeout period in milliseconds for connection with COS. Default value: 30000.
int
socketTimeout
Set method
Timeout period in milliseconds for the client to read data. Default value: 30000.
int
httpProxyIp
Set method
Proxy server IP
String
httpProxyPort
Set method
Proxy server port
int

Querying the list of buckets with media processing enabled

This API is used to query buckets with media processing enabled under the current account.

Sample request

//1. Create a template request object
MediaBucketRequest request = new MediaBucketRequest();
//2. Add request parameters as detailed in the API documentation
request.setBucketName("examplebucket-1250000000");
//3. Call the API to get the bucket response object
MediaBucketResponse response = client.describeMediaBuckets(request);

Creating a job

This API is used to create a media processing job.

Sample request

//1. Create a job request object
MediaJobsRequest request = new MediaJobsRequest();
//2. Add request parameters as detailed in the API documentation
request.setBucketName("examplebucket-1250000000");
request.setTag("Transcode");
request.getInput().setObject("1.mp4");
request.getOperation().setTemplateId("t0e09a9456d4124542b1f0e44d501*****");
request.getOperation().getOutput().setBucket("examplebucket-1250000000");
request.getOperation().getOutput().setRegion("ap-chongqing");
request.getOperation().getOutput().setObject("2.mp4");
request.setQueueId("p9900025e4ec44b5e8225e70a521*****");
//3. Call the API to get the job response object
MediaJobResponse response = client.createMediaJobs(request);

Canceling a job

Feature description

This API is used to cancel a job that is not in progress.
MediaJobsRequest request = new MediaJobsRequest();
request.setBucketName("examplebucket-1250000000");
request.setJobId("jae776cb4ec3011eab2cdd3817d4*****");
Boolean response = client.cancelMediaJob(request);

Querying a job

This API is used to query the details of a job by job ID.
//1. Create a job request object
MediaJobsRequest request = new MediaJobsRequest();
//2. Add request parameters as detailed in the API documentation
request.setBucketName("examplebucket-1250000000");
request.setJobId("j29a82fea08ba11ebb54bc9d1c05*****");
//3. Call the API to get the job response object
MediaJobResponse response = client.describeMediaJob(request);

Querying job list

This API is used to query the list of jobs in the queue.

Sample request

MediaJobsRequest request = new MediaJobsRequest();
request.setBucketName("examplebucket-1250000000");
request.setQueueId("p9900025e4ec44b5e8225e70a521*****");
request.setTag("Transcode");
MediaListJobResponse response = client.describeMediaJobs(request);
List<MediaJobObject> jobsDetail = response.getJobsDetail();

Shutting down the client

Shut down the COSClient and release the server threads connected over HTTP with the following code:
// Close the client (release server threads).
cosClient.shutdown();

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