赞
踩
1.引入pom
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-s3</artifactId>
<version>3.0.3</version>
</dependency>
spring:
cloud:
aws:
credentials:
access-key: xxxxx
secret-key:xxxxx
s3:
endpoint: http://xxxxx
region:
static: us-east-1 #这里必须要填一个不然会报错
下一个示例演示如何使用资源加载器加载不同的资源。
public class SimpleResourceLoadingBean {
@Autowired
private ResourceLoader resourceLoader;
public void resourceLoadingMethod() throws IOException {
Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
Resource secondResource = this.resourceLoader.getResource("s3://myBucket/rootFolder/subFile");
InputStream inputStream = resource.getInputStream();
//read file
}
}
4.上传文件
从 Spring Framework 3.1 开始,资源加载器也可以用来上传带有org.springframework.core.io.WritableResource 接口的文件,这是接口的特殊化org.springframework.core.io.ResourceLoader。客户端可以使用该WritableResource界面上传文件。下一个示例演示使用资源加载器上传资源。
public class SimpleResourceLoadingBean {
@Autowired
private ResourceLoader resourceLoader;
public void writeResource() throws IOException {
Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
WritableResource writableResource = (WritableResource) resource;
try (OutputStream outputStream = writableResource.getOutputStream()) {
outputStream.write("test".getBytes());
}
}
}
5.获取文件具有时效的下载地址
@Autowired
private S3Template s3Template;
@Autowired
private S3Client s3client;
public URL getfileUrl(String bucketName,String path){
return s3Template.createSignedGetURL(bucketName,path,Duration.ofDays(7));
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。