在s3client.listObjects()

时间:2018-07-02 14:58:31

标签: amazon-web-services amazon-s3

尝试将对listObjects操作的访问权限添加到存储桶策略中以从s3中读取信息,收到错误:

  

错误操作不适用于语句中的任何资源

{
           "Sid": "AllowListingOfUserFolder",
           "Action": ["s3:ListBucket"],
           "Effect": "Allow",
           "Principal":{"AWS":"arn:aws:iam::<ID>:user/my_user"},
           "Resource": "arn:aws:s3:::outputbucket/*",
           "Condition":{"StringLike":{"s3:prefix":["folder/*"]}}

        }

政策有什么问题?

UPD 完全删除“资源和条件”中的“ / *”,并收到错误:

  

org.xml.sax.SAXParseException:文件过早结束。

           {
               "Sid": "AllowListingOfUserFolder",
               "Action": ["s3:ListBucket"],
               "Effect": "Allow",
               "Principal":{"AWS":"arn:aws:iam::<ID>:user/my_user"},
               "Resource": "arn:aws:s3:::outputbucket"
            }

此代码失败:

ObjectListing objects = s3Client.listObjects(outputBucketName, outputKeyPrefix);
  
    

请求处理失败;嵌套的异常是com.amazonaws.SdkClientException:无法解析具有以下内容的XML文档:     处理程序类     com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser $ ListBucketHandler]     根本原因

  
     

org.xml.sax.SAXParseException:文件的结尾过早。在   org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(未知   来源)〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.util.ErrorHandlerWrapper.fatalError(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.impl.XMLErrorReporter.reportError(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(未知   来源)〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.parsers.XML11Configuration.parse(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.parsers.XML11Configuration.parse(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.parsers.XMLParser.parse(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   org.apache.xerces.parsers.AbstractSAXParser.parse(未知来源)   〜[xercesImpl-2.9.1.jar:na]在   com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.parseXmlInputStream(XmlResponsesSaxParser.java:142)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.parseListBucketObjectsResponse(XmlResponsesSaxParser.java:298)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.model.transform.Unmarshallers $ ListObjectsUnmarshaller.unmarshall(Unmarshallers.java:70)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.model.transform.Unmarshallers $ ListObjectsUnmarshaller.unmarshall(Unmarshallers.java:59)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.internal.S3XmlResponseHandler.handle(S3XmlResponseHandler.java:62)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.internal.S3XmlResponseHandler.handle(S3XmlResponseHandler.java:31)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.http.response.AwsResponseHandlerAdapter.handle(AwsResponseHandlerAdapter.java:70)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.handleResponse(AmazonHttpClient.java:1503)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeOneRequest(AmazonHttpClient.java:1226)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeHelper(AmazonHttpClient.java:1030)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.doExecute(AmazonHttpClient.java:742)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeWithTimer(AmazonHttpClient.java:716)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.execute(AmazonHttpClient.java:699)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutor.access $ 500(AmazonHttpClient.java:667)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient $ RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)   〜[aws-java-sdk-core-1.11.125.jar:na]在   com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4169)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4116)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4110)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:819)   〜[aws-java-sdk-s3-1.11.125.jar:na]在   com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:797)   〜[aws-java-sdk-s3-1.11.125.jar:na]

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。 如https://forums.aws.amazon.com/thread.jspa?threadID=160117中所述,问题出在bucketName上。我使用了bucketName +文件夹的路径,但只不过是存储桶名称

  • com.example.bucketName /文件夹<-错误
  • com.example.bucketName / <-正确

    ListObjectsRequest listObjectsRequest =新的ListObjectsRequest()                     .withBucketName(“ com.example.bucketName /”)                     .withPrefix(“ folder /”)