布景
公司最近的业务很多涉及安可项目,要求防止运用第三方组件,原有开发结构支撑本地文件存储/Minio/各类云存储,现在要求文件独立存储且文件服务需要自研,经调研评估后决定基于SpringBoot开发文件存储服务,运用s3协议规范,这样能够直接运用aws-sdk接入无需再开发客户端,且安全安全性方面能够得到足够的确保(签名验证部分参阅我的博文《Java完成AWS S3 V4 Authorization自定义验证》)
项目地址:gitee.com/code2roc/lo…
运行jar包,默许信息如下
api地址:http://localhost:8001/s3
用户名:admin
暗码:abcd@1234
概述
s3协议无规范阐明文档,为rest风格,创立/删去/概况办法通过PUT/DELETE/HEAD表述
很多办法共用一个路由,通过head参数区分(例如putObject和copyObject)
参阅aws的最新api文档:docs.aws.amazon.com/AmazonS3/la…
完成以下根底功用
-
Bucket创立
@PutMapping("/{bucketName}") public ResponseEntity<String> createBucket(@PathVariable String bucketName)
-
Bucket删去
@DeleteMapping("/{bucketName}") public ResponseEntity<String> deleteBucket(@PathVariable String bucketName)
-
@PutMapping("/{bucketName}/**") public ResponseEntity<String> putObject(@PathVariable String bucketName, HttpServletRequest request)
-
文件删去
@DeleteMapping("/{bucketName}/**") public ResponseEntity<String> deleteObject(@PathVariable String bucketName, HttpServletRequest request)
-
文件下载
@GetMapping("/{bucketName}/**") public void getObject(@PathVariable String bucketName, HttpServletRequest request, HttpServletResponse response)
-
文件分片操作(初始化/分片上传/合并)
@RequestMapping(value = "/{bucketName}/**", method = RequestMethod.POST, params = "uploads") public ResponseEntity<Object> createMultipartUpload(@PathVariable String bucketName, HttpServletRequest request) @RequestMapping(value = "/{bucketName}/**", method = RequestMethod.PUT, params = {"partNumber", "uploadId"}) public ResponseEntity<String> uploadPart(@PathVariable String bucketName, HttpServletRequest request, HttpServletResponse response) @RequestMapping(value = "/{bucketName}/**", method = RequestMethod.POST, params = "uploadId") public ResponseEntity<String> completeMultipartUpload(@PathVariable String bucketName, HttpServletRequest request)
完成以下扩展功用(兼容s3 browser运用)
-
Bucket概况
@RequestMapping(value = "/{bucketName}", method = RequestMethod.HEAD) public ResponseEntity<Object> headBucket(@PathVariable(value = "bucketName") String bucketName)
-
Buckent列表
@GetMapping("/") public ResponseEntity<String> listBuckets()
-
文件概况
@RequestMapping(value = "/{bucketName}/**", method = RequestMethod.HEAD) public ResponseEntity<Object> headObject(@PathVariable String bucketName, HttpServletRequest request, HttpServletResponse response)
-
文件列表
@GetMapping("/{bucketName}") public ResponseEntity<String> listObjects(@PathVariable String bucketName, HttpServletRequest request)
项目接入
maven引用
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.20.45</version>
</dependency>
客户端衔接
private S3Client getClient() {
S3Client s3 = S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(systemConfig.getUsername(), systemConfig.getPassword())))
.endpointOverride(URI.create(CommonUtil.getApiPath() + "s3/"))
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).chunkedEncodingEnabled(false).build())
.region(Region.US_EAST_1)
.build();
return s3;
}
文件操作
public void upload(String bucketName, String key, InputStream inputStream) throws Exception {
S3Client s3Client = getClient();
PutObjectRequest request = PutObjectRequest.builder().bucket(bucketName).key(key).build();
RequestBody requestBody = RequestBody.fromBytes(FileUtil.convertStreamToByte(inputStream));
s3Client.putObject(request, requestBody);
s3Client.close();
}
工具运用
下载工具
链接:pan.baidu.com/s/1HnB3KUOQ… 提取码:nnio
配置衔接
Account type:挑选S3 Compatible Storage
EndPoint填写部署服务后的地址:http://ip:port/s3
Access Key ID:填写配置文件中的username
Secret Access Key:填写配置文件中的password
去除SSL选项
配置签名
在编辑衔接页面点击左下角Advanced S3 Compatible Storage Setting
挑选签名版别为V4
支撑功用
支撑创立桶/删去桶/上传文件/删去文件/下载文件/创立文件夹功用