简介
最近公司因为业务需要, 要求将文件服务器迁移到aws s3, 初次接入时碰到了不少坑, 网上大部分链接实现复杂, 且没有详细说明, 故自己研究一番以后出了一版简洁版
什么是S3
官方介绍, 巴拉巴拉一堆, 简单来讲, 就是亚马逊提供的安全,高效,的一个存文件的工具 /邪魅微笑
实现方式
maven导入S3 jar包
<!--[start]亚马逊S3包-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.233</version>
</dependency>
<!--[end]-->
Java代码示例
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 java.io.File;
import java.util.UUID;
/**
* AWS S3 文件操作demo
* @author Administrator
*/
public class UploadFileS3Demo {
/**
* access_key_id 你的亚马逊S3服务器访问密钥ID
*/
private static final String ACCESS_KEY = "xxxxx";
/**
* secret_key 你的亚马逊S3服务器访问密钥
*/
private static final String SECRET_KEY = "xxxxxxxxxxxx";
/**
* end_point 你的亚马逊S3服务器连接路径和端口(新版本不再需要这个,直接在创建S3对象的时候根据桶名和Region自动获取)
*
* 格式: https://桶名.s3-你的Region名称.amazonaws.com
* 示例: https://xxton.s3-cn-north-1.amazonaws.com
*/
// private static final String END_POINT = "https://xxton.s3-cn-north-1.amazonaws.com";
/**
* bucketname 你的亚马逊S3服务器创建的桶名
*/
private static final String BUCKET_NAME = "xxxx";
/**
* 创建访问凭证对象
*/
private static final BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
/**
* 创建s3对象
*/
private static final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
//设置服务器所属地区
.withRegion(Regions.CN_NORTH_1)
.build();
/**
* 上传文件示例
*
* @param uploadPath 上传路径
*/
private static String uploadFile(File file, String uploadPath) {
try {
if (file == null) {
return null;
}
//设置文件目录
if(StringUtils.isNotEmpty(uploadPath)){
uploadPath= "/".equals(uploadPath.substring(uploadPath.length()-1))?uploadPath:uploadPath+"/";
}else{
uploadPath="default/";
}
//生成随机文件名
String expandedName= file.getName().substring(file.getName().lastIndexOf("."));
String key = uploadPath + UUID.randomUUID().toString() +expandedName;
// 设置文件上传对象
PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, key, file);
// 设置公共读取
request.withCannedAcl(CannedAccessControlList.PublicRead);
// 上传文件
PutObjectResult putObjectResult = s3.putObject(request);
if (null != putObjectResult) {
return key;
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取文件下载路径
* @param key 文件的key
* @return
*/
private static String downloadFile(String key){
try {
if(StringUtils.isEmpty(key)){
return null;
}
GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(BUCKET_NAME, key);
return s3.generatePresignedUrl(httpRequest).toString();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args)
{
//上传文件测试
File file = new File("D:\\test.jpg");
String filePath = uploadFile(file, "upload/");
System.out.println("【文件上传结果】:"+filePath);
System.out.println("\n");
//下载文件测试
String downUrl = downloadFile(filePath);
System.out.println("【下载文件路径】:"+downUrl);
}
}
注意点:
1.必须在aws S3服务器先配置好密钥ID ACCESS_KEY 和密钥 SECRET_KEY
2.必须在aws S3服务器先创建好桶 BUCKET_NAME
3.必须写正确你的S3服务器所属的区域
例如: Regions.CN_NORTH_1 代表 中国北京的服务器
4.许多教程分享的使用new AmazonS3Client() 获取连接的方式已经过期
5.欢迎大家指出更多问题!
————————————————
版权声明:本文为CSDN博主「晷谷丶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013456393/article/details/105745587