upload_3.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. def initiate(self, transport, stream, metadata, content_type,
  2. total_bytes=None, stream_final=True):
  3. """Initiate a resumable upload.
  4. By default, this method assumes your ``stream`` is in a "final"
  5. state ready to transmit. However, ``stream_final=False`` can be used
  6. to indicate that the size of the resource is not known. This can happen
  7. if bytes are being dynamically fed into ``stream``, e.g. if the stream
  8. is attached to application logs.
  9. If ``stream_final=False`` is used, :attr:`chunk_size` bytes will be
  10. read from the stream every time :meth:`transmit_next_chunk` is called.
  11. If one of those reads produces strictly fewer bites than the chunk
  12. size, the upload will be concluded.
  13. Args:
  14. transport (~requests.Session): A ``requests`` object which can
  15. make authenticated requests.
  16. stream (IO[bytes]): The stream (i.e. file-like object) that will
  17. be uploaded. The stream **must** be at the beginning (i.e.
  18. ``stream.tell() == 0``).
  19. metadata (Mapping[str, str]): The resource metadata, such as an
  20. ACL list.
  21. content_type (str): The content type of the resource, e.g. a JPEG
  22. image has content type ``image/jpeg``.
  23. total_bytes (Optional[int]): The total number of bytes to be
  24. uploaded. If specified, the upload size **will not** be
  25. determined from the stream (even if ``stream_final=True``).
  26. stream_final (Optional[bool]): Indicates if the ``stream`` is
  27. "final" (i.e. no more bytes will be added to it). In this case
  28. we determine the upload size from the size of the stream. If
  29. ``total_bytes`` is passed, this argument will be ignored.
  30. Returns:
  31. ~requests.Response: The HTTP response returned by ``transport``.
  32. """
  33. method, url, payload, headers = self._prepare_initiate_request(
  34. stream, metadata, content_type,
  35. total_bytes=total_bytes, stream_final=stream_final)
  36. result = _helpers.http_request(
  37. transport, method, url, data=payload, headers=headers,
  38. retry_strategy=self._retry_strategy)
  39. self._process_initiate_response(result)
  40. return result