123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- def initiate(self, transport, stream, metadata, content_type,
- total_bytes=None, stream_final=True):
- """Initiate a resumable upload.
- By default, this method assumes your ``stream`` is in a "final"
- state ready to transmit. However, ``stream_final=False`` can be used
- to indicate that the size of the resource is not known. This can happen
- if bytes are being dynamically fed into ``stream``, e.g. if the stream
- is attached to application logs.
- If ``stream_final=False`` is used, :attr:`chunk_size` bytes will be
- read from the stream every time :meth:`transmit_next_chunk` is called.
- If one of those reads produces strictly fewer bites than the chunk
- size, the upload will be concluded.
- Args:
- transport (~requests.Session): A ``requests`` object which can
- make authenticated requests.
- stream (IO[bytes]): The stream (i.e. file-like object) that will
- be uploaded. The stream **must** be at the beginning (i.e.
- ``stream.tell() == 0``).
- metadata (Mapping[str, str]): The resource metadata, such as an
- ACL list.
- content_type (str): The content type of the resource, e.g. a JPEG
- image has content type ``image/jpeg``.
- total_bytes (Optional[int]): The total number of bytes to be
- uploaded. If specified, the upload size **will not** be
- determined from the stream (even if ``stream_final=True``).
- stream_final (Optional[bool]): Indicates if the ``stream`` is
- "final" (i.e. no more bytes will be added to it). In this case
- we determine the upload size from the size of the stream. If
- ``total_bytes`` is passed, this argument will be ignored.
- Returns:
- ~requests.Response: The HTTP response returned by ``transport``.
- """
- method, url, payload, headers = self._prepare_initiate_request(
- stream, metadata, content_type,
- total_bytes=total_bytes, stream_final=stream_final)
- result = _helpers.http_request(
- transport, method, url, data=payload, headers=headers,
- retry_strategy=self._retry_strategy)
- self._process_initiate_response(result)
- return result
|