代码示例
1.媒资上传,详细参数请参考API说明。
Copied!
public class UploadTest {
private final static String AK = System.getenv("HUAWEICLOUD_SDK_AK");
private final static String SK = System.getenv("HUAWEICLOUD_SDK_SK");
private final static String REGION = "cn-north-4";
public static VodClient getVodClient() {
HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3);
ICredential auth = new BasicCredentials()
.withAk(AK)
.withSk(SK);
VodClient vodClient = VodClient.newBuilder().withCredential(auth)
.withHttpConfig(httpConfig)
.withRegion(VodRegion.valueOf(REGION))
.build();
return vodClient;
}
public static void main(String[] args) {
VodClient vodClient = getVodClient();
CreateAssetByFileUploadRequest uploadRequest = new CreateAssetByFileUploadRequest();
CreateAssetByFileUploadReq uploadReq = new CreateAssetByFileUploadReq();
uploadReq.withVideoType("MP4".toUpperCase());
uploadReq.withVideoName("videoName");
uploadReq.withTitle("title");
uploadRequest.withBody(uploadReq);
File file = new File("{filePath}");
try (FileInputStream inputStream = new FileInputStream(file)) {
CreateAssetByFileUploadResponse uploadResponse = vodClient.createAssetByFileUpload(uploadRequest);
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
HttpPut httpput = new HttpPut(uploadResponse.getVideoUploadUrl());
httpput.setHeader("Content-Type", "video/mp4");
InputStreamEntity entity = new InputStreamEntity(inputStream);
httpput.setEntity(entity);
CloseableHttpResponse rsp = closeableHttpClient.execute(httpput);
System.out.println(rsp.getStatusLine().getStatusCode());
if (rsp.getStatusLine().getStatusCode() == 200) {
ConfirmAssetUploadRequest confirmRequest = new ConfirmAssetUploadRequest();
ConfirmAssetUploadReq confirmReq = new ConfirmAssetUploadReq();
confirmReq.withStatus(ConfirmAssetUploadReq.
StatusEnum.fromValue("CREATED"));
confirmReq.withAssetId(uploadResponse.getAssetId());
confirmRequest.withBody(confirmReq);
vodClient.confirmAssetUpload(confirmRequest);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
版本说明
本示例基于华为云SDK V3.0版本开发。
功能介绍
将存储在本地的音视频文件上传到点播服务,并在上传后,自动处理音视频,如发布、转码等,您可以调用VOD API进行媒资创建,并通过OBS API将音视频文件上传到VOD的存储桶中。
准备工作
1.已安装JDK1.8及以上版本并完成环境配置。
2.已安装Maven,如未安装,请下载安装。
3.已具备IDEA等开发环境。
4.查询region,请参考地区和终端节点。
5.获取点播SDK,可通过华为SDK中心。
代码示例
1.媒资上传,详细参数请参考API说明。
public class UploadTest { // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份认证为例,运行示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 private final static String AK = System.getenv("HUAWEICLOUD_SDK_AK"); private final static String SK = System.getenv("HUAWEICLOUD_SDK_SK"); private final static String REGION = "cn-north-4"; // 根据服务实际节点填写,如cn-north-1,cn-east-2 /** * 初始化VodClient *@return 返回 已初始化好的VodClient */ public static VodClient getVodClient() { HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3); //根据实际需要,可以设置代理 //httpConfig.withProxyHost("proxy.xxxxxx.com").withProxyPort(8080).withProxyUsername("xxxxxx").withProxyPassword("********"); ICredential auth = new BasicCredentials() .withAk(AK) .withSk(SK); VodClient vodClient = VodClient.newBuilder().withCredential(auth) .withHttpConfig(httpConfig) .withRegion(VodRegion.valueOf(REGION)) .build(); return vodClient; } /** * 通过接口上传媒资 */ public static void main(String[] args) { VodClient vodClient = getVodClient(); CreateAssetByFileUploadRequest uploadRequest = new CreateAssetByFileUploadRequest(); CreateAssetByFileUploadReq uploadReq = new CreateAssetByFileUploadReq(); uploadReq.withVideoType("MP4".toUpperCase()); // 例如上传文件格式为MP4 uploadReq.withVideoName("videoName"); uploadReq.withTitle("title"); uploadRequest.withBody(uploadReq); File file = new File("{filePath}"); // 文件所在路径,线上编程体验可使用相对路径:"src/main/java/com/huawei/vod/video/demo.mp4" try (FileInputStream inputStream = new FileInputStream(file)) { // 创建上传任务 CreateAssetByFileUploadResponse uploadResponse = vodClient.createAssetByFileUpload(uploadRequest); // 通过返回的临时授权路径将媒资上传到点播桶中 CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); HttpPut httpput = new HttpPut(uploadResponse.getVideoUploadUrl()); httpput.setHeader("Content-Type", "video/mp4"); // 必填项,参考详情参数内的参考表 InputStreamEntity entity = new InputStreamEntity(inputStream); httpput.setEntity(entity); CloseableHttpResponse rsp = closeableHttpClient.execute(httpput); System.out.println(rsp.getStatusLine().getStatusCode()); // 文件上传成功则调确认接口 if (rsp.getStatusLine().getStatusCode() == 200) { ConfirmAssetUploadRequest confirmRequest = new ConfirmAssetUploadRequest(); ConfirmAssetUploadReq confirmReq = new ConfirmAssetUploadReq(); confirmReq.withStatus(ConfirmAssetUploadReq. StatusEnum.fromValue("CREATED")); confirmReq.withAssetId(uploadResponse.getAssetId()); // 由创建上传任务 confirmRequest.withBody(confirmReq); vodClient.confirmAssetUpload(confirmRequest); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
参考
更多信息请参考APIExplorer。
修订记录