package service;
@Slf4j
public class MinIOFileUploader implements FileUploader {
/** bucketName */
private String bucketName ;
/** accessKey */
private String accessKey ;
/** secretKey */
private String secretKey;
/** endpoint */
private String endpoint ;
/** download ip */
private String host ;
/** S3Client*/
AmazonS3 s3;
/**
* 构造方法
*
* @param bucketName
* @param accessKey
* @param secretKey
* @param endpoint
* @param host
*/
public MinIOFileUploader(
String bucketName, String accessKey,String secretKey, String endpoint, String host) {
this.bucketName = bucketName;
this.accessKey = accessKey;
this.secretKey = secretKey;
this.endpoint = endpoint;
this.host = host;
}
/**
* 附件上传实现类
* @author 拿铁
* @date 2021/09/13
* @return String
* */
@Override
public String upload(MultipartFile file)
throws FileExtensionNotFoundException, IOException, InterruptedException,
UriFormatUnmatchedException {
String fileType = file.getOriginalFilename();
fileType = !ObjectUtils.isEmpty(fileType) && fileType.contains(".") ?
fileType.split("\\.")[1] : fileType;
String tempFileName = FileUtils.generateRandomFileName() + "." +fileType;
String contentType = file.getContentType();
long fileSize = file.getSize();
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(contentType);
objectMetadata.setContentLength(fileSize);
initS3();
//存在则打印桶
if (!s3.doesBucketExistV2(bucketName)) {
//不存在则创建桶
s3.createBucket(bucketName);
}
s3.putObject(bucketName, tempFileName, file.getInputStream(), objectMetadata);
return host + tempFileName;
}
/**
* 附件下载地址接口,直接返回文件(实时调接口获取文件)
* @author 拿铁
* @date 2021/09/15
* */
@Override
public void download(String key, HttpServletResponse response,String fileName) {
S3ObjectInputStream s3input = null;
ServletOutputStream outputStream = null;
try {
initS3();
S3Object S3Result = s3.getObject(bucketName, key);
s3input = S3Result.getObjectContent();
response.addHeader("Content-Disposition", "attachment; filename=" +
URLEncoder.encode(fileName, "UTF-8"));
outputStream = response.getOutputStream();
byte[] readBuf = new byte[1024];
int readLen = 0;
while ((readLen = s3input.read(readBuf)) > 0) {
outputStream.write(readBuf, 0, readLen);
}
} catch (AmazonServiceException e) {
throw new RuntimeException();
} catch (FileNotFoundException e) {
throw new RuntimeException();
} catch (IOException e) {
throw new RuntimeException();
}finally {
try {
s3input.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取附件下载地址url
* @author 拿铁
* @date 2021/09/13
* @return url
* */
public String generatePresignedUrl(String tempFileName) {
URL url ;
try {
Date expiration = new Date();
long expTimeMillis = Instant.now().toEpochMilli();
expTimeMillis += 1000 * 60 * 60;
expiration.setTime(expTimeMillis);
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucketName, tempFileName)
.withMethod(HttpMethod.GET)
.withExpiration(expiration);
url = s3.generatePresignedUrl(generatePresignedUrlRequest);
System.out.println(url);
} catch (AmazonServiceException e) {
System.out.println(e.getErrorMessage());
throw new RuntimeException();
}
return url.toString();
}
/**
* 初始化S3Client
* @author 拿铁
* @date 2021/09/13
* */
public void initS3() {
ClientConfiguration config = new ClientConfiguration();
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(endpoint, "us-east-1");
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(config)
.withCredentials(awsCredentialsProvider)
.disableChunkedEncoding()
.withPathStyleAccessEnabled(true)
.build();
}
}
minio附件功能接入核心代码总结
最新推荐文章于 2024-06-26 15:55:21 发布