2017-08-23 119 views
0


我是新來的AWS,
我使用AWS CLI在本地配置我的AWS憑據,因爲我不能有一個IAM角色連接到我的筆記本電腦,
通過Eclipse IDE的AWS工具包插件,我可以看到我的憑據已正確配置。
我提到這個答案 - AWS S3 upload without access and secret key in Java
沒有訪問鍵AWS S3的Java文件上傳/憑證

有人可以幫助我瞭解如何我的代碼,該區域中,S3桶在於設置?
或者它是否從aws configure命令期間的設置中獲取區域?
當我嘗試使用s3client.setRegion方法時出現錯誤。

我不能在本地測試這個代碼,它扔我下面的錯誤 -

com.amazonaws.SdkClientException: Unable to load credentials from service endpoint 

以下是我的代碼上傳文件到AWS S3 -

AmazonS3 s3client = AmazonS3ClientBuilder.standard() 
         .withCredentials(new InstanceProfileCredentialsProvider(false)) 
         .build(); 
//s3client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.EU_CENTRAL_1)); 
PutObjectResult result = s3client.putObject(new PutObjectRequest(BUCKET_NAME, BASE_PATH + localFile.getName(), localFile)); 

完整的錯誤日誌 -

The legacy profile format requires the 'profile ' prefix before the profile name. The latest code does not require such prefix, and will consider it as part of the profile name. Please remove the prefix if you are seeing this warning. 
com.amazonaws.SdkClientException: Unable to load credentials from service endpoint 
    at com.amazonaws.auth.EC2CredentialsFetcher.handleError(EC2CredentialsFetcher.java:180) 
    at com.amazonaws.auth.EC2CredentialsFetcher.fetchCredentials(EC2CredentialsFetcher.java:159) 
    at com.amazonaws.auth.EC2CredentialsFetcher.getCredentials(EC2CredentialsFetcher.java:82) 
    at com.amazonaws.auth.InstanceProfileCredentialsProvider.getCredentials(InstanceProfileCredentialsProvider.java:141) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.getCredentialsFromContext(AmazonHttpClient.java:1118) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.runBeforeRequestHandlers(AmazonHttpClient.java:758) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:722) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:715) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:697) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:665) 
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:647) 
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:511) 
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4227) 
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4174) 
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1722) 
    at com.atrium.crud.service.PedestrianServiceImpl.savePedestrianSurvey(PedestrianServiceImpl.java:73) 
    at com.atrium.crud.controller.PedestrianController.savePedestrianSurvey(PedestrianController.java:69) 
    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:498) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:748) 
Caused by: java.net.SocketTimeoutException: connect timed out 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175) 
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) 
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) 
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) 
    at sun.net.www.http.HttpClient.New(HttpClient.java:339) 
    at sun.net.www.http.HttpClient.New(HttpClient.java:357) 
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202) 
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138) 
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032) 
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966) 
    at com.amazonaws.internal.ConnectionUtils.connectToEndpoint(ConnectionUtils.java:47) 
    at com.amazonaws.internal.EC2CredentialsUtils.readResource(EC2CredentialsUtils.java:106) 
    at com.amazonaws.internal.EC2CredentialsUtils.readResource(EC2CredentialsUtils.java:77) 
    at com.amazonaws.auth.InstanceProfileCredentialsProvider$InstanceMetadataCredentialsEndpointProvider.getCredentialsEndpoint(InstanceProfileCredentialsProvider.java:156) 
    at com.amazonaws.auth.EC2CredentialsFetcher.fetchCredentials(EC2CredentialsFetcher.java:121) 
    ... 69 more 

回答

1

您可以使用:

AmazonS3Client amazons3 = new AmazonS3Client(new ProfileCredentialsProvider()); 

ProfileCredentialsProvider會發現隱藏的文件夾.aws &您的主目錄中的文件憑據。

1

試試這個:

s3Client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); 

確保您aws.properties在類路徑中(其中有以下鍵)

accessKey=AKIAIB6FA52IMGLREIIB 
secretKey=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 
+0

當我做了AWS的配置和設置我的creds,他們得到了保存在〜/ .aws/config中的位置,我認爲,我發現,當我開除下面的命令 - AWS配置列表中,那是正確的位置? –

+0

這是正確的。這些證書將在使用AWS CLI時使用。如果您想從Java代碼訪問服務,這種方式可以正常工作。 – Deepak

1

你需要在你的建設者語句插入該地區e.g

AmazonS3 s3client = AmazonS3ClientBuilder.standard() 
     .withCredentials(newInstanceProfileCredentialsProvider(false)) 
     .withRegion("eu_central_1") 
     .build(); 
1

一個更好的解決辦法是使用一個憑據提供鏈。這樣,您的應用程序就可以在生產環境中使用實例配置文件憑據並返回到開發計算機上的配置文件憑據

例如:

AWSCredentialsProviderChain providerChain = new AWSCredentialsProviderChain(
      InstanceProfileCredentialsProvider.getInstance(), 
      new ProfileCredentialsProvider() 
    ); 

AmazonS3 s3client = AmazonS3ClientBuilder.standard() 
        .withCredentials(providerChain) 
        .build(); 

這將首先嚐試實例配置文件可以訪問並回落到檔憑證。只要他們實施the AWSCredentialsProvider interface,您可以隨意添加任意數量的提供商。

或者,使用default provider chain將嘗試按以下順序提供商:

  • 環境變量 - AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY(推薦,因爲它們是由所有AWS SDK和除CLI認可。NET),或AWS_ACCESS_KEYAWS_SECRET_KEY(僅在Java SDK識別)
  • Java系統屬性 - aws.accessKeyIdaws.secretKey
  • 憑據配置文件的文件在默認位置(~/.aws/credentials)所有AWS的SDK和AWS CLI
  • 憑據共享通過亞馬遜EC2集裝箱服務交付,如果AWS_CONTAINER_CREDENTIALS_RELATIVE_URI環境變量設置和安全管理有權限訪問變量,通過亞馬遜EC2元數據服務
交付
  • 實例檔憑證

    Source