OSS管理文件(Java)

2023-03-07,,

工具类

package com.panchina.util;

import com.alibaba.druid.util.StringUtils;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.Date; /**
* 阿里云OSS
*/
@Slf4j
@Component
public class OssUtil {
private static String endpoint ; private static String sts ; private static String accessKeyId; private static String accessKeySecret; private static String stsKeyId; private static String stsKeySecret; private static String roleArn; private static String bucketName;
private static int duration; @Value("${aliyun.oss.accessKeySecret}")
public void setAccessKeySecret(String accessKeySecret) {
OssUtil.accessKeySecret = accessKeySecret;
}
@Value("${aliyun.oss.accessKeyId}")
public void setAccessKeyId(String accessKeyId) {
OssUtil.accessKeyId = accessKeyId;
}
@Value("${aliyun.oss.stsKeyId}")
public void setStsKeyId(String stsKeyId) {
OssUtil.stsKeyId = stsKeyId;
}
@Value("${aliyun.oss.stsKeySecret}")
public void setStsKeySecret(String stsKeySecret) {
OssUtil.stsKeySecret = stsKeySecret;
}
@Value("${aliyun.oss.endpoint}")
public void setEndpoint(String endpoint) {
OssUtil.endpoint = endpoint;
}
@Value("${aliyun.oss.sts}")
public void setSts(String sts) {
OssUtil.sts = sts;
} @Value("${aliyun.oss.duration}")
public void setduration(int duration) {
OssUtil.duration = duration;
}
@Value("${aliyun.oss.roleArn}")
public void setroleArn(String roleArn) {
OssUtil.roleArn = roleArn;
}
@Value("${aliyun.oss.bucketName}")
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
/**
* 返回sts_token
* @return
*/
public static AssumeRoleResponse getSecurityToken(String roleSessionName) {
try {
// 添加endpoint(直接使用STS endpoint,前两个参数留空,无需添加region ID)
DefaultProfile.addEndpoint("", "", "Sts", sts); // 构造default profile(参数留空,无需添加region ID)
IClientProfile profile = DefaultProfile.getProfile("", stsKeyId, stsKeySecret);
// 用profile构造client
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setMethod(MethodType.POST);
request.setRoleArn(roleArn);
// RoleSessionName 是临时Token的会话名称,自己指定用于标识你的用户,主要用于审计,或者用于区分Token颁发给谁
// 但是注意RoleSessionName的长度和规则,不要有空格,只能有'-' '_' 字母和数字等字符
request.setRoleSessionName(roleSessionName);
request.setPolicy(null); // 若policy为空,则用户将获得该角色下所有权限
request.setDurationSeconds(duration*60L); // 设置凭证有效时间
final AssumeRoleResponse response = client.getAcsResponse(request);
return response;
} catch (ClientException e) {
log.info(e.getErrMsg());
return null;
}
} public static String uploadObjectByInputStream(InputStream inputStream, String folder, String fileName ){
//folder+/+fileName
OSS client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String key;
if(StringUtils.isEmpty(folder)){
key = fileName;
}else{
key = folder + fileName;
}
try{
client.putObject(bucketName, key, inputStream);
}catch (Exception e){
throw e;
}finally {
client.shutdown();
}
return key;
} /**
* 上传文件-全路径(a/b/c/filename)(流的形式-ecs免流量)
* @param inputStream 文件流
* @param fileFullPath 文件全路径(保留后缀)
* @return 保存的文件key
*/
public static String uploadObjectByInputStream(InputStream inputStream, String fileFullPath) throws Exception{
OSS client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try{
client.putObject(bucketName, fileFullPath, inputStream);
}catch (Exception e){
throw e;
}finally {
client.shutdown();
}
return fileFullPath;
}
/**
* 根据文件名获取文件访问URL(外网访问-下行收费)
* @param ossFileKey 文件名
* @param expires URL有效时间(分钟)
* @return
*/
public static String getFileUrl(String ossFileKey, int expires) {
OSS client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
URL url = client.generatePresignedUrl(bucketName, ossFileKey, dateAfter(new Date(), expires, Calendar.MINUTE));
String path = url.toString();
return path;
} /**
* 根据文件名获取文件访问URL(外网访问-下行收费)
* @param ossFileKey 文件名
* @return
*/
public static String getresizeFileUrl(String ossFileKey) {
OSS client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 设置图片处理样式。
String style = "image/resize,m_fixed,w_305,h_200";
// 指定过期时间为10分钟。
Date expiration = dateAfter(new Date(), 30, Calendar.MINUTE);
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, ossFileKey, HttpMethod.GET);
req.setExpiration(expiration);
req.setProcess(style);
String path = client.generatePresignedUrl(req).toString();
return path;
}
/**
* 删除文件
* @param folder 文件夹
* @param fileName 文件名
* @return
*/
public static void deleteFile(String folder, String fileName){
OSS client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String key;
if(StringUtils.isEmpty(folder)){
key = fileName;
}else{
key = folder + fileName;
}
client.deleteObject(bucketName,key);
client.shutdown();
} /**
* (private)获得指定日期之后一段时期的日期。例如某日期之后3天的日期等。
* @param origDate 基准日期
* @param amount 基准日期
* @param timeUnit 时间单位,如年、月、日等。用Calendar中的常量代表
* @return
*/
private static final Date dateAfter(Date origDate, int amount, int timeUnit) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(origDate);
calendar.add(timeUnit, amount);
return calendar.getTime();
}
}

上传文件

@Transactional(rollbackFor=Exception.class)
public void buildUpload(MultipartFile[] files, long postId, Date now, Long loginId) throws Exception {
PostAttachment postAttachment = new PostAttachment();
String uploadUrl = null;
String imgUrl = null;
for (MultipartFile file : files) {
Integer resultType = opinionFileType(file);
String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); uploadUrl = OssUtil.uploadObjectByInputStream(file.getInputStream(),"partyCircle/"+PrimaryKeyUtil.getPrimaryKeyId()+"."+fileSuffix);
// if (resultType == 1) {
// uploadUrl = imageFileService.upload(file);
// }else if (resultType == 2){
// uploadUrl=videoFileService.uploadvideoAndImage(file).get(0);
// imgUrl=videoFileService.uploadvideoAndImage(file).get(1);
// }
postAttachment.setId(PrimaryKeyUtil.getPrimaryKeyId());
postAttachment.setPostId(postId);
postAttachment.setOrigSuffix(fileSuffix);
postAttachment.setOrigUrl(uploadUrl);
postAttachment.setType(resultType);
postAttachmentMapper.insertSelective(postAttachment);
}
}

获取文件

 public List<ForumPostVO> selectForumPostList(ForumPostListQuery forumPostListQuery) {
List<ForumPostVO> forumPostVOList = new ArrayList<>();
List<ForumPostVO> forumPostVOS = forumPostMapperExt.selectForumPostList(forumPostListQuery);
if (forumPostVOS.isEmpty()) {
return forumPostVOS;
}
List<Long> idList=forumPostVOS.stream().map(ForumPostVO::getId).collect(Collectors.toList());
List<PostAttachment> attachmentList = postAttachmentMapperExt.getPostAttachment(idList);
for (PostAttachment curr:attachmentList) {
String origUrl=curr.getOrigUrl();
if(origUrl != null && origUrl.length() > 0){
if(curr.getType().equals(1)){
String url= OssUtil.getresizeFileUrl(origUrl);
curr.setOrigUrl(url);
}
else
{
String url= OssUtil.getFileUrl(origUrl,30);
curr.setOrigUrl(url);
}
}
} Collections.sort(forumPostVOList, new Comparator<ForumPostVO>() {
@Override
public int compare(ForumPostVO f1, ForumPostVO f2) {
return f2.getCreateTime().compareTo(f1.getCreateTime());
}
});
return forumPostVOList;
}

OSS管理文件(Java)的相关教程结束。

《OSS管理文件(Java).doc》

下载本文的Word格式文档,以方便收藏与打印。