Reputation: 2247
I'm trying to add a new set of tags to an S3 bucket using the AWS Java API, but I'm getting errors like this:
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: 5B3401CBDB133A88), S3 Extended Request ID: z1EDhMH5vN7o/9Tk93K5R1gWmmqUr49WjEz2rovD9HRCGJ54yHBfoTuUURvnpoizlCUK3Fy9qbY=
This is the code I have written (newTags
is a Map<String, String>
):
List<TagSet> tags = amazonS3.getBucketTaggingConfiguration(bucketName).getAllTagSets();
tags.add(new TagSet(newTags));
s3Service.setBucketTaggingConfiguration(bucketName, new BucketTaggingConfiguration(tags));
I'm not even using XML myself, and the error is unhelpful as to what is actually going wrong. What is happening?
Upvotes: 2
Views: 694
Reputation: 21
Ok finally figured this out but here is my code for how I got multiple tags placed on an S3 bucket. Although the code implies I can add multiple tagSets but it seems to fail with the above error if I do so. I just created a single TagSet with mu
public void TagS3(String bucketName, Map<String,String> tags){
if(0 < tags.size()){
TagSet ts = new TagSet(tags);
BucketTaggingConfiguration btconfig = new BucketTaggingConfiguration();
btconfig.withTagSets(ts);
SetBucketTaggingConfigurationRequest tgRq = new SetBucketTaggingConfigurationRequest(bucketName, btconfig);
s3client.setBucketTaggingConfiguration(tgRq);
}
}
Upvotes: 1
Reputation: 2247
There's a small mismatch between the Java API and the REST API. Have a look at the PUT Bucket Tag request. It's not explicitly stated, but it is implied that you can only have one TagSet
per Tagging
collection. BucketTaggingConfiguration, however, allows a list of TagSets.
Instead of adding a new TagSet, get the Map<String, String>
from the first TagSet in the configuration, add your tags to that map, then create a new TagSet from that. Treat BucketTaggingConfiguration as though it only allows a single-item list of TagSets.
Upvotes: 2