from boto.sqs.message import Message
import json
from s3_notification_info import S3NotificationInfo


class S3SQSMessage(Message):
    def __init__(self, queue=None, body='', xml_attrs=None):
        Message.__init__(self, queue, body)
        self.payload = None
        self.notification_type = 'S3Info'

    def event_name(self):
        return self.payload['Records'][0]['eventName']

    def event_time(self):
        return self.payload['Records'][0]['eventTime']

    def bucket_name(self):
        return self.payload['Records'][0]['s3']['bucket']['name']

    def file_name(self):
        return self.payload['Records'][0]['s3']['object']['key']

    def file_etag(self):
        return self.payload['Records'][0]['s3']['object']['eTag']

    def file_size(self):
        return self.payload['Records'][0]['s3']['object']['size']

    def set_body(self, body):
        """
        Override set_body to construct json payload
        Note Boto JSONMessage seemed to have encoding issues with S3 notification messages
        """
        if body is not None and len(body) > 0:
            self.payload = json.loads(body)
        if body and 'Records' in self.payload.keys():
            self.notification_type = 'S3Event'
        super(Message, self).set_body(body)

from boto.sqs.message import Message
import json
from s3_notification_info import S3NotificationInfo


class S3SQSMessage(Message):
    def __init__(self, queue=None, body='', xml_attrs=None):
        Message.__init__(self, queue, body)
        self.payload = None
        self.notification_type = 'S3Info'

    def event_name(self):
        return self.payload['Records'][0]['eventName']

    def event_time(self):
        return self.payload['Records'][0]['eventTime']

    def bucket_name(self):
        return self.payload['Records'][0]['s3']['bucket']['name']

    def file_name(self):
        return self.payload['Records'][0]['s3']['object']['key']

    def file_etag(self):
        if 'eTag' in self.payload['Records'][0]['s3']['object']:
            return self.payload['Records'][0]['s3']['object']['eTag']
        else:
            return None

    def file_size(self):
        return self.payload['Records'][0]['s3']['object']['size']

    def set_body(self, body):
        """
        Override set_body to construct json payload
        Note Boto JSONMessage seemed to have encoding issues with S3 notification messages
        """
        if body is not None and len(body) > 0:
            self.payload = json.loads(body)
        if body and 'Records' in self.payload.keys():
            self.notification_type = 'S3Event'
        super(Message, self).set_body(body)

Tweak for when SQS message is missing the eTag from a bucket notification.
Tweak for when SQS message is missing the eTag from a bucket notification.

  from boto.sqs.message import Message
  import json
  from s3_notification_info import S3NotificationInfo
  
  
  class S3SQSMessage(Message):
      def __init__(self, queue=None, body='', xml_attrs=None):
          Message.__init__(self, queue, body)
          self.payload = None
          self.notification_type = 'S3Info'
  
      def event_name(self):
          return self.payload['Records'][0]['eventName']
  
      def event_time(self):
          return self.payload['Records'][0]['eventTime']
  
      def bucket_name(self):
          return self.payload['Records'][0]['s3']['bucket']['name']
  
      def file_name(self):
          return self.payload['Records'][0]['s3']['object']['key']
  
      def file_etag(self):
+         if 'eTag' in self.payload['Records'][0]['s3']['object']:
-         return self.payload['Records'][0]['s3']['object']['eTag']
+             return self.payload['Records'][0]['s3']['object']['eTag']
+         else:
+             return None
  
      def file_size(self):
          return self.payload['Records'][0]['s3']['object']['size']
  
      def set_body(self, body):
          """
          Override set_body to construct json payload
          Note Boto JSONMessage seemed to have encoding issues with S3 notification messages
          """
          if body is not None and len(body) > 0:
              self.payload = json.loads(body)
          if body and 'Records' in self.payload.keys():
              self.notification_type = 'S3Event'
          super(Message, self).set_body(body)
  
Tweak for when SQS message is missing the eTag from a bucket notification.
## Code Before:
from boto.sqs.message import Message
import json
from s3_notification_info import S3NotificationInfo


class S3SQSMessage(Message):
    def __init__(self, queue=None, body='', xml_attrs=None):
        Message.__init__(self, queue, body)
        self.payload = None
        self.notification_type = 'S3Info'

    def event_name(self):
        return self.payload['Records'][0]['eventName']

    def event_time(self):
        return self.payload['Records'][0]['eventTime']

    def bucket_name(self):
        return self.payload['Records'][0]['s3']['bucket']['name']

    def file_name(self):
        return self.payload['Records'][0]['s3']['object']['key']

    def file_etag(self):
        return self.payload['Records'][0]['s3']['object']['eTag']

    def file_size(self):
        return self.payload['Records'][0]['s3']['object']['size']

    def set_body(self, body):
        """
        Override set_body to construct json payload
        Note Boto JSONMessage seemed to have encoding issues with S3 notification messages
        """
        if body is not None and len(body) > 0:
            self.payload = json.loads(body)
        if body and 'Records' in self.payload.keys():
            self.notification_type = 'S3Event'
        super(Message, self).set_body(body)

## Instruction:
Tweak for when SQS message is missing the eTag from a bucket notification.
## Code After:
from boto.sqs.message import Message
import json
from s3_notification_info import S3NotificationInfo


class S3SQSMessage(Message):
    def __init__(self, queue=None, body='', xml_attrs=None):
        Message.__init__(self, queue, body)
        self.payload = None
        self.notification_type = 'S3Info'

    def event_name(self):
        return self.payload['Records'][0]['eventName']

    def event_time(self):
        return self.payload['Records'][0]['eventTime']

    def bucket_name(self):
        return self.payload['Records'][0]['s3']['bucket']['name']

    def file_name(self):
        return self.payload['Records'][0]['s3']['object']['key']

    def file_etag(self):
        if 'eTag' in self.payload['Records'][0]['s3']['object']:
            return self.payload['Records'][0]['s3']['object']['eTag']
        else:
            return None

    def file_size(self):
        return self.payload['Records'][0]['s3']['object']['size']

    def set_body(self, body):
        """
        Override set_body to construct json payload
        Note Boto JSONMessage seemed to have encoding issues with S3 notification messages
        """
        if body is not None and len(body) > 0:
            self.payload = json.loads(body)
        if body and 'Records' in self.payload.keys():
            self.notification_type = 'S3Event'
        super(Message, self).set_body(body)
