U
    W¨+dy  ã                   @   s<   d dl Z d dlZd dlmZ d dlmZ G dd„ deƒZdS )é    N)Ú
RawMessage)ÚSQSDecodeErrorc                       sN   e Zd ZdZd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Zdd„ Z‡ fdd„Z	‡  Z
S )Ú
BigMessagea±  
    The BigMessage class provides large payloads (up to 5GB)
    by storing the payload itself in S3 and then placing a reference
    to the S3 object in the actual SQS message payload.

    To create a BigMessage, you should create a BigMessage object
    and pass in a file-like object as the ``body`` param and also
    pass in the an S3 URL specifying the bucket in which to store
    the message body::

        import boto.sqs
        from boto.sqs.bigmessage import BigMessage

        sqs = boto.sqs.connect_to_region('us-west-2')
        queue = sqs.get_queue('myqueue')
        fp = open('/path/to/bigmessage/data')
        msg = BigMessage(queue, fp, 's3://mybucket')
        queue.write(msg)

    Passing in a fully-qualified S3 URL (e.g. s3://mybucket/foo)
    is interpreted to mean that the body of the message is already
    stored in S3 and the that S3 URL is then used directly with no
    content uploaded by BigMessage.
    Nc                    s   || _ tt| ƒ ||¡ d S ©N)Ús3_urlÚsuperr   Ú__init__)ÚselfÚqueueÚbodyr   ©Ú	__class__© ú7/tmp/pip-unpacked-wheel-dlxw5sjy/boto/sqs/bigmessage.pyr   8   s    zBigMessage.__init__c                 C   sf   d  }}|r^|  d¡rP|dd …  dd¡}|d }t|ƒdkr^|d r^|d }nd}t|| ƒ‚||fS )Nzs3://é   ú/é   r   z(s3_url parameter should start with s3://)Ú
startswithÚsplitÚlenr   )r	   r   Úbucket_nameÚkey_nameZs3_componentsÚmsgr   r   r   Ú_get_bucket_key<   s    


zBigMessage._get_bucket_keyc                 C   s`   |   | j¡\}}|r|r| jS t ¡ }t ¡ }| |¡}| |¡}| |¡ d||f | _| jS )a  
        :type value: file-like object
        :param value: A file-like object containing the content
            of the message.  The actual content will be stored
            in S3 and a link to the S3 object will be stored in
            the message body.
        z
s3://%s/%s)	r   r   ÚuuidZuuid4ÚbotoÚ
connect_s3Ú
get_bucketZnew_keyZset_contents_from_file)r	   Úvaluer   r   Ús3_connÚ	s3_bucketÚkeyr   r   r   ÚencodeN   s    


zBigMessage.encodec                 C   sL   |   |¡\}}|r6|r6t ¡ }| |¡}| |¡}|S d| }t|| ƒ‚d S )NzUnable to decode S3 URL: %s)r   r   r   r   Úget_keyr   )r	   r   r   r   r   r    r!   r   r   r   r   Ú_get_s3_objecta   s    

zBigMessage._get_s3_objectc                 C   s   || _ |  |¡}| ¡ S r   )r   r$   Zget_contents_as_string)r	   r   r!   r   r   r   Údecodel   s    
zBigMessage.decodec                    s,   | j r|  | j ¡}| ¡  tt| ƒ ¡  d S r   )r   r$   Údeleter   r   )r	   r!   r   r   r   r&   q   s    zBigMessage.delete)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r$   r%   r&   Ú__classcell__r   r   r   r   r      s   r   )r   r   Zboto.sqs.messager   Zboto.exceptionr   r   r   r   r   r   Ú<module>   s   