【问题标题】:What is the best way to implement file upload using spring boot in AWS S3 bucket?在 AWS S3 存储桶中使用 Spring Boot 实现文件上传的最佳方法是什么?
【发布时间】:2018-01-03 13:08:16
【问题描述】:

在 Java Spring boot 应用程序(使用 web starter 的 Web 服务)上工作,我需要将图像存储在 S3 存储桶中,有人可以建议我如何实现吗?

在 EC2 实例中托管应用程序

【问题讨论】:

  • :D 谢谢@ErwinBolwidt

标签: spring amazon-s3 spring-boot-test


【解决方案1】:

您可以将 spring-cloud-aws 与支持 s3 的 ResourceLoader 一起使用:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
   xsi:schemaLocation="http://www.springframework.org/schema/cloud/aws/context http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd">
    <aws-context:context-credentials>
       <!-- ... -->
     </aws-context:context-credentials>
    <aws-context:context-resource-loader/>
</beans>

并将其注入到您的 bean 中:

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());
        }
    }
}

您可以在此处找到文档: http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.2.1.RELEASE/#_uploading_files

【讨论】:

  • 请注意,这不适用于 S3 服务器端加密 (SSE),因为无法修改上传的元数据。因此,此解决方案具有限制性。
【解决方案2】:

终于找到了解决办法 参考下面的博客实现Spring boot AWS s3上传图片和删除图片。

链接Spring boot s3 upload

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多