Utils
package com.foxconn.devops.common.s3;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Component
public class AmazonFileUtils {
private final static Logger logger = LoggerFactory.getLogger(AmazonFileUtils.class);
/**
* S3服务器地址
*/
private static String S3_URL;
/**
* 账号key
*/
private static String ACCESS_KEY;
/**
* 密码key
*/
private static String SECRET_KEY;
private static String bucket;
/**
* 获取连接
* @return
* @throws Exception
*/
public static AmazonS3 connectS3Client() throws Exception {
URL url = new URL(S3_URL);
ClientConfiguration config = new ClientConfiguration().withProtocol(Protocol.HTTP);
AmazonS3 client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(url.toString(), Regions.CN_NORTH_1.toString()))
.withClientConfiguration(config)
.withPathStyleAccessEnabled(true)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY)))
.build();
return client;
}
public static String createPresignedGetUrl(String keyName) {
try {
// 创建S3客户端
AmazonS3 s3Client = connectS3Client();
// 创建预签名请求
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucket, keyName)
.withMethod(HttpMethod.GET);
// 设置过期时间,例如1小时后
long expirationTimeMinutes = 60;
generatePresignedUrlRequest.setExpiration(new Date(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(expirationTimeMinutes)));
//修改訪問權限
s3Client.setObjectAcl(bucket, keyName, CannedAccessControlList.PublicReadWrite);
// 获取预签名URL
URL presignedUrl = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
// 输出预签名URL
System.out.println("Pre-Signed URL: " + presignedUrl.toString());
return presignedUrl.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 下载
* @return
* @throws Exception
*/
public static void downloadFile(String fileKey,String fileName, HttpServletResponse resp) throws Exception{
resp.setCharacterEncoding("utf-8");
resp.setContentType("multipart/form-data");
resp.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream outputStream = resp.getOutputStream();
InputStream input = reLoadFile(fileKey);
byte[] content = new byte[1024];
int a = -1;
while ((a = input.read(content))!=-1){
outputStream.write(content,0,a);
}
input.close();
outputStream.flush();
}
public static InputStream reLoadFile(String fileKey){
AmazonS3 s3client = null;
try {
s3client = connectS3Client();
} catch (Exception e) {
e.printStackTrace();
}
GetObjectRequest request = new GetObjectRequest(bucket, fileKey);
S3Object response = s3client.getObject(request);
InputStream input = response.getObjectContent();
return input;
}
/**
* 删除文件
* @param fileKey
* @throws Exception
*/
public static void deleteFile(String fileKey) throws Exception {
AmazonS3 s3client = connectS3Client();
DeleteObjectRequest request = new DeleteObjectRequest(bucket, fileKey);
s3client.deleteObject(request);
}
/**
* Bucket列表
* @return
* @throws Exception
*/
public static List<Bucket> listFile() throws Exception{
AmazonS3 s3client = connectS3Client();
ListBucketsRequest request = new ListBucketsRequest();
List<Bucket> list = s3client.listBuckets(request);
return list;
}
/**
* 是否存在Bucket
* @param bucketName
* @return
* @throws Exception
*/
public static boolean isExistBucket(String bucketName) throws Exception{
AmazonS3 s3client = connectS3Client();
try {
HeadBucketRequest request = new HeadBucketRequest(bucketName);
s3client.headBucket(request);
} catch (Exception e) {
return false;
}
return true;
}
/**
* 创建Bucket
* @param bucketName
* @return
* @throws Exception
*/
public static void createBucket(String bucketName) throws Exception{
boolean isBucketExists = isExistBucket(bucketName);
AmazonS3 s3client = null;
if (!isBucketExists) {
s3client = connectS3Client();
CreateBucketRequest request = new CreateBucketRequest(bucketName);
s3client.createBucket(request);
}
}
/**
* 删除Bucket
* @param bucketName
* @return
* @throws Exception
*/
public static void deleteBucket(String bucketName) throws Exception {
AmazonS3 s3client = connectS3Client();
DeleteBucketRequest request = new DeleteBucketRequest(bucketName);
s3client.deleteBucket(request);
}
/**
* fileKey是否存在
* @param bucketName
* @param fileKey
* @return
* @throws Exception
*/
public static boolean isExistFileKey(String bucketName, String fileKey) throws Exception{
AmazonS3 s3client = connectS3Client();
GetObjectRequest request = new GetObjectRequest(bucketName,fileKey);
S3Object response = s3client.getObject(request);
boolean result = response != null && fileKey.equals(response.getKey());
return result;
}
/**
* fileKey是否存在
* @param fileKey
* @return
* @throws Exception
*/
public static boolean isExistFileKey(String fileKey) throws Exception{
AmazonS3 s3client = connectS3Client();
GetObjectRequest request = new GetObjectRequest(AmazonFileUtils.bucket,fileKey);
S3Object response = s3client.getObject(request);
boolean result = response != null && fileKey.equals(response.getKey());
return result;
}
/**
* 获取文件key
* @param fileName
* @return
* @throws Exception
*/
private static String getFileKey(String fileName) throws Exception{
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date());
String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
String fileKey = uuid + "." + fileType.toLowerCase();
return fileKey;
}
public static String uploadFile(MultipartFile file) throws Exception {
try {
if (file == null) {
throw new RuntimeException("文件不能為空");
}
createBucket(bucket);
String fileKey = getFileKey(file.getOriginalFilename());
InputStream fileInputStream = file.getInputStream();
val metadata = new ObjectMetadata();
metadata.setContentLength(fileInputStream.available());
metadata.setContentType(file.getContentType());
// 创建ACL
AccessControlList acl = new AccessControlList();
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); // 允许所有人读取
PutObjectRequest request = new PutObjectRequest(bucket, fileKey, fileInputStream, metadata).withAccessControlList(acl);
AmazonS3 s3client = connectS3Client();
s3client.putObject(request);
return fileKey;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
public static void uploadPdfFile(String pdfFilePath, String fileKey) {
try {
File file = new File(pdfFilePath);
if (file == null) {
throw new RuntimeException("文件不能為空");
}
createBucket(bucket);
// String fileKey = getFileKey(file.getOriginalFilename());
// InputStream fileInputStream = file.getInputStream();
InputStream fileInputStream = new FileInputStream(pdfFilePath);
val metadata = new ObjectMetadata();
metadata.setContentLength(fileInputStream.available());
metadata.setContentType("application/pdf");
// 创建ACL
AccessControlList acl = new AccessControlList();
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); // 允许所有人读取
PutObjectRequest request = new PutObjectRequest(bucket, fileKey, fileInputStream, metadata).withAccessControlList(acl);
AmazonS3 s3client = connectS3Client();
s3client.putObject(request);
} catch (Exception e) {
e.printStackTrace();
// throw e;
logger.error("fileKey==" + fileKey + "==uploadPdfFile失敗:" + e.getMessage());
}
}
public static String getPdfKey(String fileKey) {
return fileKey.substring(0, fileKey.lastIndexOf(".")) + ".pdf";
}
@Value("${amazon.url}")
public void setS3Url(String s3Url) {
S3_URL = s3Url;
}
@Value("${amazon.access.key}")
public void setAccessKey(String accessKey) {
ACCESS_KEY = accessKey;
}
@Value("${amazon.secret.key}")
public void setSecretKey(String secretKey) {
SECRET_KEY = secretKey;
}
@Value("${amazon.bucket}")
public void setBucket(String bucket) {
AmazonFileUtils.bucket = bucket;
}
}
預簽名URL可預覽而不是下載 需設置 文件類型 contentType
val metadata = new ObjectMetadata();
metadata.setContentLength(fileInputStream.available());
//設置文件類型 供瀏覽器識別展示預覽
metadata.setContentType(file.getContentType());