新增oss工具类

master
面包骑士 2024-09-09 16:38:34 +08:00
parent 2b07ea9b27
commit b011d9e0d0
1 changed files with 275 additions and 0 deletions

View File

@ -0,0 +1,275 @@
package com.muyu.common.core.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.*;
import com.aliyuncs.exceptions.ClientException;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
/**
* @Description Oss
*/
@Log4j2
public class OssUtil {
/**
* Endpoint AccessKeyaccessKeySecretAPI访 访
*/
private static String endPoint = "oss-cn-shanghai.aliyuncs.com";
private static String accessKeyId = "LTAI5tN9vdDeBioiVbGG3UG3";
private static String accessKeySecret = "gzhfYLb0UX0zH35DOki9yi1Q35ndgS";
// 从环境变量中获取访问凭证。运行本代码示例之前请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
private static EnvironmentVariableCredentialsProvider credentialsProvider;
private static final Integer OSS_TASK_NUM = 5;
/**
* 100 KB~5 GB100 KB
*/
private static final Integer OSS_PART_SIZE = 1024 * 1024; //1MB
/**
*
*/
private static final Boolean OSS_ENABLE_CHECKPOINT = true;
/**
* bucket
*
* @return
*/
private static String bucketName = "wangxin-123";
private static String accessPre = "https://"+bucketName+"."+endPoint+"/";
private static OSS ossClient;
static {
try {
credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
} catch (ClientException e) {
throw new RuntimeException(e);
}
ossClient = new OSSClientBuilder().build(
endPoint,
accessKeyId,
accessKeySecret);
log.info("oss服务连接成功");
}
/**
*
*
* @param filePath
*/
public static String uploadFile(String filePath) {
return uploadFileForBucket(bucketName, getOssFilePath(filePath), filePath);
}
/**
* multipartFile
*
* @param multipartFile
*/
public static String uploadMultipartFile(MultipartFile multipartFile) throws IOException {
return uploadMultipartFile(bucketName, getOssFilePath(multipartFile.getOriginalFilename()), multipartFile);
}
/**
* multipartFile
*
* @param bucketName
* @param ossPath
* @param multipartFile
*/
public static String uploadMultipartFile(String bucketName, String ossPath, MultipartFile multipartFile) throws IOException {
InputStream inputStream = null;
try {
inputStream = multipartFile.getInputStream();
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (null != inputStream) {
inputStream.close();
}
}
return accessPre + ossPath;
}
/**
* 使FilePutObject ** 使
*
* @param bucketName
* @param ossPath oss
* @param filePath
* @return
*/
public static String uploadFileForBucket(String bucketName, String ossPath, String filePath) {
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
// 上传
ossClient.putObject(putObjectRequest);
return accessPre + ossPath;
}
/**
* 使bucket
*
* @param bucketName
* @param ossPath oss
* @param filePath
*/
public static String uploadFileInputStreamForBucket(String bucketName, String ossPath, String filePath) {
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
//断点续传
uploadFileBreakpointResume(bucketName, ossPath, filePath);
return accessPre + ossPath;
}
public static void uploadFileInputStreamForBucket(String bucketName, String ossPath, InputStream inputStream) {
ossClient.putObject(bucketName, ossPath, inputStream);
}
public static void uploadFileBreakpointResume(String bucketName, String ossPath, String filePath) {
try {
// 通过UploadFileRequest设置多个参数。
// 依次填写Bucket名称例如examplebucket以及Object完整路径例如exampledir/exampleobject.txtObject完整路径中不能包含Bucket名称。
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, ossPath);
// 通过UploadFileRequest设置单个参数。
// 填写本地文件的完整路径例如D:\\localpath\\examplefile.txt。如果未指定本地路径则默认从示例程序所属项目对应本地路径中上传文件。
uploadFileRequest.setUploadFile(filePath);
// 指定上传并发线程数默认值为1。
uploadFileRequest.setTaskNum(OSS_TASK_NUM);
// 指定上传的分片大小单位为字节取值范围为100 KB~5 GB。默认值为100 KB。
uploadFileRequest.setPartSize(OSS_PART_SIZE);
// 开启断点续传,默认关闭。
uploadFileRequest.setEnableCheckpoint(OSS_ENABLE_CHECKPOINT);
// 记录本地分片上传结果的文件。上传过程中的进度信息会保存在该文件中,如果某一分片上传失败,再次上传时会根据文件中记录的点继续上传。上传完成后,该文件会被删除。
// 如果未设置该值,默认与待上传的本地文件同路径,名称为${uploadFile}.ucp。
//uploadFileRequest.setCheckpointFile();
// 设置上传回调参数为Callback类型。
//uploadFileRequest.setCallback("yourCallbackEvent");
// 断点续传上传。
ossClient.uploadFile(uploadFileRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 关闭OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
*
*
* @param ossFilePath
* @param filePath
*/
public static void downloadFile(String ossFilePath, String filePath) {
downloadFileForBucket(bucketName, ossFilePath, filePath);
}
/**
*
*
* @param bucketName
* @param ossFilePath oss
* @param filePath
*/
public static void downloadFileForBucket(String bucketName, String ossFilePath, String filePath) {
ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
}
/**
* @return
*/
public static String getOssDefaultPath() {
LocalDateTime now = LocalDateTime.now();
String url =
now.getYear() + "/" +
now.getMonth() + "/" +
now.getDayOfMonth() + "/" +
now.getHour() + "/" +
now.getMinute() + "/";
return url;
}
public static String getOssFilePath(String filePath) {
String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
}
public static Map<String,String> getUrl() throws ClientException {
// 设置上传到OSS文件的前缀可置空此项。置空后文件将上传至Bucket的根目录下。
String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String dir = "uploadTest/"+date+"/";
OSS ossClient = new OSSClientBuilder().build(endPoint, credentialsProvider);
try {
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
// PostObject请求最大可支持的文件大小为5 GB即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String accessId = credentialsProvider.getCredentials().getAccessKeyId();
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = ossClient.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<String, String>();
respMap.put("accessId", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", accessPre);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
// respMap.put("expire", formatISO8601Date(expiration));
return respMap;
} catch (Exception e) {
throw new ClientException("【上传文件失败】:返回前端所需参数异常;【原因】:{}",e.getMessage());
} finally {
ossClient.shutdown();
}
}
}