|
|
|
@@ -17,12 +17,15 @@ import com.aliyun.oss.OSS; |
|
|
|
import com.aliyun.oss.OSSClient; |
|
|
|
import com.aliyun.oss.OSSClientBuilder; |
|
|
|
import com.aliyun.oss.model.PutObjectRequest; |
|
|
|
import com.aliyun.oss.model.PutObjectResult; |
|
|
|
import com.aliyuncs.DefaultAcsClient; |
|
|
|
import com.aliyuncs.profile.DefaultProfile; |
|
|
|
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest; |
|
|
|
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse; |
|
|
|
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest; |
|
|
|
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse; |
|
|
|
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest; |
|
|
|
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse; |
|
|
|
import com.iformall.video.aliyun.config.AliyunVideoConfig; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
@@ -85,7 +88,23 @@ public class AliyunVedioServer { |
|
|
|
return new OssUploadClient(bucket, endpoint, fileName, client); |
|
|
|
} |
|
|
|
|
|
|
|
public static void uploadVideo(CreateUploadVideoResponse response,String localFile,RedisTemplate<String, Object> redisTemplate) throws UnsupportedEncodingException { |
|
|
|
private static OssUploadClient initOssClient(RefreshUploadVideoResponse response) throws UnsupportedEncodingException { |
|
|
|
final Base64.Decoder decoder = Base64.getDecoder(); |
|
|
|
String uploadAddressStr = new String(decoder.decode(response.getUploadAddress().getBytes("UTF-8"))); |
|
|
|
String uploadAuthStr = new String(decoder.decode(response.getUploadAuth().getBytes("UTF-8"))); |
|
|
|
JSONObject uploadAuth = JSON.parseObject(uploadAuthStr); |
|
|
|
JSONObject uploadAddress = JSON.parseObject(uploadAddressStr); |
|
|
|
String bucket = uploadAddress.getString("Bucket"); |
|
|
|
String endpoint = uploadAddress.getString("Endpoint"); |
|
|
|
String fileName = uploadAddress.getString("FileName"); |
|
|
|
String accessKeyId = uploadAuth.getString("AccessKeyId"); |
|
|
|
String accessKeySecret = uploadAuth.getString("AccessKeySecret"); |
|
|
|
String securityToken = uploadAuth.getString("SecurityToken"); |
|
|
|
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
|
|
return new OssUploadClient(bucket, endpoint, fileName, client); |
|
|
|
} |
|
|
|
|
|
|
|
public static void uploadVideo(AliyunVideoConfig config,CreateUploadVideoResponse response,String localFile,RedisTemplate<String, Object> redisTemplate) throws UnsupportedEncodingException { |
|
|
|
OssUploadClient ossClient = initOssClient(response); |
|
|
|
// 创建PutObjectRequest对象。 |
|
|
|
// 填写Bucket名称、Object完整路径和本地文件的完整路径。Object完整路径中不能包含Bucket名称。 |
|
|
|
@@ -98,12 +117,43 @@ public class AliyunVedioServer { |
|
|
|
// metadata.setObjectAcl(CannedAccessControlList.Private); |
|
|
|
// putObjectRequest.setMetadata(metadata); |
|
|
|
// 上传文件。 |
|
|
|
ossClient.getOssClient().putObject(new PutObjectRequest(ossClient.getBucket(), ossClient.getFileName(), new File(localFile)). |
|
|
|
|
|
|
|
try { |
|
|
|
PutObjectResult result = ossClient.getOssClient().putObject(new PutObjectRequest(ossClient.getBucket(), localFile, new File(localFile)). |
|
|
|
<PutObjectRequest>withProgressListener(new PutObjectProgressListener(redisTemplate,response.getVideoId(),localFile))); |
|
|
|
}catch(Exception e) { |
|
|
|
String msg = e.getLocalizedMessage(); |
|
|
|
if (msg.contains("InvalidAccessKeyId")) { |
|
|
|
OssUploadClient ossClient1 = null; |
|
|
|
try { |
|
|
|
RefreshUploadVideoResponse refrshResoponse = refreshUploadVideo(config,response.getVideoId()); |
|
|
|
ossClient1 = initOssClient(refrshResoponse); |
|
|
|
ossClient1.getOssClient().putObject(new PutObjectRequest(ossClient1.getBucket(), localFile, new File(localFile)). |
|
|
|
<PutObjectRequest>withProgressListener(new PutObjectProgressListener(redisTemplate,refrshResoponse.getVideoId(),localFile))); |
|
|
|
} catch (Exception e1) { |
|
|
|
UploadCacheHelper.cacheError(redisTemplate, response.getVideoId()); |
|
|
|
log.error(" aliyun uploadVideo error."+ response.getVideoId(),e); |
|
|
|
} |
|
|
|
if (null != ossClient1) { |
|
|
|
ossClient1.getOssClient().shutdown(); |
|
|
|
} |
|
|
|
}else { |
|
|
|
UploadCacheHelper.cacheError(redisTemplate, response.getVideoId()); |
|
|
|
log.error(" aliyun uploadVideo error."+ response.getVideoId(),e); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// 关闭OSSClient。 |
|
|
|
ossClient.getOssClient().shutdown(); |
|
|
|
} |
|
|
|
|
|
|
|
public static RefreshUploadVideoResponse refreshUploadVideo(AliyunVideoConfig config,String videoId) throws Exception { |
|
|
|
RefreshUploadVideoRequest request = new RefreshUploadVideoRequest(); |
|
|
|
request.setVideoId(videoId); |
|
|
|
DefaultAcsClient client = initVodClient(config); |
|
|
|
return client.getAcsResponse(request); |
|
|
|
} |
|
|
|
|
|
|
|
//获取视频播放地址 |
|
|
|
//https://help.aliyun.com/document_detail/61064.html?spm=a2c4g.11186623.6.1043.7c2d3838K6wQPZ |
|
|
|
public static String getVedioUrl(AliyunVideoConfig config,String videoId) throws ClientException { |
|
|
|
|