首先先说下大的环境:
1、项目已经成型,项目里面已经存在比较旧的OSS的jar包的版本。
2、项目一直是通过后台上传视频到视频点播的,不是通过前端获取凭证上传的。
然后说下我要处理的事情:
1、从OSS上迁移数据到视频点播上
这个好处理,因为视频不大,下载视频,重新上传到视频点播上。
然后到出VID列表的csv文件,构建中间表,导入数据库。然后使用关联Update进行修改即可。
2、完成播放鉴权,这是老项目存在的jar包
com.aliyun
aliyun-java-sdk-core
3.3.1
com.aliyun
aliyun-java-sdk-dysmsapi
1.0.0
com.aliyun.oss
aliyun-sdk-oss
2.8.2
com.aliyun
aliyun-java-sdk-vod
2.9.0
构建代码授权代码:
public static String getVideoPlayAuth(String vid) {
DefaultProfile profile = DefaultProfile.getProfile(REGOIN, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
request.setVideoId(vid);
GetVideoPlayAuthResponse response = null;
try {
response = client.getAcsResponse(request);
} catch (ServerException e) {
throw new RuntimeException("GetVideoPlayAuthRequest Server failed");
} catch (ClientException e) {
throw new RuntimeException("GetVideoPlayAuthRequest Client failed");
}
try {
return response.getPlayAuth(); // 播放凭证
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}
这个也很好处理,代码还是比较简单的。
3、变更上传,这里就是坑的地方来了
首先先,吐槽下官方的文档,下面是官方的文档
如果照着这个POM修改的话,你就回发现,再去看下面的demo的时候就懵逼了,UploadVideoRequest到哪去了。
没办法下载SDK,查看lib包,发现是不是有什么包少导入了。发现目标:
瞬间就很高兴啊,然后准备修改pom文件引入,靠,这个jar包不是开源的。没办法,发到私服上去。update我们项目好,这个时候jar包有了,UploadVideoRequest有,这下总应该行了吧。咔咔的拿着文档一顿该,我靠,启动上传,报错setCatId的造型异常。
com.aliyun
aliyun-java-vod-upload
1.4.11
这就不得不说阿里的开发人员坑爹了,一个分类的ID,低版本是Integer,高版本是Long
aliyun-java-sdk-vod
升级呗,吧这个jar包升到2.15.2。还好没有引起老的代码异常。继续运行,靠。告诉我setSysCount这个方法不存在。顿时心情就不好了,这还能好好的玩吗。不管了降版本。
com.aliyun
aliyun-java-vod-upload
1.3.2
降低到1.3.2,运行上传,高兴啊,没有报方法不存在了。就是告诉我下面这个类不存在。
com.aliyun.oss.common.utils.StringUtils
靠,OSS里面的类,没存在,先升了版本再说。
com.aliyun.oss
aliyun-sdk-oss
3.1.0
好了,这下类要找到了,舒服没一会,完犊子了,以前别人写的代码都不好使了。我靠,这还能玩,赶紧麻利的吧版本降回来
com.aliyun.oss
aliyun-sdk-oss
2.8.2
这个时候我都有点干阿里开发的人的冲动了,没办法继续降版本
com.aliyun
aliyun-java-vod-upload
1.1.1
靠,这个更坑,尼玛发现UploadStreamRequest呢,UploadFIleStreamRequest不能被upload方法调用,文档提供的方法完全不好使了,好吧UploadVideoRequest就这么一根独苗了。试试吧。
UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
request.setCateId(1000066766);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadVideoResponse response = uploader.uploadVideo(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n");
} else {
/* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
System.out.print("VideoId=" + response.getVideoId() + "\n");
System.out.print("ErrorCode=" + response.getCode() + "\n");
System.out.print("ErrorMessage=" + response.getMessage() + "\n");
}
return response.getVideoId();
别说,还真能行,关键别人只支持指定文件路径上传,那俺们怎么跟前端对接啊。怎么无缝集成老的代码,pass,重新选择方案。这下,我不去找别的版本了。心累啊!重新回到1.3.2的版本
com.aliyun
aliyun-java-vod-upload
1.3.2
不就是没有:
com.aliyun.oss.common.utils.StringUtils
这个工具类嘛,先把oss发布到3.1.0的版本,打开源码copy出来。在项目里面新建一个pakeage,吧copy出去的代码粘贴进去,同时OSS的版本改回2.8.2。没有我们就自己建嘛,
源码如下:
package com.aliyun.oss.common.utils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.text.Collator;
import java.util.Locale;
/**
* 解决版本冲突,截取OSS3.1.0版本的源码
* Utilities for converting objects to strings.
*/
public class StringUtils {
private static final String DEFAULT_ENCODING = "UTF-8";
public static final String COMMA_SEPARATOR = ",";
public static final Charset UTF8 = Charset.forName(DEFAULT_ENCODING);
private static final Locale LOCALE_ENGLISH = Locale.ENGLISH;
// white space character that match Pattern.compile("\\s")
private static final char CHAR_SPACE = ' ';
private static final char CHAR_TAB = '\t';
private static final char CHAR_NEW_LINE = '\n';
private static final char CHAR_VERTICAL_TAB = '\u000b';
private static final char CHAR_CARRIAGE_RETURN = '\r';
private static final char CHAR_FORM_FEED = '\f';
public static Integer toInteger(StringBuilder value) {
return Integer.parseInt(value.toString());
}
public static String toString(StringBuilder value) {
return value.toString();
}
public static Boolean toBoolean(StringBuilder value) {
return Boolean.getBoolean(value.toString());
}
public static String fromInteger(Integer value) {
return Integer.toString(value);
}
public static String fromLong(Long value) {
return Long.toString(value);
}
public static String fromString(String value) {
return value;
}
public static String fromBoolean(Boolean value) {
return Boolean.toString(value);
}
public static String fromBigInteger(BigInteger value) {
return value.toString();
}
public static String fromBigDecimal(BigDecimal value) {
return value.toString();
}
public static BigInteger toBigInteger(String s) {
return new BigInteger(s);
}
public static BigDecimal toBigDecimal(String s) {
return new BigDecimal(s);
}
public static String fromFloat(Float value) {
return Float.toString(value);
}
/**
* Returns the string representation of the specified double.
*
* @param d
* The double to represent as a string.
*
* @return The string representation of the specified double.
*/
public static String fromDouble(Double d) {
return Double.toString(d);
}
/**
* Returns the string representation of the specified Byte.
*
* @param b
* The Byte to represent as a string.
*
* @return The string representation of the specified Byte.
*/
public static String fromByte(Byte b) {
return Byte.toString(b);
}
public static String replace( String originalString, String partToMatch, String replacement ) {
StringBuilder buffer = new StringBuilder( originalString.length() );
buffer.append( originalString );
int indexOf = buffer.indexOf( partToMatch );
while (indexOf != -1) {
buffer = buffer.replace(indexOf, indexOf + partToMatch.length(), replacement);
indexOf = buffer.indexOf(partToMatch, indexOf + replacement.length());
}
return buffer.toString();
}
/**
* Joins the strings in parts with joiner between each string
* @param joiner the string to insert between the strings in parts
* @param parts the parts to join
*/
public static String join(String joiner, String... parts) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
builder.append(parts[i]);
if (i < parts.length - 1) {
builder.append(joiner);
}
}
return builder.toString();
}
/**
* A null-safe trim method. If the input string is null, returns null;
* otherwise returns a trimmed version of the input.
*/
public static String trim(String value) {
if (value == null) {
return null;
}
return value.trim();
}
/**
* @return true if the given value is either null or the empty string
*/
public static boolean isNullOrEmpty(String value) {
return value == null || value.isEmpty();
}
/**
* @return true if the given value is non-null and non-empty
*/
public static boolean hasValue(String str) {
return !isNullOrEmpty(str);
}
/**
* Converts a given String to lower case with Locale.ENGLISH
*
* @param str the string to be converted to lower case
* @return the lower case of string, or itself if string is null/empty
*/
public static String lowerCase(String str) {
if(isNullOrEmpty(str)) {
return str;
}
return str.toLowerCase(LOCALE_ENGLISH);
}
/**
* Converts a given String to upper case with Locale.ENGLISH
*
* @param str the string to be converted to upper case
* @return the upper case of string, or itself if string is null/empty
*/
public static String upperCase(String str) {
if(isNullOrEmpty(str)) {
return str;
}
return str.toUpperCase(LOCALE_ENGLISH);
}
/**
* Compare two strings with Locale.ENGLISH
* This method is preferred over String.compareTo() method.
* @param str1 String 1
* @param str2 String 2
* @return negative integer if str1 lexicographically precedes str2
* positive integer if str1 lexicographically follows str2
* 0 if both strings are equal
* @throws IllegalArgumentException throws exception if both or either of the strings is null
*/
public static int compare(String str1, String str2) {
if( str1 == null || str2 == null) {
throw new IllegalArgumentException("Arguments cannot be null");
}
Collator collator = Collator.getInstance(LOCALE_ENGLISH);
return collator.compare(str1, str2);
}
/**
* Tests a char to see if is it whitespace.
* This method considers the same characters to be white
* space as the Pattern class does when matching \s
*
* @param ch the character to be tested
* @return true if the character is white space, false otherwise.
*/
private static boolean isWhiteSpace(final char ch) {
if (ch == CHAR_SPACE) return true;
if (ch == CHAR_TAB) return true;
if (ch == CHAR_NEW_LINE) return true;
if (ch == CHAR_VERTICAL_TAB) return true;
if (ch == CHAR_CARRIAGE_RETURN) return true;
if (ch == CHAR_FORM_FEED) return true;
return false;
}
/**
* This method appends a string to a string builder and collapses contiguous
* white space is a single space.
*
* This is equivalent to:
* destination.append(source.replaceAll("\\s+", " "))
* but does not create a Pattern object that needs to compile the match
* string; it also prevents us from having to make a Matcher object as well.
*
*/
public static void appendCompactedString(final StringBuilder destination, final String source) {
boolean previousIsWhiteSpace = false;
int length = source.length();
for (int i = 0; i < length; i++) {
char ch = source.charAt(i);
if (isWhiteSpace(ch)) {
if (previousIsWhiteSpace) {
continue;
}
destination.append(CHAR_SPACE);
previousIsWhiteSpace = true;
} else {
destination.append(ch);
previousIsWhiteSpace = false;
}
}
}
/**
* Performs a case insensitive comparison and returns true if the data
* begins with the given sequence.
*/
public static boolean beginsWithIgnoreCase(final String data, final String seq) {
return data.regionMatches(true, 0, seq, 0, seq.length());
}
}
重新更改流方式上传代码:
/**
* 上传视频
* @param title
* @param file
* @return
*/
public static String uploadVideoByStream(String title,String fileName, MultipartFile file) {
UploadStreamRequest request = null;
try {
request = new UploadStreamRequest(accessKeyId, accessKeySecret, title,fileName, file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
request.setCateId(1000066766L);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
System.out.print("RequestId=" + response.getRequestId() + "\n"); //请求视频点播服务的请求ID
if (response.isSuccess()) {
System.out.print("VideoId=" + response.getVideoId() + "\n");
} else {
/* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
System.out.print("VideoId=" + response.getVideoId() + "\n");
System.out.print("ErrorCode=" + response.getCode() + "\n");
System.out.print("ErrorMessage=" + response.getMessage() + "\n");
}
return response.getVideoId();
}
启动上传,奶奶个锤子的总算行了。大半天的时间就搞了个这样的事情。蛋都疼了