阿里云oss授权访问文档示例地址
阿里云没有提供直接的获取是视频时长的API接口,需要曲线处理
1、引入阿里云oss pom
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
阿里云工具类
/**
* @author ful
*/
@Component
@Slf4j
public class AliOSSUtil {
// endpoint 访问OSS的域名
@Value("${oss.agora.endpoint}")
public String endpoint;
// accessKeyId和accessKeySecret OSS的访问密钥
@Value("${oss.agora.id}")
public String accessKeyId;
@Value("${oss.agora.secret}")
public String accessKeySecret;
// Bucket 用来管理所存储Object的存储空间
@Value("${oss.agora.bucket}")
public String bucketName;
@Value("${oss.agora.region}")
public String region;
@Value("${oss.agora.vendor}")
public String vendor;
@Value("${oss.agora.cdn}")
public String cdn;
/**
* 文件直传
*
* @param objectKey 上传路径
* @param inputStream 上传流
* @throws RuntimeException
*/
public void fileUpload(String objectKey, InputStream inputStream) throws RuntimeException {
Map map = getCommon(objectKey);
OSS ossClient = null;
try {
ossClient = (OSS) map.get(0);
AliOssPublicEntity model = (AliOssPublicEntity) map.get(1);
if (ossClient.doesObjectExist(model.getBucketName(), model.getObjectKey())) {
log.error("此文件重名,请更改文件名重试!");
throw new RuntimeException("此文件重名,请更改文件名重试!");
}
PutObjectRequest putObjectRequest = new PutObjectRequest(model.getBucketName(), model.getObjectKey(), inputStream);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
String eTag = putObjectResult.getETag();
if (StringUtils.isBlank(eTag)) {
log.error("文件直传失败!");
throw new RuntimeException("文件直传失败");
}
} catch (Exception e) {
log.error("文件直传失败,exp={}", e);
throw new RuntimeException("文件直传失败:" + e.getMessage());
} finally {
ossClient.shutdown();
try {
inputStream.close();
} catch (IOException e) {
log.error("关闭文件流异常={}", e);
}
}
}
/**
* OSS获取下载签名URL
*
* @param objectKey 文件对象key
* @return 签名URL
*/
public String getOssObjectDownAuthUrl(String objectKey) throws RuntimeException {
Map map = getCommon(objectKey);
OSS ossClient = null;
try {
ossClient = (OSS) map.get(0);
AliOssPublicEntity model = (AliOssPublicEntity) map.get(1);
GeneratePresignedUrlRequest req =
new GeneratePresignedUrlRequest(model.getBucketName(), model.getObjectKey(), HttpMethod.GET);
//这里设置签名在30小时后过期
Date expiration = new Date(new Date().getTime() + 30L * 60L * 60L * 1000);// 生成URL
// Date expireDate = new Date(System.currentTimeMillis() + 30L * 60L * 60L * 1000L);
req.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(req);
String urlStr = url.toString();
return urlStr;
} catch (Exception e) {
log.error("getOssObjectDownAuthUrl 获取下载签名URL失败,exp={}", e);
throw new RuntimeException("获取下载签名URL失败");
} finally {
ossClient.shutdown();
}
}
/**
* OSS获取下载签名URL
*
* @param objectKey 文件对象key
* @param expireTime 当前时间加多少毫秒后过期,过期时间(毫秒)
* @return 签名URL
*/
public String getOssObjectDownAuthUrl(String objectKey, long expireTime) throws RuntimeException {
Map map = getCommon(objectKey);
OSS ossClient = null;
try {
ossClient = (OSS) map.get(0);
AliOssPublicEntity model = (AliOssPublicEntity) map.get(