
                         v    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlmZ	 ddl
mZ dd	lmZ d
 Zd Zd Zy)z+Helper functions for making gRPC API calls.    )absolute_import)division)unicode_literalsN)	cloud_api)errors)encryption_util)logc                 $    dj                  |       S )z9Returns the bucket resource name as expected by gRPC API.zprojects/_/buckets/{})format)bucket_names    8lib/googlecloudsdk/api_lib/storage/gcs_grpc/grpc_util.pyget_full_bucket_namer      s    	 	'	'	44    c           	      ^   ||j                   t        j                  j                  k(  r| j                  j                  t        j                  t        j                  |j                  j                  d            t        j                  |j                  j                  d                  S y )Nzutf-8)encryption_algorithmencryption_key_bytesencryption_key_sha256_bytes)typer   KeyTypeCSEKtypesCommonObjectRequestParamsENCRYPTION_ALGORITHMbase64	b64decodekeyencodesha256)gapic_clientdecryption_keys     r   get_encryption_request_paramsr!   $   s     


!8!8!=!=
=77,AA#--%%g.0$*$4$4!!((1%3	 8   r   c	                    t        |j                  j                        }	| j                  j	                  |	|j                  j
                  |j                  rt        |j                        nd||||z
  dz   ndt        | |            }
| j                  j                  |
      }|}d}|D ]n  }|j                  j                  }|s	 |j                  |       |r&|j)                         D ]  }|j+                  |        |t-        |      z  }|sg ||       p |||z
  dz   n|j.                  }||z
  }||k7  r?|s<d	j1                  ||      }t%        j2                  |       t5        j6                  |      yy# t        $ r8 |t        j                   j"                  u rt%        j&                  d       d}Y   w xY w)
z Downloads the object using gRPC.N   r   )bucketobject_
generationread_offset
read_limitcommon_object_request_params)requestFz4Writing to download stream raised broken pipe error.TzDownload not completed. Target size={}, downloaded data={}. The input stream terminated before the entire content was read, possibly due to a network condition.)r   storage_urlr   r   ReadObjectRequestresource_namer&   intr!   storageread_objectchecksummed_datacontentwriteBrokenPipeErrorr   DownloadStrategyONE_SHOTr	   infovaluesupdatelensizer   debugcloud_errorsRetryableApiError)r   cloud_resourcedownload_stream	digestersprogress_callback
start_byteend_bytedownload_strategyr    r   r*   streamprocessed_bytesdestination_pipe_is_brokenresponsedatahash_objecttarget_sizetotal_downloaded_datamessages                       r   download_objectrO   2   s    %^%?%?%K%KL+00((66,:,E,E#n''
(4.6.BJ&*#@
$ 1 ' ++G+<& /$h$$,,Dd# 
$++-K


T
" . T"o	/*% * $,#7ha^=P=P  *J6))2L	006.10 
 IIg

(
(
11 3M)'  	 : : C CC
((I
J'+
$
s   	E==<F><F>)__doc__
__future__r   r   r   r   googlecloudsdk.api_lib.storager   r   r=   "googlecloudsdk.command_lib.storager   googlecloudsdk.corer	   r   r!   rO    r   r   <module>rV      s4    2 '  '  4 A > #5
B2r   