调用阿里云SDK进行视频上传<代码实现>

前置条件:
1.有阿里云账号
2.开通视频点播服务
3.有已经备案的域名
4.然后配置域名等
5.手动导入aliyun-sdk-vod-upload的jar包到本地仓库

文件结构:
resource
-- application.properties
util
-- AliyunVODSDKUtils
-- ConstantPropertiesUtil
service
-- VideoService
-- VideoServiceImpl
controller
-- VideoAdminController




1.application.properties

# 服务端口
server.port=8160
# 服务名
spring.application.name=guli-vod

# 环境设置:dev、test、prod
spring.profiles.active=dev

#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=
aliyun.vod.file.keysecret=

# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB

2.AliyunVODSDKUtils 文件上传工具类

public class AliyunVODSDKUtils {

    /**
     * 初始化vod客户端调用对象
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws 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;
    }
}

3.ConstantPropertiesUtil 静态常量类

@Component
public class ConstantPropertiesUtil implements InitializingBean {

    @Value("${aliyun.vod.file.keyid}")
    private String keyId;

    @Value("${aliyun.vod.file.keysecret}")
    private String keySecret;

    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;

    @Override
    public void afterPropertiesSet() throws Exception {
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
    }
}

4.VideoService

public interface VideoService {
    /**
     *调用阿里云sdk执行视频上传
     * @param file
     * @return 视频的id
     */
    String uploadVideo(MultipartFile file);
}

5.VideoServiceImpl

@Service
public class VideoServiceImpl implements VideoService {
    @Override
    public String uploadVideo(MultipartFile file) {

        String videoId = null;
        try {
            InputStream inputStream = file.getInputStream();
            // 获得上传文件名
            String orgFilename = file.getOriginalFilename();
            // 去掉扩展名后的title
            String title = orgFilename.substring(0, orgFilename.lastIndexOf("."));

            // 1.创建请求对象
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET,
                    title,
                    orgFilename,
                    inputStream
            );

            // 2. 创建文件上传器
            UploadVideoImpl uploader = new UploadVideoImpl();

            // 3. 执行文件上传
            UploadStreamResponse uploadStreamResponse = uploader.uploadStream(request);

            // 4.获取videoId
            videoId = uploadStreamResponse.getVideoId();

        } catch (IOException e) {
            e.printStackTrace();
            throw new GuLiException(ResultSetEnum.VIDEO_UPLOAD_ERROR);
        }
        return videoId;
    }
}

6.VideoAdminController

@RestController
@CrossOrigin
@Api(description = "阿里云视频管理")
@RequestMapping("/admin/vod/video")
public class VideoAdminController {

    @Autowired
    private VideoService videoService;

    @PostMapping("/upload")
    public ResultSet videoUpload(
            @ApiParam(name = "file",value = "文件",required = true)
            @RequestParam("file")MultipartFile file
            ){

             String videoId = videoService.uploadVideo(file);
             return ResultSet.ok().message("视频上传成功").data("videoId",videoId);
    }
}

你可能感兴趣的:(调用阿里云SDK进行视频上传<代码实现>)