[英]com.amazonaws.services.s3.model.AmazonS3Exception: The XML you provided was not well-formed or did not validate against our published schema
I am trying to delete content from an Amazon S3 bucket using deleteObjects method as below:我正在尝试使用 deleteObjects 方法从 Amazon S3 存储桶中删除内容,如下所示:
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(s3BucketName);
List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>();
//here s3 is the AmazonS3 object , bucketName is String Object & s3Filepath is also a string object.
for (final S3ObjectSummary summary : S3Objects.withPrefix(s3, s3BucketName, s3FilePath)) {
keys.add(new DeleteObjectsRequest.KeyVersion(summary.getKey()));
System.out.println("Deleting the S3 object "+ summary);
}
deleteObjectsRequest.setKeys(keys);
try {
s3.deleteObjects(deleteObjectsRequest);
} catch (Exception e) {
System.out.print("Error when cleaning up S3-data: " + e);
}
Reference has been taken from here: https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-multiple-objects.html参考资料取自此处: https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-multiple-objects.html
Weirdly, i'm getting below error message with catch block execution.奇怪的是,我在执行 catch 块时收到以下错误消息。
com.amazonaws.services.s3.model.AmazonS3Exception: The XML you provided was not well-formed or did not validate against our published schema (Service: Amazon S3; Status Code: 400; Error Code: MalformedXML; Request ID: someRequestID; S3 Extended Request ID: someExtendedRequestId=; Proxy: null)
Full StackTrace:完整的 StackTrace:
com.amazonaws.services.s3.model.AmazonS3Exception: The XML you provided was not well-formed or did not validate against our published schema (Service: Amazon S3; Status Code: 400; Error Code: MalformedXML; Request ID: SomeRequestId; S3 Extended Request ID: SomeExternalRequestId; Proxy: null)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1819) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1403) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1372) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1145) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:550) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:530) ~[AWSJavaClientRuntime-1.11.x.jar:?]
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5437) ~[AWSS3JavaClient-1.11.x.jar:?]
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:5384) ~[AWSS3JavaClient-1.11.x.jar:?]
at com.amazonaws.services.s3.AmazonS3Client.deleteObjects(AmazonS3Client.java:2327) ~[AWSS3JavaClient-1.11.x.jar:?]
Can someone please help here, what wrong i'm doing?有人可以在这里帮忙吗,我做错了什么?
Is this due to size limit of S3 bucket that it can only delete these much number of records.这是由于 S3 存储桶的大小限制,它只能删除这么多记录吗? Because there are 10 files present in S3 & one file can have 1000 records.
因为 S3 中有 10 个文件,一个文件可以有 1000 条记录。
Thanks !谢谢 !
Try moving to the latest Amazon S3 Java V2 API, not the old V1 API.尝试移动到最新的 Amazon S3 Java V2 API,而不是旧的 V1 API。
Here is the latest code example for this use case.这是此用例的最新代码示例。
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/DeleteObjects.java https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/DeleteObjects.java
If you are not familiar with how to work with the Amazon S3 Java V2 API, see:如果您不熟悉如何使用 Amazon S3 Java V2 API,请参阅:
Get started with the AWS SDK for Java 2.x 开始使用适用于 Java 2.x 的 AWS SDK
UPDATE更新
I just tested this V1 code and it worked perfectly:我刚刚测试了这段 V1 代码,它运行良好:
import com.amazonaws.AmazonServiceException;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import java.util.Arrays;
/**
* Delete multiple objects from an Amazon S3 bucket.
*
* This code expects that you have AWS credentials set up per:
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
*
* ++ Warning ++ This code will actually delete the objects that you specify!
*/
public class DeleteObjects {
public static void main(String[] args) {
final String USAGE = "\n" +
"To run this example, supply the name of an S3 bucket and at least\n" +
"one object name (key) to delete.\n" +
"\n" +
"Ex: DeleteObjects <bucketname> <objectname1> [objectname2, ...]\n";
// if (args.length < 2) {
// System.out.println(USAGE);
/// System.exit(1);
// }
String bucket_name = "<bucketName>"; // args[0];
String[] object_keys = {"book.pdf", "JUnit.pdf"}; // objects to delete
System.out.println("Deleting objects from S3 bucket: " + bucket_name);
for (String k : object_keys) {
System.out.println(" * " + k);
}
final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withRegion(Regions.US_EAST_1)
.build();
try {
DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name)
.withKeys(object_keys);
s3.deleteObjects(dor);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
}
S3 delete operation support up to 1000 object deletes, however, I can't see any mechanism in your code to safeguards keys more than 1000 passing to S3. S3 删除操作最多支持 1000 次 object 删除,但是,我在您的代码中看不到任何机制来保护超过 1000 次传递给 S3 的密钥。
Can you print the length of the keys list?你能打印键列表的长度吗? If it going beyond 1000, limit it and pass multiple delete invocations to S3.
如果它超过 1000,限制它并将多个删除调用传递给 S3。
https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.