微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot实现上传文件到AWS S3的代码

这篇文章主要介绍了SpringBoot实现上传文件到AWS S3的代码,帮助大家更好的理解和使用springboot框架,感兴趣的朋友可以了解下

简单记录一下在Springboot中上传文件到AWS S3存储服务的代码

在 application.xml中添加aws相关配置:

custom:   aws:     access-key: CHOBitacCESSKEY     secret-key: CHOBIT/THISIS006SECRET007Key/dotORG     bucket: zhyea     endpoint: www.zhyea.com:80

新建一个 AwsS3Componment类来执行上传文件操作:

@Component public class AwsS3Component implements InitializingBean {       @Value("${custom.aws.access-key}")     private String accessKey;       @Value("${custom.aws.secret-key}")     private String accessSecret;       @Value("${custom.aws.bucket}")     private String bucket;       @Value("${custom.aws.endpoint}")     private String endpoint;       private AmazonS3 client;         @Override     public void afterPropertiesSet() {         ClientConfiguration config = new ClientConfiguration();         config.setProtocol(Protocol.HTTP);         config.disableSocketProxy();           this.client = AmazonS3ClientBuilder                 .standard()                 .withClientConfiguration(config)                 .withCredentials(new AWsstaticCredentialsProvider(new BasicAWSCredentials(accessKey, accessSecret)))                 .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, Regions.CN_norTH_1.getName()))                 .enablePathStyleAccess()                 .build();     } }

因为使用的服务有设置endpoint,所以这里需要使用下面这一行完成endpoint的设置:

withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, Regions.CN_norTH_1.getName()))

如果不设置endpoint就会收到下面这样的报错:

com.amazonaws.services.s3.model.AmazonS3Exception: The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID: FRDT8N0RAQFNCVDP; S3 Extended Request ID: DemEatwroXry2YN/5lyuMKDmhIi/aIz3QZPmLN0DYHeHU3oGUeOClJBcToz1J1qkcBZBfklRNs8=) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1660) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1324) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1074) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:745) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:719) at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:701)

异常信息提示了AccessKey无效――虽然我的AccessKey是有效的。

在endpoint的这行配置中还设置了region信息。如果不需要设置endpoint,就得补上region的配置:

this.client = AmazonS3ClientBuilder         .standard()         .withClientConfiguration(config)         .withCredentials(new AWsstaticCredentialsProvider(new BasicAWSCredentials(accessKey, accessSecret)))         .withRegion(Regions.CN_norTH_1) //Region配置         .enablePathStyleAccess()         .build();

下面是执行上传代码

/** * 执行文件上传 * * @param file 要上传文件的路径 * @param key  存储文件的路径 * @return 文件路径 */ private String upload(File file, String key) {     client.putObject(new PutObjectRequest(bucket, key, file).withCannedAcl(CannedAccessControlList.PublicRead));     GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucket, key);     URL url = client.generatePresignedUrl(urlRequest);     return url.toString(); }

这里是通过 File实例执行的上传。有时候会需要直接通过文件流执行上传,此时可以使用下面的代码

private String upload(InputStream input, String key) throws IOException {     Date expireDate = new Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(30));     ObjectMetadata Metadata = new ObjectMetadata();     Metadata.setHttpExpiresDate(expireDate);     Metadata.setContentLength(input.available());       client.putObject(new PutObjectRequest(bucket, key, input, Metadata).withCannedAcl(CannedAccessControlList.PublicRead));     GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucket, key);     URL url = client.generatePresignedUrl(urlRequest);     return url.toString(); }

注意这里的 setContentLength()最好配置一下。不设置会在处理的时候给出WARN。根据方法文档也可以看到,如果不设置,在上传的时候就会先在内存中缓存整个信息流来计算文件长度。

大体上就是这样了。

End!

以上就是SpringBoot实现上传文件到AWS S3的代码的详细内容,更多关于SpringBoot 上传文件的资料请关注编程之家其它相关文章

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐