赞
踩
<!--aws s3-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<aws.s3.version>1.11.543</aws.s3.version>
amazon:
aws:
access-key-id: AKIAT
access-key-secret: Tv7Ck
s3:
default-bucket:
region:
endpoint: https://.com/
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* ClassName AwsConfig
*
* @author wang
* Date 2022/6/23 10:19
*/
@Configuration
@Component
public class AwsConfig {
@Value("${amazon.aws.access-key-id}")
private String accessKey;
@Value("${amazon.aws.access-key-secret}")
private String secretKey;
@Value("${amazon.s3.region}")
private String region;
@Bean
public AmazonS3 s3Client() {
AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}
}
import cn.hutool.core.util.IdUtil;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Date;
/**
* ClassName AwsSysFileServiceImpl
*
* @author wang
* Date 2022/6/23 10:11
*/
@Service
@RequiredArgsConstructor
public class AwsSysFileService {
final AmazonS3 s3Client;
@Value("${amazon.aws.access-key-id}")
private String accessKey;
@Value("${amazon.s3.default-bucket}")
private String bucketName;
@Value("${amazon.s3.endpoint}")
private String endpoint;
@Value("${spring.profiles.active}")
private String env;
public String uploadFile(MultipartFile multipartFile, String source) throws IOException {
String originalFilename = multipartFile.getOriginalFilename();
assert originalFilename != null;
FileTypeUtils.checkFileType(originalFilename);
String fileName = this.generateFileName(originalFilename);
File file = this.convertMultiPartToFile(multipartFile);
String filePath = bucketName + "/" + "test" + "/"+ source + "/" + SDFFactory.DATE_DASH.format(new Date());
this.uploadFileToS3bucket(filePath, fileName, file);
file.delete();
return endpoint + filePath + "/" + fileName;
}
/**
* 创建临时文件
*
* @param multipartFile
* @return
* @throws IOException
*/
private File convertMultiPartToFile(MultipartFile multipartFile) throws IOException {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
assert fileName != null;
String prefix = fileName.substring(fileName.lastIndexOf("."));
// 用uuid作为文件名,防止生成的临时文件重复
File excelFile = File.createTempFile(IdUtil.randomUUID(), prefix);
// MultipartFile to File
multipartFile.transferTo(excelFile);
return excelFile;
}
private String generateFileName(String originalFilename) {
return System.currentTimeMillis() + "-" + originalFilename.replace(" ", "_");
}
private void uploadFileToS3bucket(String filePath, String fileName, File file) {
s3Client.putObject(new PutObjectRequest(filePath, fileName, file)
.withCannedAcl(CannedAccessControlList.PublicRead));
}
public void deleteFileFromS3Bucket(String fileUrl) {
String fileName = fileUrl.replaceAll(endpoint, "");
s3Client.deleteObject(new DeleteObjectRequest(bucketName, fileName));
}
public void downloadFile(String fileUrl) throws IOException {
ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
.withCacheControl("No-cache")
.withContentDisposition("attachment; filename=example.txt");
GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(fileUrl, accessKey)
.withResponseHeaders(headerOverrides);
S3Object headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride);
displayTextInputStream(headerOverrideObject.getObjectContent());
}
private void displayTextInputStream(InputStream input) throws IOException {
// Read the text input stream one line at a time and display each line.
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。