Home > Mobile >  I need to put_bucket_analytics_configuration
I need to put_bucket_analytics_configuration

Time:04-23

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?

CodePudding user response:

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.

  • Related