黄磊的项目,分享文件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

119 lines
3.9 KiB

package com.ruoyi.framework.web.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.framework.web.ilisteners.PutObjectProgressListener;
import com.ruoyi.framework.web.ilisteners.PutProcessData;
import com.ruoyi.system.domain.BFile;
import com.ruoyi.system.service.IBFileService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.UUID;
@Data
@Slf4j
@Service
public class AliOSSService {
@Value("${ali.oss.endpoint}")
private String endpoint = "yourEndpoint";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
@Value("${ali.oss.accessKey}")
private String accessKeyId = "yourAccessKeyId";
@Value("${ali.oss.secret}")
private String accessKeySecret = "yourAccessKeySecret";
//阿里云BucketName
@Value("${ali.oss.bucketName}")
private String bucketName;
//阿里云绑定的域名
@Value("${ali.oss.domain}")
private String domain;
//阿里云路径前缀
@Value("${ali.oss.prefix}")
private String prefix;
public OSS newOss() {
log.info("new oss accessKeyId:{}", accessKeyId);
return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
}
public void remove(OSS client, String objectName, String path) {
if (client.doesObjectExist(objectName, path)){
client.deleteObject(bucketName, path);
}
}
public String upload(OSS client, InputStream inputStream, String path) {
try {
client.putObject(bucketName, path, inputStream);
} catch (Exception e) {
throw new BaseException("上传文件失败,请检查配置信息");
}
return domain + "/" + path;
}
public String upload(OSS client, PutProcessData processData , String fileName, File f, String path, IBFileService bFileService, BFile bile) {
try {
/*PutObjectResult putObjectResult=client.putObject(bucketName, path, inputStream);*/
client
.putObject(new PutObjectRequest(bucketName,
path, f)
.<PutObjectRequest> withProgressListener(
new PutObjectProgressListener(processData,bFileService,bile)
));
} catch (Exception e) {
throw new BaseException("上传文件失败,请检查配置信息");
}
return domain + "/" + path;
}
public String uploadSuffix(OSS client, PutProcessData processData , String fileName, File f, String suffix,IBFileService bFileService, BFile bile) {
return upload(client, processData, fileName, f, getPath(getPrefix(), suffix),bFileService,bile);
}
public String uploadSuffix(OSS client, byte[] data, String suffix) {
return upload(client, new ByteArrayInputStream(data), getPath(getPrefix(), suffix));
}
public String uploadSuffix(OSS client, InputStream inputStream, String suffix) {
return upload(client, inputStream, getPath(getPrefix(), suffix));
}
public String getPath(String prefix, String suffix) {
//生成uuid
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//文件路径
String path = DateUtil.today().replaceAll("-", "");
if (StrUtil.isNotBlank(prefix)) {
path = prefix + "/" + path;
}
return path + suffix;
}
}