Reputation: 2708
I am trying to set the Life cycle rule for my existing bucket in aws. Below is the code that I am using:
IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USWest1);
LifecycleConfiguration newConfiguration = new LifecycleConfiguration
{
Rules = new List<LifecycleRule>
{
new LifecycleRule
{
Expiration = new LifecycleRuleExpiration { Days = 2 },
}
}
};
PutLifecycleConfigurationRequest putRequest = new PutLifecycleConfigurationRequest
{
BucketName = "test-bucket",
Configuration = newConfiguration
};
client.PutLifecycleConfiguration(putRequest);
when I run the above code, I keep getting an error saying :
Amazon.S3.AmazonS3Exception: 'The XML you provided was not well-formed or did not validate against our published schema'
I am not sure what am I doing wrong. I am very new to aws.
Any help will be higly appreciated.
Upvotes: 2
Views: 918
Reputation: 671
Looks like S3 Lifecycle Configuration expects a prefix in PutLifecycleConfigurationRequest. So modifying your request to include a prefix like below should work for you.
var client = new AmazonS3Client(Amazon.RegionEndpoint.USWest1);
var lifecycleConfiguration = new LifecycleConfiguration
{
Rules = new List<LifecycleRule>
{
new LifecycleRule
{
Filter = new LifecycleFilter
{
LifecycleFilterPredicate = new LifecyclePrefixPredicate
{
Prefix = "someprefix/"
}
},
Expiration = new LifecycleRuleExpiration { Days = 2 }
}
}
};
var putLifecycleConfigurationRequest = new PutLifecycleConfigurationRequest
{
BucketName = "bucketName",
Configuration = lifecycleConfiguration
};
await client.PutLifecycleConfigurationAsync(putLifecycleConfigurationRequest);
Lifecycle rule created above will be disabled by default. Which can be enabled by setting Status = LifecycleRuleStatus.Enabled
for LifecycleRule in the request.
And in case if you want the rule to be applicable for entire bucket, you can set the prefix to empty.
Edit : Even though the documentation mentions the filter as optional, I suspect the note mentioned for LifecycleRuleFilter Prefix might be the reason for XML error. (maybe the default value for the prefix has some issues and hence throws the XML error mentioned in the question)
Upvotes: 3