103 lines
4.2 KiB
Java
Raw Normal View History

2024-03-27 11:12:20 +08:00
package com.sky.utils;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
2024-04-11 12:56:00 +08:00
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.sky.properties.AliOssProperties;
2024-03-27 11:12:20 +08:00
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
2024-04-11 12:56:00 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
2024-03-27 11:12:20 +08:00
import java.io.ByteArrayInputStream;
2024-04-11 12:56:00 +08:00
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
2024-03-27 11:12:20 +08:00
@Data
@AllArgsConstructor
@Slf4j
2024-04-11 12:56:00 +08:00
@Component
2024-03-27 11:12:20 +08:00
public class AliOssUtil {
2024-04-11 12:56:00 +08:00
@Autowired
private AliOssProperties aliOssProperties;
2024-03-27 11:12:20 +08:00
2024-04-11 12:56:00 +08:00
// private String endpoint;
// private String accessKeyId;
// private String accessKeySecret;
// private String bucketName;
//
// /**
// * 文件上传
// *
// * @param bytes
// * @param objectName
// * @return
// */
// public String upload(byte[] bytes, String objectName) {
//
// // 创建OSSClient实例。
// OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
// try {
// // 创建PutObject请求。
// ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
// } 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 (ClientException 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 {
// if (ossClient != null) {
// ossClient.shutdown();
// }
// }
//
// //文件访问路径规则 https://BucketName.Endpoint/ObjectName
// StringBuilder stringBuilder = new StringBuilder("https://");
// stringBuilder
// .append(bucketName)
// .append(".")
// .append(endpoint)
// .append("/")
// .append(objectName);
//
// log.info("文件上传到:{}", stringBuilder.toString());
//
// return stringBuilder.toString();
// }
public String upload(MultipartFile file) throws IOException, com.aliyuncs.exceptions.ClientException {
InputStream inputStream = file.getInputStream();
// 避免文件覆盖
String originalFilename = file.getOriginalFilename();
String extname = originalFilename.substring(originalFilename.lastIndexOf("."));//文件扩展名
String fileName = UUID.randomUUID().toString() + extname;
2024-03-27 11:12:20 +08:00
2024-04-11 12:56:00 +08:00
//上传文件到 OSS
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //从环境变量中获取
OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), credentialsProvider);
PutObjectRequest putObjectRequest = new PutObjectRequest(aliOssProperties.getBucketName(), fileName, inputStream);
PutObjectResult result = ossClient.putObject(putObjectRequest);
2024-03-27 11:12:20 +08:00
2024-04-11 12:56:00 +08:00
//文件访问路径
String url = aliOssProperties.getEndpoint().split("//")[0] + "//" + aliOssProperties.getBucketName() + "." + aliOssProperties.getEndpoint().split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
return url;// 把上传到oss的路径返回
2024-03-27 11:12:20 +08:00
}
}