步骤一
引入aws sdk包
<!-- 亚马逊 S3 储存sdk -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.415</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
</exclusions>
</dependency>
创建类S3Service
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 com.amazonaws.services.s3.model.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipInputStream;
public class S3Service {
private final org.slf4j.Logger LOG = LoggerFactory.getLogger(S3Service.class);
private String AWS_S3_BUCKET_NAME;
private static AmazonS3 s3;
/**
* 加速域名
*/
private static String ACCELERAT_URL = "****.***.***";
protected S3Service() {
}
public S3Service(String accessKey, String secretKey, String bucketName, String region) {
this.AWS_S3_BUCKET_NAME = bucketName;
initS3(accessKey, secretKey, bucketName, region);
}
public void initS3(String accessKey, String secretKey, String bucketName, String region) {
Regions clientRegion = Regions.fromName(region);
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
s3 = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withForceGlobalBucketAccessEnabled(true)
.withRegion(clientRegion)
.build();
}
public String uploadFile(String path, InputStream is, ObjectMetadata om) throws Exception {
long start = System.currentTimeMillis();
s3.putObject(new PutObjectRequest(AWS_S3_BUCKET_NAME, path, is, om));
AccessControlList acl = s3.getObjectAcl(AWS_S3_BUCKET_NAME, path);
//设置文件权限为所有人可访问,此处操作须aws控制台开启acl权限,如果控制台设置公共访问,此处可以不用设置权限
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3.setObjectAcl(AWS_S3_BUCKET_NAME, path, acl);
//生成公用的url
URL url = s3.getUrl(AWS_S3_BUCKET_NAME, path);
LOG.info("AWS S3 Upload File spend time:{}ms", System.currentTimeMillis() - start);
return accelerateUrlReplace(url.toString());
}
public String uploadFile(String path, String bucketName, InputStream is, ObjectMetadata om) throws Exception {
long start = System.currentTimeMillis();
s3.putObject(new PutObjectRequest(bucketName, path, is, om));
AccessControlList acl = s3.getObjectAcl(bucketName, path);
//设置文件权限为所有人可访问
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3.setObjectAcl(bucketName, path, acl);
//生成公用的url
URL url = s3.getUrl(AWS_S3_BUCKET_NAME, path);
LOG.info("AWS S3 Upload File spend time:{}ms", System.currentTimeMillis() - start);
return accelerateUrlReplace(url.toString());
}
public S3Object downloadFile(String path) throws Exception {
long start = System.currentTimeMillis();
S3Object s3Object = s3.getObject(new GetObjectRequest(AWS_S3_BUCKET_NAME, path));
LOG.info("AWS S3 Download File spend time:{}ms", System.currentTimeMillis() - start);
return s3Object;
}
public S3Object downloadFile(String path, String bucketName) throws Exception {
long start = System.currentTimeMillis();
S3Object s3Object = s3.getObject(new GetObjectRequest(bucketName, path));
LOG.info("AWS S3 Download File spend time:{}ms", System.currentTimeMillis() - start);
return s3Object;
}
/**
* 下载S3文件到本地
* @param s3Path S3路径
* @param localPath 文件本地路径
*/
public void storageS3FileToLocal(String s3Path, String localPath) {
File file = new File(localPath);
if (!file.exists()) {
File folder = new File(StringUtils.substringBeforeLast(localPath, File.separator));
if (!folder.exists()) {
folder.mkdirs();
}
long start = System.currentTimeMillis();
try (S3Object s3Object = s3.getObject(new GetObjectRequest(AWS_S3_BUCKET_NAME, s3Path));
InputStream is = s3Object.getObjectContent();
OutputStream os = new FileOutputStream(localPath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
} catch (Exception e) {
LOG.error("Download S3 File To Local Error|s3Path:{}, localPath:{}", s3Path, localPath, e);
}
LOG.info("Download And Storage S3 File To Local spend time:{}ms", System.currentTimeMillis() - start);
} else {
file.setLastModified(System.currentTimeMillis());
}
}
/**
* 下载S3压缩文件到本地并解压
* @param s3Path S3路径
* @param localPath 文件本地路径
*/
public void storageS3ZipFileToLocal(String s3Path, String localPath) {
File file = new File(localPath);
if (!file.exists()) {
File folder = new File(StringUtils.substringBeforeLast(localPath, File.separator));
if (!folder.exists()) {
folder.mkdirs();
}
long start = System.currentTimeMillis();
if (StringUtils.endsWith(s3Path, ".zip")) {
try (S3Object s3Object = s3.getObject(new GetObjectRequest(AWS_S3_BUCKET_NAME, s3Path));
ZipInputStream zis = new ZipInputStream(s3Object.getObjectContent())) {
while (zis.getNextEntry() != null) {
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = zis.read(buf)) != -1) {
fos.write(buf, 0, bytesRead);
}
fos.flush();
} catch (Exception e) {
LOG.error("Storage S3 Zip NextEntry File To Local Error|s3Path:{}, localPath:{}", s3Path, localPath, e);
}
}
} catch (Exception e) {
LOG.error("Storage S3 Zip File To Local Error|s3Path:{}, localPath:{}", s3Path, localPath, e);
}
} else {
//.gz .dat .txt
try (S3Object s3Object = s3.getObject(new GetObjectRequest(AWS_S3_BUCKET_NAME, s3Path));
BufferedInputStream bis = new BufferedInputStream(new GZIPInputStream(s3Object.getObjectContent()));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
int bytesRead;
byte[] buf = new byte[1024];
while ((bytesRead = bis.read(buf)) != -1) {
bos.write(buf, 0, bytesRead);
}
bos.flush();
} catch (Exception e) {
LOG.error("Storage S3 (GZ DAT TXT) File To Local Error|s3Path:{}, localPath:{}", s3Path, localPath, e);
}
}
LOG.info("Download And Storage S3 Zip File To Local spend time:{}ms", System.currentTimeMillis() - start);
} else {
file.setLastModified(System.currentTimeMillis());
}
}
public ObjectMetadata getObjectMetadata(String path) {
long start = System.currentTimeMillis();
ObjectMetadata om = null;
try {
om = s3.getObjectMetadata(AWS_S3_BUCKET_NAME, path);
} catch (Throwable te) {
}
LOG.info("AWS S3 Get File Meta spend time:{}ms", System.currentTimeMillis() - start);
return om;
}
public String getFileNameByType(String platform, String type, String suffix) {
String dayStr = (new SimpleDateFormat("yyyyMMdd")).format(new Date());
Long num = System.currentTimeMillis();
String name = !StringUtils.isEmpty(platform) ? (platform + "/" + type + "/" + dayStr + "/" + num) : (type + "/" + dayStr + "/" + num);
return name + "." + suffix;
}
/**
* 域名替换,如果直接使用源域名可不用替换
*/
private String accelerateUrlReplace(String url) {
if (!EnvUtil.isTestOrQa()) {
int startIndex = url.indexOf("://") + 3;
int endIndex = url.indexOf(".com") + 4;
url = url.replaceFirst(url.substring(startIndex, endIndex), ACCELERAT_URL);
}
return url;
}
}
#配置s3 S3ServiceConf
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class S3ServiceConf {
@Value("${s3.AWS_S3_ACCESS_KEY}")
private String AWS_S3_ACCESS_KEY; //key
@Value("${s3.AWS_S3_SECRET_KEY}") //SECRET
private String AWS_S3_SECRET_KEY;
@Value("${s3.AWS_S3_BUCKET_NAME}") //桶名称
private String AWS_S3_BUCKET_NAME;
@Value("${s3.REGION}")
private String REGION; //区域
//主要使用
@Bean("s3Service")
public S3Service s3Service() {
return new S3Service(AWS_S3_ACCESS_KEY, AWS_S3_SECRET_KEY, AWS_S3_BUCKET_NAME, REGION);
}
}
使用
@Autowired
private S3Service s3Service;
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String fileNameStr = s3Service.getFileNameByType("polymer", "uediter", suffix);
//设置Amazon S3存储的对象元数据
ObjectMetadata om = new ObjectMetadata();
om.setContentLength(file.getSize());
om.setContentType(file.getContentType());
om.setLastModified(new Date());
url = s3Service.uploadFile(fileNameStr, file.getInputStream(), om);