package com.tievd.jyz.util;
|
|
import com.amazonaws.ClientConfiguration;
|
import com.amazonaws.HttpMethod;
|
import com.amazonaws.Protocol;
|
import com.amazonaws.SDKGlobalConfiguration;
|
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
import com.amazonaws.auth.BasicAWSCredentials;
|
import com.amazonaws.client.builder.AwsClientBuilder;
|
import com.amazonaws.services.s3.AmazonS3;
|
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
import com.amazonaws.services.s3.model.S3Object;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.StringUtils;
|
|
import javax.annotation.PostConstruct;
|
import java.io.ByteArrayInputStream;
|
import java.io.InputStream;
|
import java.net.URL;
|
import java.util.Date;
|
|
/**
|
* S3协议存储处理工具类
|
* @author Wang
|
*
|
*/
|
@Component
|
public class S3Util {
|
|
public final String HTTPS_STR = "HTTPS";
|
|
@Value("${init.minio.minio_name}")
|
public String accessKey;
|
@Value("${init.minio.minio_pass}")
|
public String secretKey;
|
@Value("${init.minio.bucketName}")
|
public String bucketName;
|
@Value("${init.minio.minio_url}")
|
public String serviceEndpoint;
|
@Value("${init.minio.ext_minio_url:${init.minio.minio_url}}")
|
public String extServiceEndpoint;
|
@Value("${init.minio.device_minio_url:${init.minio.ext_minio_url:${init.minio.minio_url}}}")
|
public String deviceServiceEndpoint;
|
|
@Value("${init.minio.pathStyle:true}")
|
public boolean isPathStyle;
|
|
private static AmazonS3 conn = null;
|
|
/**
|
* 上传文件
|
* @param bytes
|
* @param key s3 key名
|
* @param bucketName s3桶名
|
* @return
|
* @throws Exception
|
*/
|
public String uploadObject(byte[] bytes, String key, String bucketName) throws Exception {
|
try(InputStream inputStream = new ByteArrayInputStream(bytes);){
|
ObjectMetadata metadata = new ObjectMetadata();
|
metadata.setContentLength(inputStream.available());
|
conn.putObject(bucketName, key, inputStream, metadata);
|
return key;
|
}
|
}
|
|
/**
|
* 上传文件
|
* @param bytes 数据
|
* @param fileName 文件名
|
* @return
|
* @throws Exception
|
*/
|
public String uploadObject(byte[] bytes,String fileName) throws Exception {
|
try(InputStream inputStream = new ByteArrayInputStream(bytes);){
|
ObjectMetadata metadata = new ObjectMetadata();
|
metadata.setContentLength(inputStream.available());
|
String key = fileName.substring(0, fileName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
|
conn.putObject(bucketName, key, inputStream, metadata);
|
return key;
|
}
|
}
|
|
|
/**
|
* 根据文件名称进行删除
|
* @param bucketName
|
* @param key
|
* @return
|
*/
|
public void delFile(String bucketName, String key){
|
try {
|
if (conn.doesBucketExistV2(bucketName) == false) {
|
return;
|
}
|
conn.deleteObject(bucketName, key);
|
}catch (Exception ex){
|
ex.printStackTrace();
|
}
|
}
|
|
/**
|
* 下载文件
|
* @param key
|
* @param bucketName
|
* @return
|
* @throws Exception
|
*/
|
public S3Object download(String key, String bucketName) throws Exception {
|
S3Object obj = conn.getObject(bucketName, key);
|
return obj;
|
}
|
|
/**
|
* 生成url
|
* @param bucketName
|
* @param key
|
* @param expires 单位:秒
|
* @return
|
*/
|
public String createUrl(String bucketName, String key, Integer expires){
|
if(StringUtils.isEmpty(bucketName)){
|
return key;
|
}
|
try {
|
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
|
urlRequest.setExpiration(new Date(System.currentTimeMillis()+expires*1000));
|
URL url = conn.generatePresignedUrl(urlRequest);
|
return url.toString();
|
}catch (Exception e){
|
e.printStackTrace();
|
return "";
|
}catch (NoSuchFieldError noSuchFieldError){
|
noSuchFieldError.printStackTrace();
|
return "";
|
}
|
}
|
|
/**
|
* 创建桶
|
* @param bucketName
|
*/
|
public void creatBucket(String bucketName) {
|
conn.createBucket(bucketName);
|
}
|
|
@PostConstruct
|
public void creatConnect() {
|
System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");
|
System.setProperty(SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY,
|
"true");
|
if (SDKGlobalConfiguration.isCertCheckingDisabled()) {
|
System.out.println("Cert checking is disabled");
|
}
|
ClientConfiguration config = new ClientConfiguration();
|
config.setSignerOverride("S3SignerType");
|
config.setProtocol(Protocol.HTTP);
|
if(isHttps(serviceEndpoint)) {
|
config.setProtocol(Protocol.HTTPS);
|
}
|
config.setConnectionTimeout(30*1000);
|
config.setMaxConnections(200);
|
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
|
AwsClientBuilder.EndpointConfiguration endpointConfiguration
|
= new AwsClientBuilder.EndpointConfiguration(serviceEndpoint, "");
|
conn = com.amazonaws.services.s3.AmazonS3ClientBuilder.standard()
|
.withClientConfiguration(config)
|
.withCredentials(new AWSStaticCredentialsProvider(credentials))
|
.withEndpointConfiguration(endpointConfiguration)
|
.withPathStyleAccessEnabled(isPathStyle)
|
.build();
|
|
}
|
|
/**
|
* 是否是https请求
|
* @param serviceEndpoint
|
* @return
|
*/
|
private boolean isHttps(String serviceEndpoint){
|
String tmpServiceEndpoint = serviceEndpoint.toUpperCase();
|
if(tmpServiceEndpoint.startsWith(HTTPS_STR)){
|
return true;
|
}
|
return false;
|
}
|
|
public String getBucketName(){
|
return this.bucketName;
|
}
|
}
|