简体   繁体   English

尝试 listObjects 时,AmazonS3 客户端 org.xml.sax.SAXParseException

[英]AmazonS3 client org.xml.sax.SAXParseException when trying to listObjects

I'm having some drama when running the listObjects(..) method of AmazonS3.在运行 AmazonS3 的 listObjects(..) 方法时,我遇到了一些问题。 I'm certain that my credentials are set up correctly as I am able to download individual files using s3Client.getObject(..).我确信我的凭据设置正确,因为我能够使用 s3Client.getObject(..) 下载单个文件。 The logs read::日志读取::

com.amazonaws.SdkClientException: Failed to parse XML document with handler class com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser$ListObjectsV2Handler Caused by: org.xml.sax.SAXParseException: Premature end of file.

I understand that listObjects(..) does include in it's response some xml containing meta data.我知道 listObjects(..) 确实在它的响应中包含了一些包含元数据的 xml。 The code to reproduce the error is very simple.重现错误的代码非常简单。 I can't see anything wrong here :(我看不出这里有什么问题:(

ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(ENV.getProperty("cloud.aws.s3.bucket")); ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName(ENV.getProperty("cloud.aws.s3.bucket"));

ObjectListing objectListing = amazonS3Client.listObjects(listObjectsRequest); ObjectListing objectListing = amazonS3Client.listObjects(listObjectsRequest);

Here is the version of spring-cloud-aws-context I am using:: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-aws-context</artifactId <version>1.2.1.RELEASE</version> </dependency>这是我正在使用的 spring-cloud-aws-context 版本: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-aws-context</artifactId <version>1.2.1.RELEASE</version> </dependency>

Does anybody have any insight?有人有任何见解吗? Or know away around this issue?或者知道这个问题?

Thanks in advance :)提前致谢:)

I encountered the exact exception *Failed to parse XML document with handler class * and the failure is not truly descriptive.我遇到了确切的异常*Failed to parse XML document with handler class * 并且失败并不是真正的描述性的。 But my problem was not permissions but rather trying to list the bucket subfolder directly.但我的问题不是权限,而是试图直接列出存储桶子文件夹。

I was trying to listObjects from /bucketName/subFolder/subFolder2 instead of just /bucketName and prefix.我试图从 /bucketName/subFolder/subFolder2 中列出对象,而不仅仅是 /bucketName 和前缀。

This results in exception above (In Scala):这导致上面的异常(在 Scala 中):

val path = "/bucketName/myFolder/subFolder"
val results = s3Client.listObjectsV2(path)

I needed to separate the bucket name and the prefix and then use ListObjectRequestV2我需要将存储桶名称和前缀分开,然后使用 ListObjectRequestV2

val path = "/bucketName/myFolder/subFolder"
val bucketName = "bucketName"
val prefix = "myFolder/subFolder"
val listObjectsRequest = new 
val ListObjectsV2Request().withBucketName(bucketName).withPrefix(prefix) 
val results = s3Client.listObjectsV2(path)

Ok, I found the solution!好的,我找到了解决方案! The problem was with the permissions, apparently listObjects(..) requires it's own set of permissions.问题在于权限,显然 listObjects(..) 需要它自己的一组权限。 Specifically the 'ListBucket' action must be enabled.具体来说,必须启用“ListBucket”操作。

{
    "Version": "2012-10-17",
    "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": ["arn:aws:s3:::test"]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:DeleteObject"
      ],
      "Resource": ["arn:aws:s3:::test/*"]
    }
  ]
}

Another issue was with how the name that I gave to my bucket.另一个问题是我给我的存储桶命名的方式。 I used mybucket/some/prefix/before/files .我使用了mybucket/some/prefix/before/files I corrected this to mybucket .我将此更正到mybucket The prefix is only used with getObject(..) like this:前缀只与 getObject(..) 一起使用,如下所示:

GetObjectRequest getObjectRequest = new 
GetObjectRequest("mybucket/some/prefix/before/files", key);
S3Object s3Object = amazonS3Client.getObject(getObjectRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM