通过阅读本文,您可以了解通过服务端SDK调用OpenAPI获取媒资上传地址和凭证的示例代码。

示例代码

您可以通过阿里云OpenAPI开发者门户在线调试。

package com.aliyun.ice.sample;

import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.*;
import com.aliyun.teaopenapi.models.Config;

public class Sample {

    public static void main() throws Exception {

        // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        // 本示例以将AccessKey ID和 AccessKey Secret保存在环境变量为例说明。配置方法请参见:https://help.aliyun.com/document_detail/378657.html
        com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
        Config config = new Config();
        config.setCredential(credentialClient);

        // 如需硬编码AccessKey ID和AccessKey Secret,代码如下,但强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        // config.accessKeyId = <yourAccessKeyId>;
        // config.accessKeySecret = <yourAccessKeySecret>;

        config.endpoint = "ice.cn-shanghai.aliyuncs.com";
        Client client = new Client(config);
        CreateUploadMediaRequest request = new CreateUploadMediaRequest()
            .setFileInfo("{\"Type\":\"video\",\"Name\":\"test\",\"Size\":123,\"Ext\":\"mp4\"}")
            .setMediaMetaData("{\"Title\":\"UploadTest\",\"Description\":\"UploadTest\",\"BusinessType\":\"general\"}")
            .setUploadTargetConfig("{\"StorageType\":\"oss\",\"StorageLocation\":\"out-****.oss-cn-shanghai.aliyuncs.com\"}");
        CreateUploadMediaResponse response = client.createUploadMedia(request);
        System.out.println("CreateUploadMedia, mediaId: " + response.getBody().getMediaId());
    }
}

相关接口

CreateUploadMedia - 获取媒资上传地址和凭证