You need to enable JavaScript to run this app.
导航

媒资上传

最近更新时间2023.10.13 17:01:59

首次发布时间2021.02.23 10:42:40

本文为您提供了服务端 Java SDK 的媒资上传模块相关的 API 调用示例。主要包含:媒资上传、素材上传、URL 批量拉取上传、查询 URL 上传任务状态等。

注意事项

  • 媒资上传模块的获取上传地址和凭证确认上传的 OpenAPI,目前支持 2 个版本。

    • OpenAPI 的版本号为 2022-01-01

      说明

      上传文件时,要求携带文件后缀。例如,mp4 文件上传,携带 .mp4 或 .MP4。

    • OpenAPI 的版本号为 2020-08-01

  • 异步上传成功是指提交任务成功,并不代表媒资上传任务执行成功。

  • 媒资上传支持进度条功能,需要满足的条件:

    • 将 Java SDK 升级为大于等于 v1.0.97 的版本。
    • 添加相应 Listener,如无需求,传 null 值即可。

初始化

使用前请先完成初始化,参考初始化

签发 UploadAuthToken

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个UploadAuthToken,您可以循环调用生成方法。UploadAuthToken用于客户端上传,详见客户端上传

package com.volcengine.example.vod.upload;

import com.alibaba.fastjson.JSON;
import com.volcengine.model.sts2.SecurityToken2;
import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.util.Time;


public class GetUploadSts2Demo {

    public static void main(String[] args) throws Exception {
        IVodService vodService = VodServiceImpl.getInstance();

        // call below method if you dont set ak and sk in ~/.vcloud/config
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        SecurityToken2 sts2WithExpire = vodService.getUploadSts2WithExpire(Time.Hour);
        System.out.println(JSON.toJSONString(sts2WithExpire));


        SecurityToken2 sts2 = vodService.getUploadSts2WithExpire(Time.Hour);
        System.out.println(JSON.toJSONString(sts2));

    }
}

媒资上传

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

package com.volcengine.example.vod.upload;

import com.alibaba.fastjson.JSON;
import com.volcengine.helper.VodUploadMediaProcessListener;
import com.volcengine.helper.VodUploadProgressListener;
import com.volcengine.model.beans.Functions;
import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.service.vod.model.request.VodUploadMediaRequest;
import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse;

import java.util.ArrayList;
import java.util.List;

public class VodUploadMediaDemo {

    public static void main(String[] args) {
        IVodService vodService = VodServiceImpl.getInstance();

        // call below method if you dont set ak and sk in ~/.vcloud/config
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        String space = "your space name";
        String filePath = "your file path";

        List<Functions> functionsList = new ArrayList<>();
        Functions getMetaFunc = Functions.GetMetaFunction();
        functionsList.add(getMetaFunc);

        Functions snapShotFunc = Functions.SnapShotFunction(2.3);
        functionsList.add(snapShotFunc);

        VodUploadMediaRequest vodUploadMediaRequest = VodUploadMediaRequest.newBuilder()
                .setSpaceName(space)
                .setFilePath(filePath)
                .setFileName("hello/vod/video.mp4")
                .setFunctions(JSON.toJSONString(functionsList))
                .setStorageClass(0)
  //            .setUploadStrategy(1)  低内存模式,对内存需求较高进行设置,传值为 1。无内存需求不需要特别设置  
                .build();
        // 需要进度条功能时添加相应 listener,如无需求,传 null 值即可
        VodUploadProgressListener listener = new VodUploadMediaProcessListener();

        try {
            VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMedia(vodUploadMediaRequest, listener);
            if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) {
                System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError());
                System.exit(-1);
            }
            System.out.println(vodCommitUploadInfoResponse.toString());
            System.out.println(vodCommitUploadInfoResponse.getResult().getData().getVid());
            System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

        

素材上传

接口请求参数和返回参数详见OpenAPI:获取上传地址和凭证确认上传

package com.volcengine.example.vod.upload;

import com.alibaba.fastjson.JSON;
import com.volcengine.service.vod.Const;
import com.volcengine.model.beans.Functions;
import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.service.vod.model.request.VodUploadMaterialRequest;
import com.volcengine.service.vod.model.response.VodCommitUploadInfoResponse;

import java.util.ArrayList;
import java.util.List;

public class VodUploadMaterialDemo {

    public static void main(String[] args) {
        IVodService vodService = VodServiceImpl.getInstance();

        // call below method if you dont set ak and sk in ~/.vcloud/config
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        String space = "your space name";
        String filePath = "your file path";

        List<Functions> functionsList = new ArrayList<>();
        Functions getMetaFunc = Functions.GetMetaFunction();
        functionsList.add(getMetaFunc);

        Functions snapShotFunc = Functions.SnapShotFunction(2.3);
        functionsList.add(snapShotFunc);

        Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试视频", "test", "素材测试,视频文件", Const.CategoryVideo, "mp4");
        functionsList.add(addOptionInfo);

        VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder()
                .setSpaceName(space)
                .setFilePath(filePath)
                .setFileType(Const.FileTypeMedia)
                .setFunctions(JSON.toJSONString(functionsList))
                .build();

        try {
            VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest, null);
            if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) {
                System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError());
                System.exit(-1);
            }
            System.out.println(vodCommitUploadInfoResponse.toString());
            System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid());
            System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

////  图片素材上传
//    public static void main(String[] args) {
//        IVodService vodService = VodServiceImpl.getInstance();
//
//        // call below method if you dont set ak and sk in ~/.vcloud/config
//        vodService.setAccessKey("your ak");
//        vodService.setSecretKey("your sk");
//
//        String space = "your space name";
//        String filePath = "your file path";
//
//        List<Functions> functionsList = new ArrayList<>();
//        Functions getMetaFunc = Functions.GetMetaFunction();
//        functionsList.add(getMetaFunc);
//
//        Functions snapShotFunc = Functions.SnapShotFunction(2.3);
//        functionsList.add(snapShotFunc);
//
//        Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试图片", "test", "素材测试,图片文件", Const.CategoryImage, "jpg");
//        functionsList.add(addOptionInfo);
//
//        VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder()
//                .setSpaceName(space)
//                .setFilePath(filePath)
//                .setFileType(Const.FileTypeImage)
//                .setFileName("hello/vod/image.jpg")
//                .setFunctions(JSON.toJSONString(functionsList))
//                .build();
//
//        try {
//            VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest,null);
//            if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) {
//                System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError());
//                System.exit(-1);
//            }
//            System.out.println(vodCommitUploadInfoResponse.toString());
//            System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid());
//            System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

////    字幕素材上传
//    public static void main(String[] args) {
//        IVodService vodService = VodServiceImpl.getInstance();
//
//        // call below method if you dont set ak and sk in ~/.vcloud/config
//        vodService.setAccessKey("your ak");
//        vodService.setSecretKey("your sk");
//
//        String space = "your space name";
//        String filePath = "your file path";
//
//        List<Functions> functionsList = new ArrayList<>();
//        Functions getMetaFunc = Functions.GetMetaFunction();
//        functionsList.add(getMetaFunc);
//
//        Functions snapShotFunc = Functions.SnapShotFunction(2.3);
//        functionsList.add(snapShotFunc);
//
//        Functions addOptionInfo = Functions.AddOptionInfoFunction("素材测试字幕", "test", "素材测试,字幕文件", Const.CategorySubtitle, "vtt");
//        functionsList.add(addOptionInfo);
//
//        VodUploadMaterialRequest vodUploadMaterialRequest = VodUploadMaterialRequest.newBuilder()
//                .setSpaceName(space)
//                .setFilePath(filePath)
//                .setFileType(Const.FileTypeObject)
//                .setFileName("hello/vod/object.vtt")
//                .setFunctions(JSON.toJSONString(functionsList))
//                .build();
//
//        try {
//            VodCommitUploadInfoResponse vodCommitUploadInfoResponse = vodService.uploadMaterial(vodUploadMaterialRequest,null);
//            if (vodCommitUploadInfoResponse.getResponseMetadata().hasError()) {
//                System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getError());
//                System.exit(-1);
//            }
//            System.out.println(vodCommitUploadInfoResponse.toString());
//            System.out.println(vodCommitUploadInfoResponse.getResult().getData().getMid());
//            System.out.println(vodCommitUploadInfoResponse.getResponseMetadata().getRequestId());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}

        

URL 批量拉取上传

接口请求参数和返回参数详见 OpenAPI:URL批量拉取上传

package com.volcengine.example.vod.upload;

import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;

import java.util.HashMap;
import java.util.Map;

public class VodUploadMediaByUrlDemo {

    public static void main(String[] args) throws Exception {
        IVodService vodService = VodServiceImpl.getInstance();
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        try {
            com.volcengine.service.vod.model.request.VodUrlUploadRequest.Builder reqBuilder = com.volcengine.service.vod.model.request.VodUrlUploadRequest.newBuilder();
			reqBuilder.setSpaceName("your space");
			com.volcengine.service.vod.model.business.VodUrlUploadURLSet.Builder uRLSetsBuilder = com.volcengine.service.vod.model.business.VodUrlUploadURLSet.newBuilder();
			uRLSetsBuilder.setSourceUrl("");
            uRLSetsBuilder.setStorageClass(0);
            uRLSetsBuilder.setFileExtension(".mp4");
            uRLSetsBuilder.setCallbackArgs("my java callback args");
            Map<String, String> customUrlHeaders = new HashMap<>();
            customUrlHeaders.put("your header key", "your header value");
            uRLSetsBuilder.putAllCustomURLHeaders(customUrlHeaders);
            reqBuilder.addURLSets(uRLSetsBuilder);

            com.volcengine.service.vod.model.response.VodUrlUploadResponse resp = vodService.uploadMediaByUrl(reqBuilder.build());
            if (resp.getResponseMetadata().hasError()) {
                System.out.println(resp.getResponseMetadata().getError());
                System.exit(-1);
            }
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

查询 URL 批量上传任务状态

接口请求参数和返回参数详见 OpenAPI:查询 URL 批量上传任务状态

package com.volcengine.example.vod.upload;

import com.volcengine.service.vod.IVodService;
import com.volcengine.service.vod.impl.VodServiceImpl;
import com.volcengine.service.vod.model.request.VodQueryUploadTaskInfoRequest;
import com.volcengine.service.vod.model.response.VodQueryUploadTaskInfoResponse;

import java.util.Arrays;
import java.util.List;

public class VodQueryUploadTaskInfoDemo {

    public static void main(String[] args) {
        IVodService vodService = VodServiceImpl.getInstance();

        // call below method if you dont set ak and sk in ~/.vcloud/config
        vodService.setAccessKey("your ak");
        vodService.setSecretKey("your sk");

        String jobId = "url jobId";
        List<String> jobIds = Arrays.asList(jobId);

        VodQueryUploadTaskInfoRequest vodQueryUploadTaskInfoRequest = VodQueryUploadTaskInfoRequest.newBuilder()
                .setJobIds(String.join(",", jobIds))
                .build();

        try {
            VodQueryUploadTaskInfoResponse vodQueryUploadTaskInfoResponse = vodService.queryUploadTaskInfo(vodQueryUploadTaskInfoRequest);
            if (vodQueryUploadTaskInfoResponse.getResponseMetadata().hasError()) {
                System.out.println(vodQueryUploadTaskInfoResponse.getResponseMetadata().getError());
                System.exit(-1);
            }
            System.out.println(vodQueryUploadTaskInfoResponse.toString());
            System.out.println(vodQueryUploadTaskInfoResponse.getResult().getData().getMediaInfoList(0).getState());
            System.out.println(vodQueryUploadTaskInfoResponse.getResponseMetadata().getRequestId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

上传进度条

进度条功能用于展示点播媒资上传的进度,您可根据自身业务需求使用进度条功能。进度条功能采用观察者模式,您可以通过实现 VodUploadProgressListener 接口中的 progressChanged() 方法集成相应的业务逻辑。

说明

支持的版本号,需要大于 v1.0.97。

事件源支持的 EventType

EventType 名称含义

FILE_SIZE_EVENT

文件大小事件通知,获得文件大小时会触发该事件,通知文件大小。

UPLOAD_BYTES_EVENT

传输事件通知,每个分片传输完成时会触发该事件,通知写入的分片字节数。

示例代码如下所示。

package com.volcengine.helper;

// 使用进度条,需要实现 VodUploadProgressListener 方法
public class VodUploadMediaProcessListener implements VodUploadProgressListener {
    /**
     * 已成功上传总字节数
     */
    private long bytesUploaded = 0;
    /**
     * 原文件大小
     */
    private long fileSize = -1;

   // 使用进度条需要您重写回调方法,以下为参考示例
    @Override
    public void progressChanged(VodUploadProgressEvent progressEvent) {
        long bytes = progressEvent.getByteSize();
        VodUploadProgressEventType eventType = progressEvent.getEventType();
        switch (eventType) {
            // 文件大小事件通知,获得文件大小时会触发该事件,通知文件大小
            case FILE_SIZE_EVENT:
                this.fileSize = bytes;
                // business logic ....
                System.out.println("file size is " + this.fileSize);
                break;
            // 传输事件通知,每个分片传输完成时会触发该事件,通知写入的分片字节数
            case UPLOAD_BYTES_EVENT:
                this.bytesUploaded += bytes;
                if (this.bytesUploaded != this.fileSize - 1) {
                    int percent = (int) (this.bytesUploaded * 100.0 / this.fileSize);
                    System.out.println("uploaded " + percent + "%");
                } else {
                    int percent = 99;
                    System.out.println("uploaded " + percent + "%");
                }
                // business logic ....
                break;
            default:
                break;
        }
    }
}