api学习

本文档提供了Java SDK媒体上传相关功能的API调用示例,包含上传地址和凭证、注册媒资信息等。要实现完整的媒体文件上传,可配合客户端上传,也可以直接使用Java上传SDK。

参考:https://help.aliyun.com/document_detail/61063.html

初始化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;

public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
    String regionId = "cn-shanghai";  // 点播服务接入区域
    DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
}

token服务

 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
44
45
46
47
48
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;

/**
 * 获取视频上传地址和凭证
 * @param client 发送请求客户端
 * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
 * @throws Exception
*/
public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client) throws Exception {
    CreateUploadVideoRequest request = new CreateUploadVideoRequest();
    request.setTitle("this is a sample");
    request.setFileName("filename.mp4");

    //UserData,用户自定义设置参数,用户需要单独回调URL及数据透传时设置(非必须)
    //JSONObject userData = new JSONObject();

    //UserData回调部分设置
    //JSONObject messageCallback = new JSONObject();
    //messageCallback.put("CallbackURL", "http://192.168.0.0/16");
    //messageCallback.put("CallbackType", "http");
    //userData.put("MessageCallback", messageCallback.toJSONString());

    //UserData透传数据部分设置
    //JSONObject extend = new JSONObject();
    //extend.put("MyId", "user-defined-id");
    //userData.put("Extend", extend.toJSONString());

    //request.setUserData(userData.toJSONString());

    return client.getAcsResponse(request);
}

// 请求示例
public static void main(String[] argv) {
    DefaultAcsClient client = initVodClient("<Your AccessKeyId>", "<Your AccessKeySecret>");
    CreateUploadVideoResponse response = new CreateUploadVideoResponse();
    try {
        response = createUploadVideo(client);
        System.out.print("VideoId = " + response.getVideoId() + "\n");
        System.out.print("UploadAddress = " + response.getUploadAddress() + "\n");
        // 这个是 upload 的 token ,可以用于返回
        System.out.print("UploadAuth = " + response.getUploadAuth() + "\n");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }
    System.out.print("RequestId = " + response.getRequestId() + "\n");
}