o
    Mf                    @   s  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 ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl1m3Z3 ddl4m5Z5 ddl6m7Z7 e8 Z9G dd deZ:dd  Z;d!d" Z<dS )#z9Client for interacting with the Google Cloud Storage API.    N)AnonymousCredentials)page_iterator)_LocalStack)ClientWithProject)NotFound)_bucket_bound_hostname_url)_get_api_endpoint_override)_get_environ_project)_get_storage_emulator_override)_use_client_cert)_virtual_hosted_style_base_url)_DEFAULT_UNIVERSE_DOMAIN)_DEFAULT_SCHEME)_STORAGE_HOST_TEMPLATE)_NOW)_UTC)
Connection)get_expiration_seconds_v4get_v4_now_dtstampsensure_signed_credentials_sign_messageBatch)Bucket_item_to_blob_blobs_page_start)Blob)HMACKeyMetadata)	BucketACL)DefaultObjectACL)_DEFAULT_TIMEOUT)DEFAULT_RETRYc                       s8  e Zd ZdZdZ	 edddddi f fdd	Zedd Ze	d	d
 Z
e	dd Ze	dd Zejdd Zdd Zdd Ze	dd ZdeefddZdBddZdCddZddeedfddZdddejdeefddZddeddfd d!Zddeddfd"d#Zddeddfd$d%Zddeedfd&d'Zd(d) Zeddefd*d+Zeddefd,d-Z dddddddd.eef
d/d0Z!ddd.dddddded1efd2d3Z"ddddddddd4ddeedddfd5d6Z#dddd4dddeef	d7d8Z$ddedfd9d:Z%dddddeefd;d<Z&ddefd=d>Z'				.		?		dDd@dAZ(  Z)S )EClienta	  Client to bundle configuration needed for API requests.

    :type project: str or None
    :param project: the project which the client acts on behalf of. Will be
                    passed when creating a topic.  If not passed,
                    falls back to the default inferred from the environment.

    :type credentials: :class:`~google.auth.credentials.Credentials`
    :param credentials: (Optional) The OAuth2 Credentials to use for this
                        client. If not passed (and if no ``_http`` object is
                        passed), falls back to the default inferred from the
                        environment.

    :type _http: :class:`~requests.Session`
    :param _http: (Optional) HTTP object to make requests. Can be any object
                  that defines ``request()`` with the same interface as
                  :meth:`requests.Session.request`. If not passed, an
                  ``_http`` object is created that is bound to the
                  ``credentials`` for the current object.
                  This parameter should be considered private, and could
                  change in the future.

    :type client_info: :class:`~google.api_core.client_info.ClientInfo`
    :param client_info:
        The client info used to send a user-agent string along with API
        requests. If ``None``, then default info will be used. Generally,
        you only need to set this if you're developing your own library
        or partner tool.

    :type client_options: :class:`~google.api_core.client_options.ClientOptions` or :class:`dict`
    :param client_options: (Optional) Client options used to set user options on the client.
        A non-default universe domain or api endpoint should be set through client_options.

    :type use_auth_w_custom_endpoint: bool
    :param use_auth_w_custom_endpoint:
        (Optional) Whether authentication is required under custom endpoints.
        If false, uses AnonymousCredentials and bypasses authentication.
        Defaults to True. Note this is only used when a custom endpoint is set in conjunction.

    :type extra_headers: dict
    :param extra_headers:
        (Optional) Custom headers to be sent with the requests attached to the client.
        For example, you can add custom audit logging headers.
    )z7https://www.googleapis.com/auth/devstorage.full_controlz4https://www.googleapis.com/auth/devstorage.read_onlyz5https://www.googleapis.com/auth/devstorage.read_writeNTc                    s  d | _ |d u rd}d}nd}|tu rd }|| _|| _|| _d|i}	|r/t|tr/tjj	
|}|r9|jr9|j| _nd | _t }
t }|rK|jrK|j}n |
rP|
}n|rU|}n| jrit r_tdttj| jd }nd }||	d< |
rsdnd| _|	d d ur| js|s|d u rt }|d u rt }|d u rd}d}tt| j||||d | jj| jkrtd	j| j| jjd
|rd | _t| fi |	}||_|| _t  | _!d S )NT<none>Fclient_infozThe "GOOGLE_API_USE_CLIENT_CERTIFICATE" env variable is set to "true" and a non-default universe domain is configured. mTLS is not supported in any universe other thangoogleapis.com.)universe_domainapi_endpoint)projectcredentialsclient_options_httpzThe configured universe domain ({client_ud}) does not match the universe domain found in the credentials ({cred_ud}). If you haven't configured the universe domain explicitly, `googleapis.com` is the default.)	client_udcred_ud)"_base_connection_marker_initial_client_info_initial_client_options_extra_headers
isinstancedictgoogleapi_corer)   	from_dictr%   _universe_domainr
   r   r&   r   
ValueErrorr   r   format_is_emulator_setr   r	   superr"   __init___credentialsr'   r   extra_headers_connectionr   _batch_stack)selfr'   r(   r*   r$   r)   use_auth_w_custom_endpointr>   
no_projectconnection_kw_argsstorage_emulator_overrideapi_endpoint_overrider&   
connection	__class__ ]/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/google/cloud/storage/client.pyr<   v   s   








	
zClient.__init__c                 C   s   | dt  d}d|_|S )aU  Factory: return client with anonymous credentials.

        .. note::

           Such a client has only limited access to "public" buckets:
           listing their contents and downloading their blobs.

        :rtype: :class:`google.cloud.storage.client.Client`
        :returns: Instance w/ anonymous credentials and no project.
        r#   )r'   r(   N)r   r'   )clsclientrJ   rJ   rK   create_anonymous_client   s   zClient.create_anonymous_clientc                 C   s
   | j ptS N)r7   r   rA   rJ   rJ   rK   r%     s   
zClient.universe_domainc                 C      | j jS rO   )r?   API_BASE_URLrP   rJ   rJ   rK   r&     s   zClient.api_endpointc                 C   s   | j dur| j S | jS )zGet connection or batch on the client.

        :rtype: :class:`google.cloud.storage._http.Connection`
        :returns: The connection set on the client, or the batch
                  if one is set.
        N)current_batchr-   rP   rJ   rJ   rK   r?     s   
zClient._connectionc                 C   s   | j dur	td|| _ dS )a  Set connection on the client.

        Intended to be used by constructor (since the base class calls)
            self._connection = connection
        Will raise if the connection is set more than once.

        :type value: :class:`google.cloud.storage._http.Connection`
        :param value: The connection set on the client.

        :raises: :class:`ValueError` if connection has already been set.
        Nz Connection already set on client)r-   r8   )rA   valuerJ   rJ   rK   r?   $  s   

c                 C   s   | j | dS )zPush a batch onto our stack.

        "Protected", intended for use by batch context mgrs.

        :type batch: :class:`google.cloud.storage.batch.Batch`
        :param batch: newly-active batch
        N)r@   push)rA   batchrJ   rJ   rK   _push_batch5  s   zClient._push_batchc                 C   s
   | j  S )a  Pop a batch from our stack.

        "Protected", intended for use by batch context mgrs.

        :raises: IndexError if the stack is empty.
        :rtype: :class:`google.cloud.storage.batch.Batch`
        :returns: the top-most batch/transaction, after removing it.
        )r@   poprP   rJ   rJ   rK   
_pop_batch?  s   
	zClient._pop_batchc                 C   rQ   )zCurrently-active batch.

        :rtype: :class:`google.cloud.storage.batch.Batch` or ``NoneType`` (if
                no batch is active).
        :returns: The batch at the top of the batch stack.
        )r@   toprP   rJ   rJ   rK   rS   J  s   zClient.current_batchc                 C   s2   |du r| j }d| d}| j|||d}|d S )a  Get the email address of the project's GCS service account

        :type project: str
        :param project:
            (Optional) Project ID to use for retreiving GCS service account
            email address.  Defaults to the client's project.
        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :rtype: str
        :returns: service account email address
        N
/projects/z/serviceAccounttimeoutretryemail_address)r'   _get_resource)rA   r'   r]   r^   pathapi_responserJ   rJ   rK   get_service_account_emailT  s
   z Client.get_service_account_emailc                 C   s   t | ||dS )a0  Factory constructor for bucket object.

        .. note::
          This will not make an HTTP request; it simply instantiates
          a bucket object owned by this client.

        :type bucket_name: str
        :param bucket_name: The name of the bucket to be instantiated.

        :type user_project: str
        :param user_project: (Optional) The project ID to be billed for API
                             requests made via the bucket.

        :rtype: :class:`google.cloud.storage.bucket.Bucket`
        :returns: The bucket object created.
        )rM   nameuser_project)r   )rA   bucket_namere   rJ   rJ   rK   bucketp  s   zClient.bucketc                 C   s   t | |dS )a  Factory constructor for batch object.

        .. note::
          This will not make an HTTP request; it simply instantiates
          a batch object owned by this client.

        :type raise_exception: bool
        :param raise_exception:
            (Optional) Defaults to True. If True, instead of adding exceptions
            to the list of return responses, the final exception will be raised.
            Note that exceptions are unwrapped after all operations are complete
            in success or failure, and only the last exception is raised.

        :rtype: :class:`google.cloud.storage.batch.Batch`
        :returns: The batch object created.
        )rM   raise_exceptionr   )rA   rh   rJ   rJ   rK   rV     s   zClient.batchc              	   C      | j jd||||||dS )aV  Helper for bucket / blob methods making API 'GET' calls.

        Args:
            path str:
                The path of the resource to fetch.

            query_params Optional[dict]:
                HTTP query parameters to be passed

            headers Optional[dict]:
                HTTP headers to be passed

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

            _target_object (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                 :class:`~google.cloud.storage.bucket.blob`,             ]):
                Object to which future data is to be applied -- only relevant
                in the context of a batch.

        Returns:
            dict
                The JSON resource fetched

        Raises:
            google.cloud.exceptions.NotFound
                If the bucket is not found.
        GETmethodra   query_paramsheadersr]   r^   _target_objectr?   api_requestrA   ra   rm   rn   r]   r^   ro   rJ   rJ   rK   r`        8zClient._get_resourcec
                 C   s0   t j| jj||	d}
tj| |
|||||||d	S )Nr\   )	rM   rq   ra   item_to_value
page_tokenmax_resultsextra_params
page_start	page_size)	functoolspartialr?   rq   r   HTTPIterator)rA   ra   rt   ru   rv   rw   rx   ry   r]   r^   rq   rJ   rJ   rK   _list_resource  s   
zClient._list_resourcec              
   C      | j jd|||||||dS )a  Helper for bucket / blob methods making API 'PATCH' calls.

        Args:
            path str:
                The path of the resource to fetch.

            data dict:
                The data to be patched.

            query_params Optional[dict]:
                HTTP query parameters to be passed

            headers Optional[dict]:
                HTTP headers to be passed

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

            _target_object (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                 :class:`~google.cloud.storage.bucket.blob`,             ]):
                Object to which future data is to be applied -- only relevant
                in the context of a batch.

        Returns:
            dict
                The JSON resource fetched

        Raises:
            google.cloud.exceptions.NotFound
                If the bucket is not found.
        PATCHrl   ra   datarm   rn   r]   r^   ro   rp   rA   ra   r   rm   rn   r]   r^   ro   rJ   rJ   rK   _patch_resource     <zClient._patch_resourcec              
   C   r~   )a  Helper for bucket / blob methods making API 'PUT' calls.

        Args:
            path str:
                The path of the resource to fetch.

            data dict:
                The data to be patched.

            query_params Optional[dict]:
                HTTP query parameters to be passed

            headers Optional[dict]:
                HTTP headers to be passed

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

            _target_object (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                 :class:`~google.cloud.storage.bucket.blob`,             ]):
                Object to which future data is to be applied -- only relevant
                in the context of a batch.

        Returns:
            dict
                The JSON resource fetched

        Raises:
            google.cloud.exceptions.NotFound
                If the bucket is not found.
        PUTr   rp   r   rJ   rJ   rK   _put_resource:  r   zClient._put_resourcec              
   C   r~   )a  Helper for bucket / blob methods making API 'POST' calls.

        Args:
            path str:
                The path of the resource to which to post.

            data dict:
                The data to be posted.

            query_params Optional[dict]:
                HTTP query parameters to be passed

            headers Optional[dict]:
                HTTP headers to be passed

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

            _target_object (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                 :class:`~google.cloud.storage.bucket.blob`,             ]):
                Object to which future data is to be applied -- only relevant
                in the context of a batch.

        Returns:
            dict
                The JSON resource returned from the post.

        Raises:
            google.cloud.exceptions.NotFound
                If the bucket is not found.
        POSTr   rp   r   rJ   rJ   rK   _post_resource  s   =zClient._post_resourcec              	   C   ri   )aZ  Helper for bucket / blob methods making API 'DELETE' calls.

        Args:
            path str:
                The path of the resource to delete.

            query_params Optional[dict]:
                HTTP query parameters to be passed

            headers Optional[dict]:
                HTTP headers to be passed

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

            _target_object (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                 :class:`~google.cloud.storage.bucket.blob`,             ]):
                Object to which future data is to be applied -- only relevant
                in the context of a batch.

        Returns:
            dict
                The JSON resource fetched

        Raises:
            google.cloud.exceptions.NotFound
                If the bucket is not found.
        DELETErk   rp   rr   rJ   rJ   rK   _delete_resource  rs   zClient._delete_resourcec                 C   s2   t |tr|}|jdu r| |_|S t| |d}|S )a  Helper to return given bucket or create new by name.

        Args:
            bucket_or_name (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                  str,             ]):
                The bucket resource to pass or name to create.

        Returns:
            google.cloud.storage.bucket.Bucket
                The newly created bucket or the given one.
        N)rd   )r2   r   rM   _client)rA   bucket_or_namerg   rJ   rJ   rK   _bucket_arg_to_bucket  s   

zClient._bucket_arg_to_bucketc                 C   s"   |  |}|j| ||||d |S )a~	  Retrieve a bucket via a GET request.

        See [API reference docs](https://cloud.google.com/storage/docs/json_api/v1/buckets/get) and a [code sample](https://cloud.google.com/storage/docs/samples/storage-get-bucket-metadata#storage_get_bucket_metadata-python).

        Args:
            bucket_or_name (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                  str,             ]):
                The bucket resource to pass or name to create.

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            if_metageneration_match (Optional[long]):
                Make the operation conditional on whether the
                blob's current metageneration matches the given value.

            if_metageneration_not_match (Optional[long]):
                Make the operation conditional on whether the blob's
                current metageneration does not match the given value.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

        Returns:
            google.cloud.storage.bucket.Bucket
                The bucket matching the name provided.

        Raises:
            google.cloud.exceptions.NotFound
                If the bucket is not found.
        )rM   r]   if_metageneration_matchif_metageneration_not_matchr^   )r   reload)rA   r   r]   r   r   r^   rg   rJ   rJ   rK   
get_bucket!  s   
7zClient.get_bucketc                 C   s,   z| j |||||dW S  ty   Y dS w )a  Get a bucket by name, returning None if not found.

        You can use this if you would rather check for a None value
        than catching a NotFound exception.

        :type bucket_name: str
        :param bucket_name: The name of the bucket to get.

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type if_metageneration_match: long
        :param if_metageneration_match: (Optional) Make the operation conditional on whether the
                                        blob's current metageneration matches the given value.

        :type if_metageneration_not_match: long
        :param if_metageneration_not_match: (Optional) Make the operation conditional on whether the
                                            blob's current metageneration does not match the given value.

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :rtype: :class:`google.cloud.storage.bucket.Bucket` or ``NoneType``
        :returns: The bucket matching the name provided or None if not found.
        )r]   r   r   r^   N)r   r   )rA   rf   r]   r   r   r^   rJ   rJ   rK   lookup_bucketb  s   $zClient.lookup_bucketFc                    s   |  | i }|du r| j}| jr|du rt }|du rd}|dur&d|i}|dur5tjdtdd | _|durBt	|}||d< |durOt
	|}||d< |durW||d	< |	r]|	|d
<  fdd jD } j|d< |durt||d< |dur~d|i|d< | jd|||
| d} |  S )a  Create a new bucket via a POST request.

        See [API reference docs](https://cloud.google.com/storage/docs/json_api/v1/buckets/insert) and a [code sample](https://cloud.google.com/storage/docs/samples/storage-create-bucket#storage_create_bucket-python).

        Args:
            bucket_or_name (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                  str,             ]):
                The bucket resource to pass or name to create.
            requester_pays (bool):
                DEPRECATED. Use Bucket().requester_pays instead.
                (Optional) Whether requester pays for API requests for
                this bucket and its blobs.
            project (str):
                (Optional) The project under which the bucket is to be created.
                If not passed, uses the project set on the client.
            user_project (str):
                (Optional) The project ID to be billed for API requests
                made via created bucket.
            location (str):
                (Optional) The location of the bucket. If not passed,
                the default location, US, will be used. If specifying a dual-region,
                `data_locations` should be set in conjunction. See:
                https://cloud.google.com/storage/docs/locations
            data_locations (list of str):
                (Optional) The list of regional locations of a custom dual-region bucket.
                Dual-regions require exactly 2 regional locations. See:
                https://cloud.google.com/storage/docs/locations
            predefined_acl (str):
                (Optional) Name of predefined ACL to apply to bucket. See:
                https://cloud.google.com/storage/docs/access-control/lists#predefined-acl
            predefined_default_object_acl (str):
                (Optional) Name of predefined ACL to apply to bucket's objects. See:
                https://cloud.google.com/storage/docs/access-control/lists#predefined-acl
            enable_object_retention (bool):
                (Optional) Whether object retention should be enabled on this bucket. See:
                https://cloud.google.com/storage/docs/object-lock
            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

        Returns:
            google.cloud.storage.bucket.Bucket
                The newly created bucket.

        Raises:
            google.cloud.exceptions.Conflict
                If the bucket already exists.
        Nr#   r'   zFrequester_pays arg is deprecated. Use Bucket().requester_pays instead.   )
stacklevelpredefinedAclpredefinedDefaultObjectAcluserProjectenableObjectRetentionc                    s   i | ]}| j | qS rJ   )_properties).0keyrg   rJ   rK   
<dictcomp>  s    z(Client.create_bucket.<locals>.<dictcomp>rd   locationdataLocationscustomPlacementConfig/b)rm   r]   r^   ro   )r   r'   r:   r	   warningswarnPendingDeprecationWarningrequester_paysr   validate_predefinedr   _changesrd   r   _set_properties)rA   r   r   r'   re   r   data_locationspredefined_aclpredefined_default_object_aclenable_object_retentionr]   r^   rm   
propertiesrb   rJ   r   rK   create_bucket  s\   
P


	zClient.create_bucketmd5c                 C   s>   t |ts
t|}|j|| |||||||	|
||||d dS )a  Download the contents of a blob object or blob URI into a file-like object.

        See https://cloud.google.com/storage/docs/downloading-objects

        Args:
            blob_or_uri (Union[             :class:`~google.cloud.storage.blob.Blob`,              str,             ]):
                The blob resource to pass or URI to download.

            file_obj (file):
                A file handle to which to write the blob's data.

            start (int):
                (Optional) The first byte in a range to be downloaded.

            end (int):
                (Optional) The last byte in a range to be downloaded.

            raw_download (bool):
                (Optional) If true, download the object without any expansion.

            if_etag_match (Union[str, Set[str]]):
                (Optional) See :ref:`using-if-etag-match`

            if_etag_not_match (Union[str, Set[str]]):
                (Optional) See :ref:`using-if-etag-not-match`

            if_generation_match (long):
                (Optional) See :ref:`using-if-generation-match`

            if_generation_not_match (long):
                (Optional) See :ref:`using-if-generation-not-match`

            if_metageneration_match (long):
                (Optional) See :ref:`using-if-metageneration-match`

            if_metageneration_not_match (long):
                (Optional) See :ref:`using-if-metageneration-not-match`

            timeout ([Union[float, Tuple[float, float]]]):
                (Optional) The amount of time, in seconds, to wait
                for the server response.  See: :ref:`configuring_timeouts`

            checksum (str):
                (Optional) The type of checksum to compute to verify the integrity
                of the object. The response headers must contain a checksum of the
                requested type. If the headers lack an appropriate checksum (for
                instance in the case of transcoded or ranged downloads where the
                remote service does not know the correct checksum, including
                downloads where chunk_size is set) an INFO-level log will be
                emitted. Supported values are "md5", "crc32c" and None. The default
                is "md5".
            retry (google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy)
                (Optional) How to retry the RPC. A None value will disable
                retries. A google.api_core.retry.Retry value will enable retries,
                and the object will define retriable response codes and errors and
                configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a
                Retry object and activates it only if certain conditions are met.
                This class exists to provide safe defaults for RPC calls that are
                not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a
                condition such as if_metageneration_match is set.

                See the retry.py source code and docstrings in this package
                (google.cloud.storage.retry) for information on retry types and how
                to configure them.

                Media operations (downloads and uploads) do not support non-default
                predicates in a Retry object. The default will always be used. Other
                configuration changes for Retry objects such as delays and deadlines
                are respected.
        )rM   startendraw_downloadif_etag_matchif_etag_not_matchif_generation_matchif_generation_not_matchr   r   r]   checksumr^   N)r2   r   from_string_prep_and_do_download)rA   blob_or_urifile_objr   r   r   r   r   r   r   r   r   r]   r   r^   rJ   rJ   rK   download_blob_to_file   s$   
^

zClient.download_blob_to_filenoAclc                 C   s   |  |}d|
i}|dur||d< |dur||d< |dur!||d< |dur)||d< |dur1||d< |dur9||d< |	durA|	|d	< |durI||d
< |durQ||d< |durY||d< |jdurc|j|d< |jd }| j|t|||t|||d	}||_t |_|S )ag  Return an iterator used to find blobs in the bucket.

        If :attr:`user_project` is set, bills the API request to that project.

        .. note::
          List prefixes (directories) in a bucket using a prefix and delimiter.
          See a [code sample](https://cloud.google.com/storage/docs/samples/storage-list-files-with-prefix#storage_list_files_with_prefix-python)
          listing objects using a prefix filter.

        Args:
            bucket_or_name (Union[                 :class:`~google.cloud.storage.bucket.Bucket`,                  str,             ]):
                The bucket resource to pass or name to create.

            max_results (int):
                (Optional) The maximum number of blobs to return.

            page_token (str):
                (Optional) If present, return the next batch of blobs, using the
                value, which must correspond to the ``nextPageToken`` value
                returned in the previous response.  Deprecated: use the ``pages``
                property of the returned iterator instead of manually passing the
                token.

            prefix (str):
                (Optional) Prefix used to filter blobs.

            delimiter (str):
                (Optional) Delimiter, used with ``prefix`` to
                emulate hierarchy.

            start_offset (str):
                (Optional) Filter results to objects whose names are
                lexicographically equal to or after ``startOffset``. If
                ``endOffset`` is also set, the objects listed will have names
                between ``startOffset`` (inclusive) and ``endOffset``
                (exclusive).

            end_offset (str):
                (Optional) Filter results to objects whose names are
                lexicographically before ``endOffset``. If ``startOffset`` is
                also set, the objects listed will have names between
                ``startOffset`` (inclusive) and ``endOffset`` (exclusive).

            include_trailing_delimiter (boolean):
                (Optional) If true, objects that end in exactly one instance of
                ``delimiter`` will have their metadata included in ``items`` in
                addition to ``prefixes``.

            versions (bool):
                (Optional) Whether object versions should be returned
                as separate blobs.

            projection (str):
                (Optional) If used, must be 'full' or 'noAcl'.
                Defaults to ``'noAcl'``. Specifies the set of
                properties to return.

            fields (str):
                (Optional) Selector specifying which fields to include
                in a partial response. Must be a list of fields. For
                example to get a partial response with just the next
                page token and the name and language of each blob returned:
                ``'items(name,contentLanguage),nextPageToken'``.
                See: https://cloud.google.com/storage/docs/json_api/v1/parameters#fields

            page_size (int):
                (Optional) Maximum number of blobs to return in each page.
                Defaults to a value set by the API.

            timeout (Optional[Union[float, Tuple[float, float]]]):
                The amount of time, in seconds, to wait for the server response.

                Can also be passed as a tuple (connect_timeout, read_timeout).
                See :meth:`requests.Session.request` documentation for details.

            retry (Optional[Union[google.api_core.retry.Retry, google.cloud.storage.retry.ConditionalRetryPolicy]]):
                How to retry the RPC. A None value will disable retries.
                A google.api_core.retry.Retry value will enable retries, and the object will
                define retriable response codes and errors and configure backoff and timeout options.

                A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
                activates it only if certain conditions are met. This class exists to provide safe defaults
                for RPC calls that are not technically safe to retry normally (due to potential data
                duplication or other side-effects) but become safe to retry if a condition such as
                if_metageneration_match is set.

                See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
                information on retry types and how to configure them.

            match_glob (str):
                (Optional) A glob pattern used to filter results (for example, foo*bar).
                The string value must be UTF-8 encoded. See:
                https://cloud.google.com/storage/docs/json_api/v1/objects/list#list-object-glob

            include_folders_as_prefixes (bool):
                (Optional) If true, includes Folders and Managed Folders in the set of
                ``prefixes`` returned by the query. Only applicable if ``delimiter`` is set to /.
                See: https://cloud.google.com/storage/docs/managed-folders

            soft_deleted (bool):
                (Optional) If true, only soft-deleted objects will be listed as distinct results in order of increasing
                generation number. This parameter can only be used successfully if the bucket has a soft delete policy.
                Note ``soft_deleted`` and ``versions`` cannot be set to True simultaneously. See:
                https://cloud.google.com/storage/docs/soft-delete

        Returns:
            Iterator of all :class:`~google.cloud.storage.blob.Blob`
            in this bucket matching the arguments. The RPC call
            returns a response when the iterator is consumed.

            As part of the response, you'll also get back an iterator.prefixes entity that lists object names
            up to and including the requested delimiter. Duplicate entries are omitted from this list.
        
projectionNprefix	delimiter	matchGlobstartOffset	endOffsetincludeTrailingDelimiterversionsfieldsincludeFoldersAsPrefixessoftDeletedr   z/o)ru   rv   rw   rx   ry   r]   r^   )	r   re   ra   r}   r   r   rg   setprefixes)rA   r   rv   ru   r   r   start_offset
end_offsetinclude_trailing_delimiterr   r   r   ry   r]   r^   
match_globinclude_folders_as_prefixessoft_deletedrg   rw   ra   iteratorrJ   rJ   rK   
list_blobs  sP    
	


zClient.list_blobsc
              
   C   s   i }
|du r	| j }| jr|du rt }|du rd}|dur!d|i}
|dur)||
d< ||
d< |dur5||
d< | jdt|||
|||	dS )	a+
  Get all buckets in the project associated to the client.

        This will not populate the list of blobs available in each
        bucket.

        See [API reference docs](https://cloud.google.com/storage/docs/json_api/v1/buckets/list) and a [code sample](https://cloud.google.com/storage/docs/samples/storage-list-buckets#storage_list_buckets-python).

        :type max_results: int
        :param max_results: (Optional) The maximum number of buckets to return.

        :type page_token: str
        :param page_token:
            (Optional) If present, return the next batch of buckets, using the
            value, which must correspond to the ``nextPageToken`` value
            returned in the previous response.  Deprecated: use the ``pages``
            property of the returned iterator instead of manually passing the
            token.

        :type prefix: str
        :param prefix: (Optional) Filter results to buckets whose names begin
                       with this prefix.

        :type projection: str
        :param projection:
            (Optional) Specifies the set of properties to return. If used, must
            be 'full' or 'noAcl'. Defaults to 'noAcl'.

        :type fields: str
        :param fields:
            (Optional) Selector specifying which fields to include in a partial
            response. Must be a list of fields. For example to get a partial
            response with just the next page token and the language of each
            bucket returned: 'items/id,nextPageToken'

        :type project: str
        :param project: (Optional) The project whose buckets are to be listed.
                        If not passed, uses the project set on the client.

        :type page_size: int
        :param page_size: (Optional) Maximum number of buckets to return in each page.
            Defaults to a value set by the API.

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :rtype: :class:`~google.api_core.page_iterator.Iterator`
        :raises ValueError: if both ``project`` is ``None`` and the client's
                            project is also ``None``.
        :returns: Iterator of all :class:`~google.cloud.storage.bucket.Bucket`
                  belonging to this project.
        Nr#   r'   r   r   r   r   )ru   rv   rw   ry   r]   r^   )r'   r:   r	   r}   _item_to_bucket)rA   rv   ru   r   r   r   r'   ry   r]   r^   rw   rJ   rJ   rK   list_bucketsO  s2   EzClient.list_bucketsc                 C   sh   |du r| j }d| d}d|i}|dur||d< | j|d|||d}t| }	|d |	_|d }
|	|
fS )	a0  Create an HMAC key for a service account.

        :type service_account_email: str
        :param service_account_email: e-mail address of the service account

        :type project_id: str
        :param project_id: (Optional) Explicit project ID for the key.
            Defaults to the client's project.

        :type user_project: str
        :param user_project: (Optional) This parameter is currently ignored.

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry: (Optional) How to retry the RPC. A None value will disable retries.
            A google.api_core.retry.Retry value will enable retries, and the object will
            define retriable response codes and errors and configure backoff and timeout options.

            A google.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and
            activates it only if certain conditions are met. This class exists to provide safe defaults
            for RPC calls that are not technically safe to retry normally (due to potential data
            duplication or other side-effects) but become safe to retry if a condition such as
            if_metageneration_match is set.

            See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for
            information on retry types and how to configure them.

        :rtype:
            Tuple[:class:`~google.cloud.storage.hmac_key.HMACKeyMetadata`, str]
        :returns: metadata for the created key, plus the bytes of the key's secret, which is an 40-character base64-encoded string.
        Nr[   	/hmacKeysserviceAccountEmailr   )rm   r]   r^   metadatasecret)r'   r   r   r   )rA   service_account_email
project_idre   r]   r^   ra   	qs_paramsrb   r   r   rJ   rJ   rK   create_hmac_key  s"   +
zClient.create_hmac_keyc           
      C   sd   |du r| j }d| d}i }	|dur||	d< |dur||	d< |dur'||	d< | j|t||	||dS )ai  List HMAC keys for a project.

        :type max_results: int
        :param max_results:
            (Optional) Max number of keys to return in a given page.

        :type service_account_email: str
        :param service_account_email:
            (Optional) Limit keys to those created by the given service account.

        :type show_deleted_keys: bool
        :param show_deleted_keys:
            (Optional) Included deleted keys in the list. Default is to
            exclude them.

        :type project_id: str
        :param project_id: (Optional) Explicit project ID for the key.
            Defaults to the client's project.

        :type user_project: str
        :param user_project: (Optional) This parameter is currently ignored.

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :rtype:
            Tuple[:class:`~google.cloud.storage.hmac_key.HMACKeyMetadata`, str]
        :returns: metadata for the created key, plus the bytes of the key's secret, which is an 40-character base64-encoded string.
        Nr[   r   r   showDeletedKeysr   )rv   rw   r]   r^   )r'   r}   _item_to_hmac_key_metadata)
rA   rv   r   show_deleted_keysr   re   r]   r^   ra   rw   rJ   rJ   rK   list_hmac_keys  s$   -zClient.list_hmac_keysc                 C   s   t | |||}|j|d |S )aR  Return a metadata instance for the given HMAC key.

        :type access_id: str
        :param access_id: Unique ID of an existing key.

        :type project_id: str
        :param project_id: (Optional) Project ID of an existing key.
            Defaults to client's project.

        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type user_project: str
        :param user_project: (Optional) This parameter is currently ignored.
        )r]   )r   r   )rA   	access_idr   re   r]   r   rJ   rJ   rK   get_hmac_key_metadata=  s   zClient.get_hmac_key_metadatahttpc              
   C   s  |r|rt d|du r| jn|}t| t \}}dj|j|d}d|id|id|id|id	d
ig}|p6g }i }t|p=i  D ]\}}|dsU|||< |	||i qA||7 }t
tjdd}|du rn|tjdd }|tjt|d }tjtt|| d d dd}t|d}|r|
rt|||
}t|}n||}t|d}||d
||||dd |rt| j |dd}n|rt!||	 d}n	| j  d| d}||dS )a
  Generate a V4 signed policy object. Generated policy object allows user to upload objects with a POST request.

        .. note::

            Assumes ``credentials`` implements the
            :class:`google.auth.credentials.Signing` interface. Also assumes
            ``credentials`` has a ``service_account_email`` property which
            identifies the credentials.

        See a [code sample](https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_generate_signed_post_policy_v4.py).

        :type bucket_name: str
        :param bucket_name: Bucket name.

        :type blob_name: str
        :param blob_name: Object name.

        :type expiration: Union[Integer, datetime.datetime, datetime.timedelta]
        :param expiration: Policy expiration time. If a ``datetime`` instance is
                           passed without an explicit ``tzinfo`` set,  it will be
                           assumed to be ``UTC``.

        :type conditions: list
        :param conditions: (Optional) List of POST policy conditions, which are
                           used to restrict what is allowed in the request.

        :type fields: dict
        :param fields: (Optional) Additional elements to include into request.

        :type credentials: :class:`google.auth.credentials.Signing`
        :param credentials: (Optional) Credentials object with an associated private
                            key to sign text.

        :type virtual_hosted_style: bool
        :param virtual_hosted_style:
            (Optional) If True, construct the URL relative to the bucket
            virtual hostname, e.g., '<bucket-name>.storage.googleapis.com'.
            Incompatible with bucket_bound_hostname.

        :type bucket_bound_hostname: str
        :param bucket_bound_hostname:
            (Optional) If passed, construct the URL relative to the bucket-bound hostname.
            Value can be bare or with a scheme, e.g., 'example.com' or 'http://example.com'.
            Incompatible with virtual_hosted_style.
            See: https://cloud.google.com/storage/docs/request-endpoints#cname

        :type scheme: str
        :param scheme:
            (Optional) If ``bucket_bound_hostname`` is passed as a bare hostname, use
            this value as a scheme. ``https`` will work only when using a CDN.
            Defaults to ``"http"``.

        :type service_account_email: str
        :param service_account_email: (Optional) E-mail address of the service account.

        :type access_token: str
        :param access_token: (Optional) Access token for a service account.

        :raises: :exc:`ValueError` when mutually exclusive arguments are used.

        :rtype: dict
        :returns: Signed POST policy.
        zLOnly one of virtual_hosted_style and bucket_bound_hostname can be specified.Nz.{email}/{datestamp}/auto/storage/goog4_request)email	datestamprg   r   x-goog-datex-goog-credentialx-goog-algorithmzGOOG4-RSA-SHA256z	x-ignore-)tzinfor   )hours)secondsZ)
conditions
expiration),:)
separatorszutf-8)r   r   r   r   zx-goog-signaturepolicyT)trailing_slash/)urlr   )"r8   r=   r   r   r9   signer_emailsorteditems
startswithappendr   r   replacedatetime	timedeltar   jsondumpscollectionsOrderedDict	isoformatbase64	b64encodeencoder   	b64decode
sign_bytesbinasciihexlifydecodeupdater   r&   r   )rA   rf   	blob_namer   r   r   r(   virtual_hosted_stylebucket_bound_hostnameschemer   access_token	timestampr   x_goog_credentialrequired_conditionspolicy_fieldsr   rT   nowpolicy_expiresr   str_to_sign	signaturesignature_bytesr   rJ   rJ   rK   generate_signed_post_policy_v4U  s   M




z%Client.generate_signed_post_policy_v4rO   )T)NNNFNr   NN)*__name__
__module____qualname____doc__SCOPEr.   r<   classmethodrN   propertyr%   r&   r?   setterrW   rY   rS   r    r!   rc   rg   rV   r`   r   _do_nothing_page_startr}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  __classcell__rJ   rJ   rH   rK   r"   A   s:   - 












F

K
K
K
B
D
2
 
u
 @
l
B
F
r"   c                 C   s$   | d}t| j|}|| |S )aJ  Convert a JSON bucket to the native object.

    :type iterator: :class:`~google.api_core.page_iterator.Iterator`
    :param iterator: The iterator that has retrieved the item.

    :type item: dict
    :param item: An item to be converted to a bucket.

    :rtype: :class:`.Bucket`
    :returns: The next bucket in the page.
    rd   )getr   rM   r   )r   itemrd   rg   rJ   rJ   rK   r     s   

r   c                 C   s   t | j}||_|S )a  Convert a JSON key metadata resource to the native object.

    :type iterator: :class:`~google.api_core.page_iterator.Iterator`
    :param iterator: The iterator that has retrieved the item.

    :type item: dict
    :param item: An item to be converted to a key metadata instance.

    :rtype: :class:`~google.cloud.storage.hmac_key.HMACKeyMetadata`
    :returns: The next key metadata instance in the page.
    )r   rM   r   )r   r&  r   rJ   rJ   rK   r     s   
r   )=r  r  r  r   r   rz   r   r   google.api_core.client_optionsr4   google.auth.credentialsr   google.api_corer   google.cloud._helpersr   google.cloud.clientr   google.cloud.exceptionsr   google.cloud.storage._helpersr   r   r	   r
   r   r   r   r   r   r   r   google.cloud.storage._httpr   google.cloud.storage._signingr   r   r   r   google.cloud.storage.batchr   google.cloud.storage.bucketr   r   r   google.cloud.storage.blobr   google.cloud.storage.hmac_keyr   google.cloud.storage.aclr   r   google.cloud.storage.constantsr    google.cloud.storage.retryr!   objectr.   r"   r   r   rJ   rJ   rJ   rK   <module>   sh                F