使用腾迅云产品时,一般情况下,腾迅都会提供一个 SDK 供我们调用。
拿云直播里面的混流做为案例,讲解一下 SDK 的使用方法。通过官方的代码生成器,可以生成一个 Demo 做初步验证。
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
| import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.live.v20180801.LiveClient;
import com.tencentcloudapi.live.v20180801.models.CreateCommonMixStreamRequest; import com.tencentcloudapi.live.v20180801.models.CreateCommonMixStreamResponse;
public class CreateCommonMixStream { public static void main(String [] args) { try { Credential cred = new Credential("SecretId", "SecretKey"); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("live.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); LiveClient client = new LiveClient(cred, "ap-shanghai", clientProfile); String params = "{}"; CreateCommonMixStreamRequest req = CreateCommonMixStreamRequest.fromJsonString(params, CreateCommonMixStreamRequest.class); CreateCommonMixStreamResponse resp = client.CreateCommonMixStream(req); System.out.println(CreateCommonMixStreamRequest.toJsonString(resp)); } catch (TencentCloudSDKException e) { System.out.println(e.toString()); }
} }
|
我们可以把一些配置信息提取到 yml 配置文件中,同时让 Spring IOC 容器来管理 LiveClient。
1 2 3 4 5
| tencent-cloud: secretId: secretKey: endpoint: live.tencentcloudapi.com clientRegion: ap-shanghai
|
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
| import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.live.v20180801.LiveClient; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration @ConfigurationProperties("tencent-cloud") @Data public class TencentCloudConfig {
private String secretId; private String secretKey; private String endpoint; private String clientRegion;
@Bean public LiveClient getLiveClient() {
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile);
LiveClient client = new LiveClient(cred, clientRegion, clientProfile); return client; }
}
|
在 Service 的实现中,我们就可以直接注入 LiveClient 了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Service public class MixStreamServiceImpl implements MixStreamService {
@Autowired private LiveClient liveClient; @Override public CreateCommonMixStreamResponse mixStream() { CreateCommonMixStreamRequest mixStreamRequest = new CreateCommonMixStreamRequest(); CreateCommonMixStreamResponse response; try { response = liveClient.CreateCommonMixStream(mixStreamRequest); } catch (TencentCloudSDKException e) { e.printStackTrace(); } return response; } }
|