java Spring Boot 和 Amazon AWS - 如何使用 Spring Cloud AWS 连接到 S3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31633552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:54:20  来源:igfitidea点击:

Spring Boot and Amazon AWS - how to connect to S3 using Spring Cloud AWS?

javaspringamazon-web-servicesamazon-s3spring-boot

提问by Brian

I'm pretty new to Spring in general, as well as Amazon AWS, so I apologize in advance if I'm doing something horribly wrong.

总的来说,我对 Spring 以及 Amazon AWS 还是很陌生,所以如果我做错了什么,我提前道歉。

I can not seem to get my Spring Boot application to communicate with AWS services in any way when deployed onto Elastic Beanstalk. I've been following the guide hereto try to set my project up correctly.

当部署到 Elastic Beanstalk 时,我似乎无法让我的 Spring Boot 应用程序以任何方式与 AWS 服务进行通信。我一直在按照此处的指南尝试正确设置我的项目。

I'm essentially taking the code they provide and attempting to run from my project:

我基本上采用了他们提供的代码并尝试从我的项目中运行:

@Component
public class Storage {

    @Autowired
    private ResourceLoader resourceLoader;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    public Storage() {

    }

    public void writeResource() throws IOException {
        Resource resource = this.resourceLoader.getResource("s3://server-images/rootFile.log");
        WritableResource writableResource = (WritableResource) resource;
        try (OutputStream outputStream = writableResource.getOutputStream()) {
            outputStream.write("test".getBytes());
        }
    }
}

Super, seems simple enough. I also set my application.properties file:

超级,看起来很简单。我还设置了我的 application.properties 文件:

cloud.aws.credentials.accessKey = A******A
cloud.aws.credentials.secretKey = i******K

cloud.aws.credentials.instanceProfile = true
cloud.aws.region.auto = true

so I set the key and secret in the profile, and then lastly, I add the stuff to the build.gradle file to get the Spring Cloud stuff:

所以我在配置文件中设置了密钥和秘密,然后最后,我将这些东西添加到 build.gradle 文件中以获取 Spring Cloud 的东西:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.4.RELEASE")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: "io.spring.dependency-management"

war {
    baseName = 'server'
    // version = '0.0.1'
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-aws:1.0.2.RELEASE'
    }
}

//jar {
//    baseName = 'gs-spring-boot'
//    version =  '0.1.0'
//}

repositories {
    mavenCentral()
    jcenter()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

configurations {
    providedRuntime
}

dependencies {
    // tag::jetty[]
    // compile("org.springframework.boot:spring-boot-starter-web") {
    //    exclude module: "spring-boot-starter-tomcat"
    //}
    compile("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

    // Spring Cloud AWS
    compile 'org.springframework.cloud:spring-cloud-starter-aws'

    //compile("org.springframework.boot:spring-boot-starter-jetty")
    // end::jetty[]
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    // end::actuator[]
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.4'
}

Lastly, I don't know how much this matters, but I gave "AmazonS3FullAccess" and "AmazonEC2FullAccess" to the elasticbeanstalk role in AWS. I also set permissions for "Everyone" to list/upload/delete.

最后,我不知道这有多重要,但我将“AmazonS3FullAccess”和“AmazonEC2FullAccess”赋予了 AWS 中的 elasticbeanstalk 角色。我还为“所有人”设置了列出/上传/删除的权限。

So far, All I get is:

到目前为止,我得到的只是:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:295)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:68)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113)
    at org.springframework.boot.context.web.ErrorPageFilter.access
    {
        "Sid": "RequiredEncryptedPutObject",
        "Effect": "Deny",
        "Principal": "*",
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::my-bucket/*",
        "Condition": {
            "StringNotEquals": {
                "s3:x-amz-server-side-encryption": [
                    "AES256",
                    "aws:kms"
                ]
            }
        }
    },
0(ErrorPageFilter.java:59) at org.springframework.boot.context.web.ErrorPageFilter.doFilterInternal(ErrorPageFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:106) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:673) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NullPointerException: null at app.data.Storage.readTerminator(Storage.java:44) at app.controllers.CodeController.getCode(CodeController.java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) ... 54 common frames omitted

As I can see, the resourceLoader isn't getting anything

正如我所看到的,resourceLoader 没有得到任何东西

Maybe there is some permission problems? Maybe I have misconfigured it? I figure this shouldn't be this hard, so I'm definitely doing something wrong! If anyone has any ideas, they are more than welcome.

也许有一些权限问题?也许我配置错误?我想这不应该这么难,所以我肯定做错了什么!如果有人有任何想法,我们非常欢迎。

回答by Vladimir Zenkevich

Brian.

布莱恩。

If the cause is wrong permissions you should check if your user(you created in IAM) has corresponding permissions(Policies):

如果原因是权限错误,您应该检查您的用户(您在 IAM 中创建的)是否具有相应的权限(策略):

AmazonS3FullAccess, AWSConnector

AmazonS3FullAccess、AWSConnector

Hope this will help.

希望这会有所帮助。

回答by Rash

Check if your bucket policy allows you to put un-encrypted objects. If this is not the reason, there may be other reasons why AWS S3 would not allow your objects to be uploaded. When you use the native AWS library, they set a lot of defaults, but spring libraries may not be setting those defaults.

检查您的存储桶策略是否允许您放置未加密的对象。如果这不是原因,则可能有其他原因导致 AWS S3 不允许上传您的对象。当您使用原生 AWS 库时,它们会设置很多默认值,但 Spring 库可能不会设置这些默认值。

I was getting the same error, until I figured out that my bucket policy was blocking any un-encrypted objects from being uploaded.

我遇到了同样的错误,直到我发现我的存储桶策略阻止上传任何未加密的对象。

##代码##