全部产品
云市场

媒体上传

更新时间:2019-06-05 17:19:34

本文档为PHP版获取上传地址和凭证、注册媒资信息等服务端API的调用示例,要实现完整的媒体文件上传,可配合 客户端上传,也可以直接使用 服务端PHP上传SDK

初始化客户端

使用前请先初始化客户端,参考 initVodClient 接口。

获取视频上传地址和凭证

接口参数和返回字段请参考 CreateUploadVideo

  1. /**
  2. * 获取视频上传地址和凭证
  3. * @param client 发送请求客户端
  4. * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
  5. */
  6. function createUploadVideo($client) {
  7. $request = new vod\CreateUploadVideoRequest();
  8. $request->setTitle("Sample Title");
  9. $request->setFileName("videoFile.mov");
  10. $request->setDescription("Video Description");
  11. $request->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png");
  12. $request->setTags("tag1,tag2");
  13. $request->setAcceptFormat('JSON');
  14. return $client->getAcsResponse($request);
  15. }
  16. try {
  17. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  18. $uploadInfo = createUploadVideo($client);
  19. var_dump($uploadInfo);
  20. } catch (Exception $e) {
  21. print $e->getMessage()."\n";
  22. }

刷新视频上传凭证

接口参数和返回字段请参考 RefreshUploadVideo

  1. /**
  2. * 刷新视频上传凭证
  3. * @param client 发送请求客户端
  4. * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据
  5. */
  6. function refreshUploadVideo($client, $videoId) {
  7. $request = new vod\RefreshUploadVideoRequest();
  8. $request->setVideoId($videoId);
  9. $request->setAcceptFormat('JSON');
  10. return $client->getAcsResponse($request);
  11. }
  12. try {
  13. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  14. $refreshInfo = refreshUploadVideo($client, 'videoId');
  15. var_dump($refreshInfo);
  16. } catch (Exception $e) {
  17. print $e->getMessage()."\n";
  18. }

获取图片上传地址和凭证

接口参数和返回字段请参考 CreateUploadImage

  1. /**
  2. * 获取图片上传地址和凭证
  3. * @param client 发送请求客户端
  4. * @return CreateUploadImageResponse 获取图片上传地址和凭证响应数据
  5. */
  6. function createUploadImage($client, $imageType, $imageExt) {
  7. $request = new vod\CreateUploadImageRequest();
  8. $request->setImageType($imageType);
  9. $request->setImageExt($imageExt);
  10. $request->setAcceptFormat('JSON');
  11. return $client->getAcsResponse($request);
  12. }
  13. try {
  14. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  15. $imageInfo = createUploadImage($client, 'cover', 'jpg');
  16. var_dump($imageInfo);
  17. } catch (Exception $e) {
  18. print $e->getMessage()."\n";
  19. }

获取辅助媒资上传地址和凭证

接口参数和返回字段请参考 CreateUploadAttachedMedia

  1. /**
  2. * 获取辅助媒资(水印、字幕等)上传地址和凭证
  3. * @param client 发送请求客户端
  4. * @return CreateUploadAttachedMediaResponse 获取辅助媒资上传地址和凭证响应数据
  5. */
  6. function createUploadAttachedMedia($client) {
  7. $request = new vod\CreateUploadAttachedMediaRequest();
  8. $request->setBusinessType("watermark");
  9. $request->setMediaExt("gif");
  10. $request->setTitle("this is a sample");
  11. return $client->getAcsResponse($request);
  12. }
  13. try {
  14. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  15. $result = createUploadAttachedMedia($client);
  16. var_dump($result);
  17. } catch (Exception $e) {
  18. print $e->getMessage()."\n";
  19. }

URL批量拉取上传

接口参数和返回字段请参考 UploadMediaByURL

  1. /**
  2. * URL批量拉取上传
  3. * @param client 发送请求客户端
  4. * @return UploadMediaByURLResponse URL批量拉取上传响应数据
  5. */
  6. function uploadMediaByURL($client) {
  7. $request = new vod\UploadMediaByURLRequest();
  8. $url = "http://test.xxx.com/xxxx.mp4";
  9. $request->setUploadURLs($url);
  10. $uploadMetadataList = array();
  11. $uploadMetadata = array();
  12. $uploadMetadata["SourceUrl"] = $url;
  13. $uploadMetadata["Title"] = "upload by url sample";
  14. $uploadMetadataList[] = $uploadMetadata;
  15. $request->setUploadMetadatas(json_encode($uploadMetadataList));
  16. return $client->getAcsResponse($request);
  17. }
  18. try {
  19. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  20. $result = uploadMediaByURL($client);
  21. var_dump($result);
  22. } catch (Exception $e) {
  23. print $e->getMessage()."\n";
  24. }

注册媒资信息

接口参数和返回字段请参考 RegisterMedia

  1. /**
  2. * 注册媒资信息
  3. * @param client 发送请求客户端
  4. * @return RegisterMediaResponse 注册媒资信息响应数据
  5. */
  6. function registerMedia($client) {
  7. $request = new vod\RegisterMediaRequest();
  8. $metaDataArray= array();
  9. $metaData= array();
  10. $metaData["Title"] = "registerMedia by url sample";
  11. $metaData["FileURL"] = "https://xxxxxx.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4";
  12. $metaDataArray[] = $metaData;
  13. $request->setRegisterMetadatas(json_encode($metaDataArray));
  14. return $client->getAcsResponse($request);
  15. }
  16. try {
  17. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  18. $result = registerMedia($client);
  19. var_dump($result);
  20. } catch (Exception $e) {
  21. print $e->getMessage()."\n";
  22. }

获取URL上传信息

接口参数和返回字段请参考 GetURLUploadInfos

  1. /**
  2. * 获取URL上传信息
  3. * @param client 发送请求客户端
  4. * @return GetURLUploadInfosResponse 获取URL上传信息响应数据
  5. */
  6. function getURLUploadInfos($client) {
  7. $request = new vod\GetURLUploadInfosRequest();
  8. // URL列表
  9. $urls = array();
  10. $urls[] = "http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4";
  11. $urls[] = "http://xxx.cn-shanghai.aliyuncs.com/sample2.mp4";
  12. // 对URL进行编码
  13. $uploadURLs = array();
  14. foreach ($urls as $url) {
  15. $uploadURLs[] = urlencode($url);
  16. }
  17. // 设置上传的URL列表,用逗号分隔
  18. $request->setUploadURLs(implode(",", $uploadURLs));
  19. // 也可以传入jobId查询
  20. //$request->setJobIds("jobId1xxxxxx,jobId2xxxxxx")
  21. return $client->getAcsResponse($request);
  22. }
  23. try {
  24. $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
  25. $result = getURLUploadInfos($client);
  26. var_dump($result);
  27. } catch (Exception $e) {
  28. print $e->getMessage()."\n";
  29. }