o
    rMfy  ã                   @   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__© úU/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/boto/sqs/bigmessage.pyr   8   s   zBigMessage.__init__c                 C   sl   d  }}|r2|  d¡r+|dd …  dd¡}|d }t|ƒdkr'|d r'|d }||fS d}t|| ƒ‚||fS )Nzs3://é   ú/é   r   z(s3_url parameter should start with s3://)Ú
startswithÚsplitÚlenr   )r	   r   Úbucket_nameÚkey_nameÚ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   ÚuuidÚuuid4ÚbotoÚ
connect_s3Ú
get_bucketÚnew_keyÚset_contents_from_file)r	   Úvaluer   r   Ús3_connÚ	s3_bucketÚkeyr   r   r   ÚencodeN   s   


zBigMessage.encodec                 C   sH   |   |¡\}}|r|rt ¡ }| |¡}| |¡}|S d| }t|| ƒ‚)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(   Ú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   Úboto.sqs.messager   Úboto.exceptionr   r   r   r   r   r   Ú<module>   s
   