I need to put_bucket_analytics_configuration

All we need is an easy explanation of the problem, so here it is.

I need to put_bucket_analytics_configuration to the specific s3 bucket for ex. named "test-bucket". I wrote a python code:

import boto3
client = boto3.client('s3')


response = client.put_bucket_analytics_configuration(
    Bucket='test-bucket',
    Id='storage-class-analysis',
    AnalyticsConfiguration={
        'Id':'storage-class-analysis',
        'Filter': {
            'Prefix' : 'dir',
            'Tag': {
                'Key':'production',
                'Value':'true'
            }
        },
        'StorageClassAnalysis' : {
            'DataExport' : {
                'OutputSchemaVersion':'V_1',
                'Destination' : {
                    'S3BucketDestination': {
                        'Format' : 'CSV',
                        'BucketAccountId': '************',
                        'Bucket' : 'arn:aws:s3:::storage-class-analysis-bucket-logs',
                        'Prefix' : 'dir'
                    }
                }
            }
        }
    },
    ExpectedBucketOwner='************'
)

But I got this error:

Traceback (most recent call last):
  File "C:\Python learning\Natalia\test.py", line 5, in <module>
    response = client.put_bucket_analytics_configuration(
  File "C:\Users\Ant\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\client.py", line 401, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "C:\Users\Ant\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\client.py", line 731, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (MalformedXML) when calling the PutBucketAnalyticsConfiguration operation: The XML you provided was not well-formed or did not validate against our published schema

So I copied the configuration from the "get_bucket_analytics_configuration" output:

"AnalyticsConfiguration":{
   "Id":"string",
   "Filter":{
      "And":{
         "Prefix":"dir",
         "Tags":[
            {
               "Key":"production",
               "Value":"true"
            }
         ]
      }
   },
   "StorageClassAnalysis":{
      "DataExport":{
         "OutputSchemaVersion":"V_1",
         "Destination":{
            "S3BucketDestination":{
               "Format":"CSV",
               "Bucket":"arn:aws:s3:::storage-class-analysis-bucket-logs",
               "Prefix":"dir"
            }
         }
      }
   }
}
}

Could you please tell me why I’m getting such an error.
What exactly I need to fix here?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

This code worked for me:

import boto3
client = boto3.client('s3')


response = client.put_bucket_analytics_configuration(
    Bucket='test-bucket211',
    Id='storage-class-analysis',
    AnalyticsConfiguration={
        'Id':'storage-class-analysis',
        'Filter': {
            'And': {
            'Prefix' : 'dir',
            'Tags':[ {
                'Key':'production',
                'Value':'true'
            } ]
          }
        },
        'StorageClassAnalysis' : {
            'DataExport' : {
                'OutputSchemaVersion':'V_1',
                'Destination' : {
                    'S3BucketDestination': {
                        'Format' : 'CSV',
                        'BucketAccountId': '****',
                        'Bucket' : 'arn:aws:s3:::destination_bucket',
                        'Prefix' : 'dir'
                    }
                }
            }
        }
    },
    ExpectedBucketOwner='*****'
)

Slightly different in that I use And inside the Filter attribute and make Tags an array.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply