|
@@ -29,7 +29,8 @@ import com.stuuudy.commons.util.exception.CommonsException;
|
|
|
import com.stuuudy.commons.util.exception.EnumErrorException;
|
|
|
import com.tzld.piaoquan.api.common.enums.EnumUploadFileType;
|
|
|
import com.tzld.piaoquan.api.config.AliOssConfig;
|
|
|
-import com.tzld.piaoquan.api.model.vo.contentplatform.SignatureVO;
|
|
|
+import com.tzld.piaoquan.api.model.vo.SignatureVO;
|
|
|
+import com.tzld.piaoquan.api.model.vo.StsTokenVO;
|
|
|
import com.tzld.piaoquan.growth.common.utils.DateUtils;
|
|
|
import com.tzld.piaoquan.growth.common.utils.RandomUtil;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
@@ -1140,7 +1141,7 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
// Date expiration = new Date(new Date().getTime() +( 60) * 1000);
|
|
|
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
|
|
|
|
|
|
- URL url = getOssReadOnlyClient().generatePresignedUrl(PropertiesUtils.getVideoBucket(),
|
|
|
+ URL url = getOssReadOnlyClient().generatePresignedUrl(BasePropertiesUtils.getVideoBucket(),
|
|
|
filePath, expiration, HttpMethod.GET);
|
|
|
String path = url.getFile();
|
|
|
if (StringUtils.startsWith(path, "/")) {
|
|
@@ -1152,7 +1153,7 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
|
|
|
public static String saveObjectFromInternal(InputStream inputStream, String fullKey)
|
|
|
throws OSSException, ClientException, IOException {
|
|
|
- String bucketName = PropertiesUtils.getVideoBucket();
|
|
|
+ String bucketName = BasePropertiesUtils.getVideoBucket();
|
|
|
getInternalOssClient().putObject(bucketName, fullKey, inputStream);
|
|
|
return fullKey;
|
|
|
}
|
|
@@ -1219,7 +1220,7 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
try {
|
|
|
ossInternalClient.restoreObject(bucket, srckey);
|
|
|
} catch (Exception e) {
|
|
|
- // TODO Auto-generated catch blockvideo-common/src/main/java/com/weiqu/video/common/enums/ExceptionCodeEnum.java
|
|
|
+ // TODO OSS Auto-generated catch blockvideo-common/src/main/java/com/weiqu/video/common/enums/ExceptionCodeEnum.java
|
|
|
// e.printStackTrace();
|
|
|
logger.error("视频可能在解冻中");
|
|
|
}
|
|
@@ -1241,7 +1242,7 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
try {
|
|
|
isRestoreCompleted = objectMetadata.isRestoreCompleted();
|
|
|
} catch (Exception e) {
|
|
|
- // TODO Auto-generated catch block
|
|
|
+ // TODO OSS Auto-generated catch block
|
|
|
// e.printStackTrace();
|
|
|
logger.error("视频可能在解冻中");
|
|
|
}
|
|
@@ -1261,7 +1262,7 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
return -1;
|
|
|
}
|
|
|
String realPath = path.replace(PropertiesUtils.getDownloadDomain(), "");
|
|
|
- String bucketName = PropertiesUtils.getVideoBucket();
|
|
|
+ String bucketName = BasePropertiesUtils.getVideoBucket();
|
|
|
ObjectMetadata objectMetadata = null;
|
|
|
try {
|
|
|
objectMetadata = getInternalOssClient().getObjectMetadata(bucketName, realPath);
|
|
@@ -1295,7 +1296,7 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
|
|
|
|
|
|
public static Map<String, Object> getMediaAllInfo(String inPath) {
|
|
|
- return getMediaAllInfo(inPath, PropertiesUtils.getVideoBucket());
|
|
|
+ return getMediaAllInfo(inPath, BasePropertiesUtils.getVideoBucket());
|
|
|
}
|
|
|
|
|
|
public static Map<String, Object> getMediaAllInfo(String inPath, String bucketName) {
|
|
@@ -1379,5 +1380,97 @@ public class AliOssFileTool extends AliOssConfig {
|
|
|
return getInternalOssClient().copyObject(request);
|
|
|
}
|
|
|
|
|
|
+ public static String getRandomObjectKey(Integer fileType) {
|
|
|
+ String env = PropertiesUtils.getProjectEnv();
|
|
|
+ if (StringUtils.isEmpty(env)) {
|
|
|
+ env = "qa";
|
|
|
+ }
|
|
|
+ String fileName = RandomUtil.generate18String();
|
|
|
+ if (EnumUploadFileType.VIDEO.getIntType().equals(fileType)) {
|
|
|
+ fileName = "/video/" + env + "/" + DateUtils.dateToStringyyyyMMdd(new Date()) + "/" + fileName;
|
|
|
+ } else if (EnumUploadFileType.VOICE.getIntType().equals(fileType)) {
|
|
|
+ fileName = "/voice/" + env + "/" + DateUtils.dateToStringyyyyMMdd(new Date()) + "/" + fileName;
|
|
|
+ } else {
|
|
|
+ fileName = "/pic/" + env + "/" + DateUtils.dateToStringyyyyMMdd(new Date()) + "/" + fileName;
|
|
|
+ }
|
|
|
+ return getProjectName() + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static StsTokenVO getStsToken(String fileName, Long durationSeconds) {
|
|
|
+
|
|
|
+ String endpoint = BasePropertiesUtils.getLongvideoStsEndpoint();
|
|
|
+ String accessKeyId = BasePropertiesUtils.getLongvideoStsAccessKeyId();
|
|
|
+ String accessKeySecret = BasePropertiesUtils.getLongvideoStsAccessKeySecret();
|
|
|
+ String roleArn = BasePropertiesUtils.getLongvideoStsRoleArn();
|
|
|
+ String roleSessionName = BasePropertiesUtils.getLongvideoStsSessionName();
|
|
|
+ StsTokenVO result = new StsTokenVO();
|
|
|
+ try {
|
|
|
+ String region = "cn-hangzhou";
|
|
|
+ DefaultProfile.addEndpoint("", "", "Sts", endpoint);
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
|
|
|
+ DefaultAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ final AssumeRoleRequest request = new AssumeRoleRequest();
|
|
|
+ request.setMethod(MethodType.POST);
|
|
|
+ request.setRoleArn(roleArn);
|
|
|
+ request.setRoleSessionName(roleSessionName);
|
|
|
+ //设置过期时间 临时访问凭证的有效时间,单位为秒。最小值为900,最大值以当前角色设定的最大会话时间为准。角色最大会话时间取值范围为3600秒~43200秒
|
|
|
+ if (Objects.nonNull(durationSeconds)) {
|
|
|
+ request.setDurationSeconds(durationSeconds);
|
|
|
+ }
|
|
|
+ JSONObject policy = getUploadFileStsPolicy(BasePropertiesUtils.getVideoBucket(), fileName);
|
|
|
+ request.setPolicy(policy.toJSONString());
|
|
|
+
|
|
|
+ final AssumeRoleResponse response = client.getAcsResponse(request);
|
|
|
+ result.setExpiration(response.getCredentials().getExpiration());
|
|
|
+ result.setAccessKeyId(response.getCredentials().getAccessKeyId());
|
|
|
+ result.setAccessKeySecret(response.getCredentials().getAccessKeySecret());
|
|
|
+ result.setSecurityToken(response.getCredentials().getSecurityToken());
|
|
|
+ result.setRequestId(response.getRequestId());
|
|
|
+ result.setFileName(fileName);
|
|
|
+ result.setHost(AliOssConfig.getUploadDomain());
|
|
|
+
|
|
|
+ String host0 = BasePropertiesUtils.getOssaccelerateUploadDomain();
|
|
|
+ String host1 = "https://" + AliOssConfig.getBucket(EnumPublicBuckets.PUBBUCKET.getBucketName()) + "." + AliOssConfig.getOssEndPoint() + "/";
|
|
|
+ result.setHosts(new String[]{host0, host1});
|
|
|
+
|
|
|
+ result.setBucket(BasePropertiesUtils.getVideoBucket());
|
|
|
+ result.setRegion(region);
|
|
|
+ result.setCname(true);
|
|
|
+
|
|
|
+ // 返回服务器当前时间
|
|
|
+ result.setServerTimestamp(System.currentTimeMillis());
|
|
|
+
|
|
|
+ } catch (com.aliyuncs.exceptions.ClientException e) {
|
|
|
+ logger.error(String.format("获取临时令牌失败,errorCode:%s;errorMessage:%s;requestId:%s",
|
|
|
+ e.getErrCode(), e.getErrMsg(), e.getRequestId()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("获取临时令牌失败", e);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static JSONObject getUploadFileStsPolicy(String bucketName, String fileName) {
|
|
|
+
|
|
|
+ JSONObject policy = new JSONObject();
|
|
|
+ policy.put("Version", "1");
|
|
|
+
|
|
|
+ JSONArray statements = new JSONArray();
|
|
|
+ JSONObject statement = new JSONObject();
|
|
|
+ statement.put("Effect", "Allow");
|
|
|
+
|
|
|
+ JSONArray action = new JSONArray();
|
|
|
+ action.add("oss:PutObject");
|
|
|
+
|
|
|
+ statement.put("Action", action);
|
|
|
+ JSONArray resource = new JSONArray();
|
|
|
+ resource.add("acs:oss:*:1894469520484605:" + bucketName + "/" + fileName);
|
|
|
+ statement.put("Resource", resource);
|
|
|
+
|
|
|
+ statements.add(statement);
|
|
|
+ policy.put("Statement", statements);
|
|
|
+
|
|
|
+ logger.info(String.format("Policy:%s", JSON.toJSONString(policy)));
|
|
|
+ return policy;
|
|
|
+ }
|
|
|
|
|
|
}
|