
    tni,                     h   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZ ddlmZ ddlmZ e
rddlmZ ddlmZ  ej        d          Zd	Z ej        ej        e ej                    g
           d Z	 	 	 d!dedee         dee         dedef
dZd Z	 d"dee          deed         ed         f         fdZ!	 	 	 d#dee         fdZ"d Z#d Z$	 	 	 	 d$dededee          dee         dee         dee          defdZ%	 	 	 d%dededee          dee         dee         defd Z&dS )&z! PodWorker | modules | upload.py     N)TYPE_CHECKINGOptionalTuple)urlparse)tqdmTransferConfig)
BaseClientzrunpod upload utilityz4%(filename)-20s:%(lineno)-4d %(asctime)s %(message)s)levelformathandlersc                  v    	 ddl m}  ddlm} ddlm} | ||fS # t          $ r}t          d          |d}~ww xY w)zu
    Lazy-load boto3 dependencies.
    Returns tuple of (session, TransferConfig, Config) or raises ImportError.
    r   )sessionr   )ConfigzNboto3 is required for S3 upload functionality. Install with: pip install boto3N)boto3r   boto3.s3.transferr	   botocore.configr   ImportError)r   r	   r   es       ^/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/runpod/serverless/utils/rp_upload.py_import_boto3_dependenciesr      s    
	!!!!!!444444******..   .
 
 	s    
838local_upload	file_namesource_path	file_data	directoryreturnc                 J   t                               d| d           t          j        |d           | d|  }|rt	          j        ||           nO|>t          |d          5 }|                    |           ddd           n# 1 swxY w Y   nt          d          |S )	a  
    Save file to local directory as fallback when S3 is unavailable.

    Args:
        file_name: Name of the file to save
        source_path: Path to source file to copy (for file-based uploads)
        file_data: Bytes to write (for in-memory uploads)
        directory: Local directory to save to (default: 'local_upload')

    Returns:
        Path to the saved local file
    z/No bucket endpoint set, saving to disk folder 'z'. If this is a live endpoint, please reference: https://github.com/runpod/runpod-python/blob/main/docs/serverless/utils/rp_upload.mdT)exist_ok/Nwbz0Either source_path or file_data must be provided)	loggerwarningosmakedirsshutilcopyfileopenwrite
ValueError)r   r   r   r   local_upload_locationfile_outputs         r   _save_to_local_fallbackr-   ,   s   $ NN	_) 	_ 	_ 	_   K	D))))(66966 M%:;;;;		'.. 	)+i(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) KLLL  s   &BBBc                 0   t          |           }d| v r4|                     d          d                             d          d         S |j                            d          r4|                     d          d                             d          d         S dS )z4
    Extracts the region from the endpoint URL.
    z.s3.   .r   z.digitaloceanspaces.comN)r   splitnetlocendswith)endpoint_url
parsed_urls     r   extract_region_from_urlr6   R   s     ,''J!!&))!,22377:: !!";<< N!!#&&q)//0IJJ1MM4    bucket_credsr
   r	   c                 l   	 t                      \  }}}n+# t          $ r t                              d           Y dS w xY w|                                } |dddd          } |dt          j                    dd	
          }| r| d         }| d         }| d         }	n`t          j        	                    dd          }t          j        	                    dd          }t          j        	                    dd          }	|r/|r-|	r+t          |          }
|                    d|||	||
          }nd}||fS )zx
    Returns a boto3 client and transfer config for the bucket.
    Lazy-loads boto3 to reduce initial import time.
    zVboto3 not installed. S3 upload functionality disabled. Install with: pip install boto3)NNs3v4   standardmax_attemptsmodesignature_versionretriesi d  T)multipart_thresholdmax_concurrencymultipart_chunksizeuse_threadsendpointUrlaccessIdaccessSecretBUCKET_ENDPOINT_URLNBUCKET_ACCESS_KEY_IDBUCKET_SECRET_ACCESS_KEYs3)r4   aws_access_key_idaws_secret_access_keyconfigregion_name)r   r   r"   r#   Sessionmultiprocessing	cpu_countr$   environgetr6   client)r8   r   r	   r   bucket_sessionboto_configtransfer_configr4   access_key_idsecret_access_keyregionboto_clients               r   get_boto_clientr_   c   s   *D*F*F'   .	
 	
 	
 zz __&&N& 1j*Q*Q  K %n%'133%	  O  M#M2$Z0(8z~~&;TBB
'=tDDJNN+EtLL  *; (66$++%+"3 , 
 
 ''s    $==bucket_namec                 v   t          t          j                              dd         }t                      \  }}t          j                            |          d         }d|                    d          z   }	t          |d          5 }
|
	                                }ddd           n# 1 swxY w Y   |!| | }t          ||d          }||||<   |S |r|nt          j        d	          }|                    | |  d
| | ||	           |                    d| |  d
| | dd          }||||<   |S )z1
    Upload a single file to bucket storage.
    N   r/   zimage/r0   rbsimulated_uploaded)r   r   %m-%yr    )BucketKeyBodyContentType
get_objectrf   rg   :	 Params	ExpiresIn)struuiduuid4r_   r$   pathsplitextlstripr(   readr-   timestrftime
put_objectgenerate_presigned_url)job_idimage_locationresult_indexresults_listr`   
image_namer^   _file_extensioncontent_type
input_fileoutputfile_name_with_extsim_upload_locationbucketpresigned_urls                   r   upload_imager      s    TZ\\""2A2&J$&&NKW%%n55a8Nn33C888L	nd	#	# #z""# # # # # # # # # # # # # # #  *<N<<5*
 
 
 #)<L&""'C[[T]7-C-CF{44
4N44 	      66#+.V.V*.Vn.V.VWW 7  M %2\"s   B((B,/B,c                    g }dgt          |          z  }t          |          D ]M\  }}t          j        t          | |||f          }|                                 |                    |           N|D ]}|                                 |S )z}
    Uploads a list of files in parallel.
    Once all files are uploaded, the function returns the presigned URLs list.
    N)targetargs)len	enumerate	threadingThreadr   startappendjoin)r{   	file_listupload_progress	file_urlsindexselected_file
new_uploaduploads           r   filesr      s    
 OY'I )) 4 4 + +}%v}eY&O
 
 

 	z**** "  r7   c           
      6   	 t                      \  }}}n)# t          $ r t                              d            w xY w|                                } |dddd          }|                    d|d         |d	         |d
         |          }g }	|D ]}
t          |
d          5 }|                    t          |d                   |  d|
 |           ddd           n# 1 swxY w Y   |		                    |d          d|d          d|  d|
            |	S )z*
    Uploads files to bucket storage.
    zPboto3 not installed. Cannot upload to S3 bucket. Install with: pip install boto3r:   r;   r<   r=   r@   rM   rG   rH   rI   )r4   rN   rO   rP   rc   
bucketNamer    )rf   rg   rh   N)
r   r   r"   errorrR   rW   r(   ry   rp   r   )r{   r   r8   r   r   r   temp_bucket_sessiontemp_boto_configtemp_boto_clientbucket_urlsr   r   s               r   bucket_uploadr      s   799FF   .	
 	
 	
 	 "//++v 1j*Q*Q   +11!-0&z2*>: 2   K" 

 

-&& 	)''<566//// (   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	M*bb\,-Gbb&bbS`bb	
 	
 	
 	
 s    &;$1C!!C%	(C%	file_locationprefix
extra_argsc                    t          |          \  }}|st          j        d          }|r| d|  n| }|t          | |          S t          j                            |          }	t          |	dd|           5 }
|||||
j        d}|r||d	<    |j	        di | ddd           n# 1 swxY w Y   |
                    d
||dd          }|S )zN
    Uploads a single file to bucket storage and returns a presigned URL.
    re   r    N)r   BTtotalunit
unit_scaledesc)Filenamerf   rg   r   Callback	ExtraArgsrj   rk   rl   rm    )r_   rw   rx   r-   r$   rs   getsizer   updateupload_filerz   )r   r   r8   r`   r   r   r^   rZ   key	file_sizeprogress_barupload_file_argsr   s                r   upload_file_to_bucketr     sh    $3<#@#@ K -mG,,%+
:V
!
!i
!
!
!C&ymLLLL..I	cd
 
 
 4	%!%$+
 
  	7,6[)33"23334 4 4 4 4 4 4 4 4 4 4 4 4 4 4   66C@@F 7  M s   9"B''B+.B+c                    t          |          \  }}|st          j        d          }|r| d|  n| }|t          | |          S t	          |          }t          |dd|           5 }	|                    t          j        |          ||||	j	                   ddd           n# 1 swxY w Y   |
                    d	||d
d          }
|
S )z\
    Uploads an in-memory object (bytes) to bucket storage and returns a presigned URL.
    re   r    N)r   r   Tr   )r   r   rj   rk   rl   rm   )r_   rw   rx   r-   r   r   upload_fileobjioBytesIOr   rz   )r   r   r8   r`   r   r^   rZ   r   r   r   r   s              r   upload_in_memory_objectr   H  sK    $3<#@#@ K -mG,,%+
:V
!
!i
!
!
!C&yIFFFFII	cd
 
 
 	
	""Jy!!"!( 	# 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
  66C@@F 7  M s   )2B''B+.B+)NNr   )N)r   NN)NNNN)NNN)'__doc__r   loggingrS   r$   r&   r   rw   rq   typingr   r   r   urllib.parser   tqdm_loggable.autor   r   r	   botocore.clientr
   	getLoggerr"   FMTbasicConfigINFOStreamHandlerr   rp   bytesr-   r6   dictr_   r   r   r   r   r   r   r7   r   <module>r      s   ' ' 
			      				        1 1 1 1 1 1 1 1 1 1 ! ! ! ! ! ! # # # # # # +000000******		2	3	3<  ',s>Sg>S>U>U=V W W W W  & "&!%#	#! #!#!##! #! 	#!
 	#! #! #! #!L  $ $(5( 5(4.5(
8L!8,<#==>5( 5( 5( 5(| !%1 1
 #1 1 1 1n  0) ) )` $(!% !%* *** 4.* #	*
 SM* * 	* * * *b $(!% $ $$$ 4.$ #	$
 SM$ 	$ $ $ $ $ $r7   